Paperclip::NotIdentifiedByImageMagickError when file is not a valid attachment content type

≡放荡痞女 提交于 2019-12-13 16:50:23

问题


I have systematically an error when I'm trying to upload a file that is not in ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"]

When I try to upload a file like a 'wav' I have this message

* Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav is not recognized by the 'identify' command. * Photo /var/folders/nT/nTr21TWfFRO7r3cyG-h-7++++TM/-Tmp-/Clip audio 01,39154,0.wav is not recognized by the 'identify' command. * Photo content type Accepted files include: jpg, gif, png

So it detects that the file is not an image and display my message "Accepted files include: jpg, gif, png" but I have this extra message included before mine Photo is not recognized by the 'identify' command... Upload works fine for pictures

My code is:

Controller:

def upload  
  @picture= Picture.new(params[:picture])  
    if !@picture.valid?  
        render  :form  
    end  
end  

View form:

<%= error_messages_for :picture, :header_message => nil, :message => nil %>  
<% form_for :picture, @picture, :name => "uploadPic", :url => { :action => 'upload_data'}, :html => {:name => 'uploadForm', :multipart => true } do |form| %>  
    <%= form.file_field :photo %>  
    <%= submit_tag 'Save'%>  
<% end %>

Picture model:

 class Picture < ActiveRecord::Base    
    require 'paperclip'  
    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }  

    validates_attachment_size :photo, :less_than => 2.megabytes , :message => "must be less than 2 megabytes"  
    validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png", "image/pjpeg", "image/x-png"], :message => "Accepted files include: jpg, gif, png"   

 end

回答1:


solved it with :whiny => false
has_attached_file :photo, :whiny => false, :styles => { :medium => "300x300>", :thumb => "100x100>" }




回答2:


:whiny => false was not enough to solve the problem with the latest version of paperclip (2.3.6). I ended up doing this in a rails initializer:

module Paperclip
  class Attachment
    alias original_assign assign
    def assign(*args)
      original_assign(*args)
    rescue NotIdentifiedByImageMagickError => e
    end
  end
end

It seems okay to swallow that exception because validation errors get added anyway at least if you use :whiny => true.



来源:https://stackoverflow.com/questions/2111716/paperclipnotidentifiedbyimagemagickerror-when-file-is-not-a-valid-attachment-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!