【问题标题】:content-type validation with paperclip fails回形针的内容类型验证失败
【发布时间】:2014-08-02 09:10:53
【问题描述】:

我正在尝试使用回形针上传 .xm 文件,该文件的 mime 类型应为 audio/x-mod。 我通过设置以下选项将回形针配置为允许此配置:

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

附件字段的验证如下所示:

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

每当我尝试上传 mime 类型为 audio/x-mod 回形针的 .xm 文件时,都会收到错误 Song content type is invalid。 当我将有效的内容类型指定为 [/.+/] 时,它可以工作。

我错过了什么吗?这可能是回形针检查内容类型的错误吗?有什么方法可以查看回形针认为文件的内容类型是什么?

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'

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    我认为你的内容类型验证应该是这样的:

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

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

    来自日志content_type="audio/x-xm",对于内容类型验证回形针使用正则表达式,您可以在rubular 中进行检查

    【讨论】:

    • 谢谢,我完全错过了日志中的部分。我假设内容类型必须是audio/x-mod,因为这就是file -b --mime-type 所说的。
    猜你喜欢
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多