module JamRuby # holds a click track or precount file class JamTrackFile < ActiveRecord::Base include JamRuby::S3ManagerMixin # there should only be one Master per JamTrack, but there can be N Track per JamTrack FILE_TYPE = %w{ClickWav ClickTxt Precount} @@log = Logging.logger[JamTrackFile] before_destroy :delete_s3_files attr_accessible :jam_track_id, :file_type, :filename, as: :admin attr_accessible :url, :md5, :length, as: :admin attr_accessor :original_audio_s3_path, :skip_uploader, :preview_generate_error before_destroy :delete_s3_files validates :file_type, inclusion: {in: FILE_TYPE } belongs_to :jam_track, class_name: "JamRuby::JamTrack" # create storage directory that will house this jam_track, as well as def store_dir "jam_track_files" end def licensor_suffix suffix = '' if jam_track.licensor raise "no licensor name" if jam_track.licensor.name.nil? suffix = " - #{jam_track.licensor.name}" end suffix end # create name of the file def filename(original_name) "#{store_dir}/#{jam_track.original_artist}/#{jam_track.name}#{licensor_suffix}/#{original_name}" end def manually_uploaded_filename if click_wav? filename('click.wav') elsif click_txt? filename('click.txt') elsif precount? filename('precount.wav') else raise 'unknown file type: ' + file_type end end def click_wav? track_type == 'ClickWav' end def click_txt? track_type == 'ClickTxt' end def precount? track_type == 'Precount' end # creates a short-lived URL that has access to the object. # the idea is that this is used when a user who has the rights to this tries to download this JamTrack # we would verify their rights (can_download?), and generates a URL in response to the click so that they can download # but the url is short lived enough so that it wouldn't be easily shared def sign_url(expiration_time = 120) s3_manager.sign_url(self[url], {:expires => expiration_time, :response_content_type => 'audio/wav', :secure => true}) end def can_download?(user) # I think we have to make a special case for 'previews', but maybe that's just up to the controller to not check can_download? jam_track.owners.include?(user) end def delete_s3_files s3_manager.delete(self[:url]) if self[:url] && s3_manager.exists?(self[:url]) end end end