问题描述
我正在尝试使用回形针上传应具有 MIME 类型 audio/x-mod
的 .xm 文件.我通过设置以下选项将回形针配置为允许此配置:
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 }
每当我尝试上传具有 MIME 类型 audio/x-mod
回形针的 .xm 文件时,我都会收到错误 歌曲内容类型无效
.当我将有效的内容类型指定为 [/.+/]
时,它会起作用.
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'
推荐答案
我认为您的内容类型验证应该如下所示:
I think you content type validation should looks like this:
validates_attachment_content_type : song, :content_type => /\Aaudio/
或
validates_attachment_content_type :song, :content_type => /^audio\/(x-xm)/
从日志 content_type="audio/x-xm"
,对于内容类型验证 Paperclip 使用正则表达式,您可以在 rubular
From log content_type="audio/x-xm"
, for content type validation Paperclip use regexp you can check this in rubular
这篇关于使用回形针进行内容类型验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!