Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip validates_attachment_content_type for mp3 triggered when attaching mp3

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.

like image 482
Pete Avatar asked Nov 17 '09 20:11

Pete


4 Answers

Wrong content type? Try audio/mpeg.

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

like image 141
Alex Polkhovsky Avatar answered Nov 18 '22 03:11

Alex Polkhovsky


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

like image 23
Pete Avatar answered Nov 18 '22 01:11

Pete


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' ]
like image 3
Sandro L Avatar answered Nov 18 '22 02:11

Sandro L


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.

like image 1
Filip Avatar answered Nov 18 '22 02:11

Filip