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开发者_C百科
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
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 :)
it's not a weird. It is the most popular error for paperclip. And it's not about paperclip, actually, but about ImageMagick.
- Did you install ImageMagick?
- 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
精彩评论