4
votes

I use Rails 3 and paperclip. My logic allows user to upload an image. That works fine unless the user selects a file that is not an image.

If the user picks a text file, for instance, validation passes but ends up with this error:

5 errors prohibited the profile update:

Profile pic content type is not one of image/jpeg, image/png, image/gif
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-17xuiu4-0.js is not recognized by the 'identify' command.

At least the first error refers to the file type. But if the user uploads some more specific file, like a .PXM, then Rails behaves strange and shows this:

4 errors prohibited the profile update:

Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.
Profile pic /var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/stream20101118-229-1scwkg7-0.pxm is not recognized by the 'identify' command.

Does anyone know what's going on here? I have the following code in my model:

validates_attachment_content_type :profile_pic, :content_type=>['image/jpeg', 'image/png', 'image/gif']

...and this paperclip initializer:

Paperclip.options[:command_path] = "/opt/local/bin/"

ImageMagik appears to be installed and set up correctly:

$ which Magick-config
/opt/local/bin/Magick-config

Thanks!

2
What does which identify, locate identify or find / -name identify return? - Eric
$ which identify /opt/local/bin/identify - AnApprentice
I fixed this with cocaine 0.3.2. Please see stackoverflow.com/questions/12753157/… - Leo Lukin

2 Answers

3
votes

I had the same problem with Paperclip and Rails 2.3.8. In your Model's has_attached_file declaration, remove the :styles for any non-image files.

3
votes

Just put the code below on model. It will not process non-image file.

before_post_process :image?
def image?
  !(data_content_type =~ /^image.*/).nil?
end