Created
March 14, 2009 13:13
-
-
Save rob-at-thewebfellas/79062 to your computer and use it in GitHub Desktop.
Getting MP3 information with Paperclip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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