Paperclip在附加mp3时触发validates_attachment_content_type验证。

8

当我在使用Paperclip为我的语音模型添加以下验证时,我遇到了困难。当我尝试上传MP3文件时,它会被触发:

class Voice < ActiveRecord::Base
  has_attached_file :clip

  validates_attachment_presence :clip
  validates_attachment_content_type :clip, :content_type => [ 'application/mp3', 'application/x-mp3', 'audio/mpeg', 'audio/mp3' ],
                                    :message => 'file must be of filetype .mp3'

  validates_attachment_size :clip, :less_than => 10.megabytes                                    

  validates_presence_of :title      
end

我尝试了多个不同的mp3文件,但是它们似乎都无法上传,因为验证失败。

5个回答

5

我最初允许的内容类型数组如下:['application/mp3','application/x-mp3','audio/mpeg','audio/mp3']然而保存到数据库的内容类型却是 'audio/mpg',这让我感到困惑。我的 mp3 文件有问题还是其他奇怪的事情发生了?这似乎很奇怪。 - Pete

4

抱歉,我只是闲扯了一下。

我只是删除了验证,查看数据库中保存的 content_type ('audio/mpg') 并将其添加到验证允许的 content_type 数组中。

工作完成 :-)


你好。我正在尝试使用音频/wav类型,但它无法工作。你能帮我吗?谢谢 :) - Vishal

3
为了实现(希望是)完整的mp3支持,我使用了以下mime类型:
validates_attachment_content_type :audio,
  :content_type => [ 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]

1

是的,但如果用户使用其他浏览器(或浏览器的其他版本),mp3的内容类型可能会被意外地解释,他将无法保存mp3。


我同意,我认为这将是建立一个内容类型数组的情况,这些类型都对MP3有效,以涵盖所有基础。 - Pete

0

所以,有趣的是,今晚我遇到了这个问题,上面提供的解决方案都对我无效。我一直在收到这个错误:

`[paperclip] Content Type Spoof: Filename blah_blah_blah.mp3 (audio/mp3 from Headers, ["audio/mpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.`

我通过使用以下内容作为我的验证器解决了这个问题:

validates_attachment_content_type :recording,
content_type: [
  'application/mp3',
  'application/x-mp3',
  'audio/mpeg',
  ['audio/mpeg'], # note the array around the type
  'audio/mp3'
],
message: 'File must be of filetype .mp3'

希望这能帮助到某人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接