Search code examples
ruby-on-railspaperclippaperclip-validation

Rails paperclip attachment validation failure with .ogg format


this is my first post on stackoverflow. I'm developing a rails app which require a song model that has an .ogg audio file attached by paperclip. I'm using .ogg format due to some browsers audio formats restrictions.

Unfortunately, when creating a new song object, it seems to fail all format validations, preventing the uploading of the audio file.

I've tried several format descriptions, such as 'audio/ogg', 'audio,oga', video/ogg'... But none seems to work. Uploading .mp3 files works fine, but I need to use .ogg due to reasons explained above.

I use paperclip to upload image files in other models of the app and it works fine, so it seems that I'm missing something... Thanks in advance for all your help!

The model, song.rb:

class Song < ActiveRecord::Base
  attr_accessible :name, :lyrics, :track_order, :music_file, :url

  has_attached_file :music_file, dependent: :destroy

  validates_presence_of :name, :lyrics, :track_order, :record_id
  validates_attachment_presence :music_file
  validates_attachment_content_type :music_file, :content_type => ['audio/ogg', 'video/ogg']

  belongs_to :record

  default_scope order('track_order ASC')

  before_validation :set_file_url

  private

  def set_file_url
    self.url = music_file.url
  end

end

The error I get:

ArgumentError (uncaught throw #<ActiveModel::Errors:0x0000000560df60 @base=#<Song id: 12, name: "Las Horas", lyrics: "Letra aquí", track_order: 1, record_id: 2, created_at: "2014-09-30 14:06:05", updated_at: "2014-09-30 15:27:25", music_file_file_name: "lshoras.ogg", music_file_content_type: "video/ogg", music_file_file_size: 3220214, music_file_updated_at: "2014-10-01 08:59:56", url: "/system/songs/music_files/000/000/012/original/lsho...">, @messages={:music_file=>["has an extension that does not match its contents"], :name=>[], :lyrics=>[], :track_order=>[]}>):
  app/controllers/songs_controller.rb:33:in `throw'
  app/controllers/songs_controller.rb:33:in `update'

Solution

  • Seems that your ogg file has a wrong mime, checking this, you can see that ogg files should have audio/ogg but your's seem to have video/ogg (ogv). You can check it with:

    file -b --mime lshoras.ogg
    

    If this doesn't solve your problem you can check this to disable or modify the validation:

    Paperclip.options[:content_type_mappings] = { ogg: 'application/ogg' }