3

I have a Rails 3 app using Paperclip 2.3.8. I have the following specified in my model:

validates_attachment_content_type :file,
  :content_type => ['image/jpeg', 'image/png', 'image/gif',
                    'image/pjpeg', 'image/x-png'], 
  :message => 'Not a valid image file.'

But, when I test a bogus upload, instead of "Not a valid image file." I get this weird error message:

/var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
is not recognized by the 'identify' command.

Any ideas what's going wrong here??

-- EDIT --

For what it's worth I have already covered the ImageMagick/Rmagick steps from the similar question mentioned in the comments (Thanks fl00r!).

One thing that occurs to me (now that I'm on the track of it being an ImageMagick error) is that I have a watermark processor on this image attachment.

So, maybe it's trying to do the watermark processor before it tries to validate and that is where the error message is coming from?

-- EDIT --

I tried removing the processor and that didn't change the error message... so, not sure what to try next.

-- EDIT --

:) Here's the whole model, as requested.

require 'paperclip_processors/watermark'

class Attachment < ActiveRecord::Base
  # RELATIONSHIPS
  belongs_to :photo
  belongs_to :user
  has_attached_file :file,
    :processors => [:watermark],
    :styles =>  {
      :full => "960",
      :half => "470",
      :third => "306",
      :fourth => "225",
      :fifth => "176x132#",
      :tile => "176x158>",
      :sixth => "145x109#",
      :eighth => "106x80#",
      :tenth => "87x65#",
      :marked => { :geometry => "470",
        :watermark_path => "#{Rails.root}/public/images/watermark.png",
        :position => 'Center' }
    },
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :path => "photos/:user_id/:id/:username_:id_:style.:extension"

  # VALIDATIONS
  validates_attachment_presence :file
  validates_attachment_content_type :file,
    :content_type => ['image/jpeg', 'image/png', 'image/gif',
                      'image/pjpeg', 'image/x-png'],
    :message => 'Not a valid image file.'
  validate :file_dimensions, :unless => "errors.any?"

  # CUSTOM VALIDATIONS
  def file_dimensions
    dimensions = Paperclip::Geometry.from_file(file.to_file(:original))
    self.width = dimensions.width
    self.height = dimensions.height
    if dimensions.width < 1600 && dimensions.height < 1600
      errors.add(:file,'Width or height must be at least 1600px')
    end
  end

  # MAINTENANCE METHODS
  def self.orphans
    where( :photo_id => nil )
  end
end
Andrew
  • 42,517
  • 51
  • 181
  • 281
  • 1
    possible duplicate of [rails paperclip and passenger `is not recognized by the 'identify' command`](http://stackoverflow.com/questions/1996102/rails-paperclip-and-passenger-is-not-recognized-by-the-identify-command) – fl00r Apr 04 '11 at 22:43
  • I have a processor on this image file -- is that possibly where the error lies? – Andrew Apr 04 '11 at 22:55
  • I don't know. Try to update paperclip gem – fl00r Apr 04 '11 at 23:25
  • I'm on 2.3.8 which appears to be the most recent version. I 'reloaded' it just to be sure, but the error is the same. Thanks for helping :) – Andrew Apr 04 '11 at 23:44

2 Answers2

5

I think I figured out the problem.

Try removing the :styles from your model and you will see that the 'identify' error message goes way and the model validates as expected.

The problem is Paperclip is processing the styles even though the content_type validation has failed. It tries to process your pdf as an image and then you get the error:

/var/folders/cs/cs-jiL3ZH1WOkgLrcqa5Ck+++TI/-Tmp-/stream20110404-43533-vm7eza.pdf
is not recognized by the 'identify' command.

The solution is to skip the processing if the validation fails, by adding this to your model:

before_post_process :skip_if_invalid

def skip_if_invalid
  return false unless self.valid?
end

This way Paperclip won't try to turn files that are not images into thumbnails :)

deb
  • 12,326
  • 21
  • 67
  • 86
0

it's not a weird. It is the most popular error for paperclip. And it's not about paperclip, actually, but about ImageMagick.

  1. Did you install ImageMagick?
  2. Did you added image_magick command_path via initializer?

If you have istalled IM poperly now checkout its location:

which identify
#=> it will return some path

Create new file in your Rails application config/initializers/paperclip.rb:

Paperclip.options[:command_path] = "path/to/identify"

Also you can add :whiny => false option to your has_attached_file

has_attached_file :picture, :styles => { ... }, :whiny => false

So it won't throw any errors if something went wrong

Also you can read here if you want to store pictures and files in one model and want to ignore styling for non-image files:

Paperclip process images only

Community
  • 1
  • 1
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • Hmm, I do have ImageMagick, I checked the path using `which identify` and I added the command path option to the initializer and restarted the server, but that didn't fix the error... Also, it doesn't seem like that would work for my production environment (Heroku), and ideas how to solve the issue there? – Andrew Apr 04 '11 at 22:49
  • Also, in looking at the other question you posted, I also have `rmagick` in my gemfile. – Andrew Apr 04 '11 at 22:51
  • 1
    Whiny false gives no error message, just silent failure, that won't work for me -- an funky error message is better than silent failure for my users. – Andrew Apr 04 '11 at 22:53
  • what will happened if you'll upload text file? same issue? – fl00r Apr 04 '11 at 22:55
  • Yeah, every file type. I made another possible guess in an edit to the question... – Andrew Apr 04 '11 at 22:59
  • This model is just for images, but thanks for the other link! – Andrew Apr 04 '11 at 23:00
  • try to remove your processor and you'll see if it is an issue ) – fl00r Apr 04 '11 at 23:02