Paperclip validates_attachment_content_type for mp3 triggered when attaching mp3
Asked Answered
M

5

8

Struggling to workout when i add 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.

Miscall answered 17/11, 2009 at 20:27 Comment(0)
S
5

Wrong content type? Try audio/mpeg.

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

Sitnik answered 17/11, 2009 at 20:35 Comment(1)
Well the array of content types i went with initially to allow was: [ 'application/mp3', 'application/x-mp3', 'audio/mpeg', 'audio/mp3' ], However the content type getting saved to the database was 'audio/mpg' which i thought was odd. So is there something dodgy about my mp3's or something else weird going on? This does seem odd.Miscall
M
4

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 :-)

Miscall answered 17/11, 2009 at 20:30 Comment(1)
Hello. I am trying for audio/wav type. But it doesn't work. Could you please help me? Thanks :)Instal
T
3

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' ]
Taranto answered 25/10, 2011 at 13:53 Comment(0)
O
1

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.

Outshine answered 7/5, 2010 at 13:45 Comment(1)
I agree, i think it's going to be a case of building up an array of content_types that are all valid for an mp3 to cover all bases.Miscall
E
0

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.

Electrode answered 28/4, 2016 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.