content-type validation with paperclip fails

戏子无情 提交于 2019-12-23 04:37:10

问题


I'm trying to use paperclip to upload .xm files which should have the mime type audio/x-mod. I configured paperclip to allow this configuration by setting the following options:

Paperclip.options[:content_type_mappings] = {
    xm: "audio/x-mod"
}

The validation for the attachment field looks like this:

validates_attachment :song, presence: true,
  content_type: { content_type: ["audio/x-mod"] },
  size: { in: 0..128.kilobytes }

Whenever I try to upload a .xm file that has the mime type audio/x-mod paperclip, I get the error Song content type is invalid. It works when I specify the valid content types as [/.+/].

Have I missed anything? Is this maybe a bug in how paperclip checks the content type? Is there some way to see what paperclip thinks the content type of the file is?

Started POST "/mods" for 127.0.0.1 at 2014-08-02 11:28:56 +0200
Processing by ModsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FhBvyd8jOapcjONk8kyOgGE/oOZPA+sDBJxr/w3zUG0=", "mod"=>{"title"=>"Girl Next Door", "release(1i)"=>"2014", "release(2i)"=>"8", "release(3i)"=>"2", "song"=>#<ActionDispatch::Http::UploadedFile:0x00000000e763c8 @tempfile=#<Tempfile:/tmp/RackMultipart20140802-1742-wujc7n>, @original_filename="Wiklund_-_Girl_next_door.xm", @content_type="audio/x-xm", @headers="Content-Disposition: form-data; name=\"mod[song]\"; filename=\"Wiklund_-_Girl_next_door.xm\"\r\nContent-Type: audio/x-xm\r\n">}, "commit"=>"Save"}
Command :: file -b --mime '/tmp/d437374435a48a211b1f7b9e585c4c2d20140802-1742-1259ddt.xm'
   (0.1ms)  begin transaction
Command :: file -b --mime '/tmp/d437374435a48a211b1f7b9e585c4c2d20140802-1742-1e5e5sn.xm'

回答1:


I think you content type validation should looks like this:

validates_attachment_content_type : song, :content_type => /\Aaudio/

or

validates_attachment_content_type :song, :content_type => /^audio\/(x-xm)/

From log content_type="audio/x-xm", for content type validation Paperclip use regexp you can check this in rubular



来源:https://stackoverflow.com/questions/25093473/content-type-validation-with-paperclip-fails

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