Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting MP3 information with Paperclip
class Track < ActiveRecord::Base
#
# All the usual stuff
#
before_save :update_mp3_info
#
# More stuff
#
protected
def update_mp3_info
if mp3?
if mp3.dirty?
Mp3Info.open(mp3.to_file.path) do |mp3info|
self.length = mp3info.length
%w(title artist album year genre).each { |attr| send("#{attr}=", mp3info.tag.send(attr)) }
end
end
else
self.length = 0
%w(title artist album year genre).each { |attr| send("#{attr}=", '') }
end
true
rescue Mp3InfoError => e
errors.add(:mp3, "unable to process file (#{e.message})")
false
end
end
def create
@track = logged_in_user.tracks.build(params[:track])
if @track.save
flash[:notice] = "Uploaded."
redirect_to :action => :index
else
flash[:error] = "Ohhh, something went wrong. Try it again."
render :action => :new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment