Revisions

gist: 79062 Download_button fork
public
Description:
Getting MP3 information with Paperclip
Public Clone URL: git://gist.github.com/79062.git
Embed All Files: show embed
track.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
tracks_controller.rb #
1
2
3
4
5
6
7
8
9
10
11
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