CaptainCarl
CaptainCarl

Reputation: 3489

Paperclip's content_type is invalid according to app, but isn't

I'm new to Paperclip and it all seemed to work pretty fast. I'm trying to get the user to only upload PNG's or JPG's though, and although I'm uploading a JPG and my content_type validates for JPG it says it's invalid nonetheless.

I've tried removing the PNG content_type, to no avail.

I've tried using has_attached_file aswell, but it seems to ignore the :content_type and stuff. Because if I upload a JPG with :content_type => "image/png" only; It doesn t give an error.

Any suggestions?

    validates_attachment :avatar, :styles => { 
                                    :medium => "300x300", 
                                    :thumb => "100x100" 
                                }, :content_type => { 
                                    :content_type => "image/jpg", 
                                    :content_type => "image/png"
                                },
                                :size => { :in => 0..1.megabytes }

Oh, and while I'm at it; I want to get my thumb and medium to the fixed width. So no scaling like 100x80 but just 100x100 either way. How can I do that?

Upvotes: 4

Views: 5355

Answers (3)

Hoa Hoang
Hoa Hoang

Reputation: 1242

I compared the log from server, and noticed the different on content-type header. It should be 'image/jpeg'

Parameters: {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x0055b7d4c30870 @tempfile=#<Tempfile:/tmp/RackMultipart20170510-21515-11ld4ji.jpg>, @original_filename="IMAG0303.jpg", @content_type="multipart/form-data", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"IMAG0303.jpg\"\r\nContent-Type: multipart/form-data\r\nContent-Length: 1057779\r\n">}

Parameters: {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x0055b7d3ec3408 @tempfile=#<Tempfile:/tmp/RackMultipart20170510-21515-1l0x8sw.jpg>, @original_filename="2017.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"photo_10/05/2017.jpg\"\r\nContent-Type: image/jpeg\r\n">}

Upvotes: 0

upisdown
upisdown

Reputation: 309

I ran into the same issue tonight, and it turned out I needed image/jpeg and image/jpg

 validates_attachment :avatar, :styles => { 
                                    :medium => "300x300", 
                                    :thumb => "100x100" 
                                }, :content_type => { 
                                    :content_type => "image/jpg",
                                    :content_type => "image/jpeg", 
                                    :content_type => "image/png"
                                },
                                :size => { :in => 0..1.megabytes }

It worked when just trying /^image/ but I like being specific.

Upvotes: 3

Richard Peck
Richard Peck

Reputation: 76774

It seems you're trying to use some Paperclip validation, which you could look at here: Paperclip - Validate File Type but not Presence

According to that answer, you could use this:

validates_attachment_content_type :sound, :content_type => ['audio/mp3', 'application/x-mp3'], :if => :sound_attached?

An alternative is to use lambda to check to see whether you're dealing with a specific content type. Here's an example from one of our live apps:

   has_attached_file :attachment,
            styles:          lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"}  : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}}

    def is_image?
            attachment.instance.attachment_content_type =~ %r(image)
    end

Lambda is only really a way to gauge whether the content-type is what you need (I.E you only allow JPG images). In order to validate the presence of an image (rather than a video), you'd need to validates_attachment_content_type

Upvotes: 5

Related Questions