开发者

Paperclip validates_attachment_content_type for mp3 triggered when attaching mp3

Struggling to workout when i add 开发者_Python百科the following validtion to my Voice model using paperclip, it is being triggered when i try and upload an 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

I have tried a number of different mp3 files but none of them seem to upload because the validation is failing.


Wrong content type? Try audio/mpeg.

http://www.w3schools.com/media/media_mimeref.asp


Just being silly, sorry.

I simply removed the validation, viewed in the db what the content_type was being saved as ('audio/mpg') and added it to the aray of allowed content_types in the validation.

Job done :-)


For an (hopefully) complete mp3-support I used the following mimetypes:

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' ]


Yes, but If a user has other browser (or other version of browser) mp3's content type could be interpreted in unexpected way and he will not have the ability to save mp3.


So, oddly enough, I was having this problem tonight, and none of the above solutions were working for me. I was getting this error:

`[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.`

I solved it by using this as my validator:

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'

Hopefully this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜