Skip to content

Instantly share code, notes, and snippets.

@rob-at-thewebfellas
Created March 14, 2009 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rob-at-thewebfellas/79062 to your computer and use it in GitHub Desktop.
Save rob-at-thewebfellas/79062 to your computer and use it in GitHub Desktop.
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