Paperclip file name in validation message - ruby-on-rails

Here is what I'm trying to do in the model:
has_attached_file :photo, :styles => self.image_sizes, :whiny => false
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'],
:message => I18n.t('paperclip.invalid_image_type', :file => self.photo.original_file_name)
I cant find the solution how should I get file name in original_file_name:
NameError (undefined local variable or method `photo_file_name' for #<Class:0xaafb004>):
or
NoMethodError (undefined method `photo' for #<Class:0xb303e7c>):

Problem is self is not the instance, but rather Class.
You can use the uploaded content-type as follows:
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'],
:message => :inclusion
and then in your translation file, add
activerecord.errors.models.<modelname>.attributes.photo.inclusion: "%{value} is not allowed"
where value will be replaced with the uploaded content type

Try photo_file_name instead of photo.original_file_name.
For more information on this, refer to Method: Paperclip::Attachment#original_filename
Hope it helps.

http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails.html
self.photo.url

Try using
self.photo.instance_read(:file_name)
For more information on Paperclip::Attachment#instance_read, refer to the docs here.
Hope this helps.

Related

What is the content type of json file to AWS S3 with ruby?

I'd like to upload json file to S3 by ruby with paperclip. I coded as following, but it returned the following error.
undefined method `merge' for "application/json":String
Could you tell me how to set content-type of json-file?
product_definition.rb
class ProductDefinition < ActiveRecord::Base
belongs_to :product
has_attached_file :meta_data,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:url => ":s3_domain_url",
:path => "/assets/:id/:style/:basename.:extension",
:s3_host_name => "s3-ap-northeast-1.amazonaws.com"
validates_attachment :meta_data, content_type: 'application/json'
end
products_controller.rb
#product_definition = ProductDefinition.create(meta_data:sample.json)
If you're using a recent version of Paperclip it looks like you formatted the arguments to validates_attachment_content_type incorrectly. I think it should be:
validates_attachment :meta_data, content_type: { content_type: 'application/json' }
See the example in the Paperclip Readme: https://github.com/thoughtbot/paperclip#validations
EDIT: How I came to this conclusion is by noticing the error says it tried to call merge on the string. Merge is meant to be called on a Hash, so therefore it expects a Hash value for :content_type.
What you have is exactly what the current docs on github state, but it does not work:
https://github.com/thoughtbot/paperclip
Other examples use a different way of formatting matching types - so very confusing.
What does seem to work, is a syntax-change which appears to date back years, which is:
validates_attachment :meta_data, attachment_content_type: {content_type: 'application/json'}
You can also put a set of acceptable content-types in an array, such as
validates_attachment :meta_data, attachment_content_type: {content_type: ['application/json', 'application/doc']}

Paperclip security validation error mp4

I am using Paperclip to upload videos and keep getting a Security Validation error about the content type
The error when saving an mp4 to my model class is "content type discovered from file command: video/mp4. See documentation to allow this combination."
The save looks like this
AssignmentEventVideo.create(video: "https://s3-ap-southeast-2.amazonaws.com/dev/upload/0c857445-09ad-44b6-bbfa-810a9974a501/ScreenCaptureProject4.mp4")
The model class
class AssignmentEventVideo < ActiveRecord::Base
has_attached_file :video, :styles => {
:medium => { :geometry => "640x480", :format => 'mp4' },
:android => { :geometry => "640x480", :format => 'webm'},
:mobile => { :geometry => "300x300", :format => 'png', :time => 2 },
:thumb => { :geometry => "100x100#", :format => 'png', :time => 2 }
}
validates_attachment_content_type :video, content_type: ['video/mp4']
end
If have tried disabling validation all together with the code below but it still throws the error
do_not_validate_attachment_file_type :video
I have confirmed that the file command is return the correct type with
file -b --mime ScreenCaptureProject3.mp4
which returns
video/mp4; charset=binary
The save is working fine for another model class that accepts images and checks content using
validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
I'm not sure where to turn next - except to recreate the class and change the column name to something that doesn't clash with video?
Hope someone can help!
Thanks katafrakt - you got me on the right path.
I was using a presigned_post and uploading to S3 using JQuery FileUploader. This was not setting the Content-Type and I was getting back a binary/octet type that Paperclip didn't know how to deal with.
I set content_type on the presigned post, which stores the right meta data in S3 and all is well.

custom paperclip validation error message

I have this validation for content type:
validates_attachment_content_type :photo, :content_type => /^image\/(jpg|jpeg|pjpeg|png|x-png|gif)$/, :message => 'file type is not allowed (only jpeg/png/gif images)'
I want only the message above to be displayed but instead it says
Photos photo content type file type is not allowed (only jpeg/png/gif images)
because its a photos model and attached file photo.
thanks
> Not a real solution but a Easy one is to skip paperclip validation and
> write custom one
> validate :check_content_type
>
> def check_content_type
> if !['image/jpeg', 'image/gif','image/png'].include?(self.image_content_type)
> errors.add_to_base("File '#{self.image_file_name}' is not a valid image type") # or errors.add
> end
> end
I'm late to this party.
validates_attachment_size :image, :in => 0.megabytes..2.megabytes, message: " is too large, try less than 2mb or for help"
Gets you:
Should get you closer to home, with an output of:
"Image file size is too large, try less than 2mb"
Hello please d validation paperclip avtar image
attr_accessible :avatar
has_attached_file :avatar, :styles => { :small => "60x60>", :thumb => "60x60>" }
validates_attachment :avatar, :presence => true,
:content_type => { :content_type => "image/jpg" },
:size => { :in => 0..1000.kilobytes }

How to set Paperclip style work only if contenttype is image?

I am using the following:
has_attached_file :file,:styles => { :thumbnail => '320x240!'},:url => "/images/:attachment/:id/:style/:basename.:extension",:path => ":rails_root/public/images/:attachment/:id/:style/:basename.:extension"
validates_attachment_content_type :file, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
To upload both images and video. If I use :style =>{} then image does not upload. I want to use :style method only if content type of file is image.
You can use condition inside of lambda, sorry about ugly formatting:
has_attached_file :file, :styles => lambda
{ |a|
if a.instance.is_image?
{:thumbnail => "320x240!"}
end
}
def is_image?
return false unless asset.content_type
['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg'].include?(asset.content_type)
end
Update 2016:
Most upvoted answer still works, but you need to return an empty hash if it's not of the expected type (eg. a PDF that you don't want to process instead of an image), else you'll run into TypeError - can't dup NilClass issues.
Sample using a ternary for terseness:
has_attached_file :file, :styles => lambda { |a| a.instance.is_image? ? {:thumbnail => "320x240!"} : {} }

Can't resize uploaded image

I'm using paperclip gem for uploading and resizing images. This setup works fine. I'm able to display the uploaded images. The problem comes when I try to resize the uploaded image.
Here is snippet from the model file
has_attached_file :photo,
:size => {:small => "150x150>"}
When I try to upload the image I get this error.
Photo /var/folders/gm/gm-SegRMHuOkSlYtTMkO8U+++TI/-Tmp-/file.jpg is not recognized by the 'identify' command.
I'm sure that the file is jpg. Here is the output of the file command
file.jpg: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJ"
I'm not sure but in our application we do the same thing and it works. Our code looks like this:
has_attached_file :image,
:styles => {:small => "280x173#", :medium => "635x393#"},
:convert_options => {:all => "-quality 80"},#,
:default_style => :medium,
:default_url => "/images/study/nophoto.jpg"
validates_attachment_size :image, :less_than => 10.megabyte
validates_attachment_content_type :image, :content_type => ['image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg']
The difference I see, is that you might have to provide convert_options to be able to resize.
Have you tried any other jpg file, maybe with a simpler path also?

Resources