88 lines
2.4 KiB
Ruby
88 lines
2.4 KiB
Ruby
module JamRuby
|
|
class RecordedTrack < ActiveRecord::Base
|
|
|
|
self.table_name = "recorded_tracks"
|
|
|
|
self.primary_key = 'id'
|
|
|
|
SOUND = %w(mono stereo)
|
|
|
|
belongs_to :user, :class_name => "JamRuby::User", :inverse_of => :recorded_tracks
|
|
belongs_to :recording, :class_name => "JamRuby::Recording", :inverse_of => :recorded_tracks
|
|
belongs_to :instrument, :class_name => "JamRuby::Instrument"
|
|
|
|
validates :sound, :inclusion => {:in => SOUND}
|
|
|
|
# Copy an ephemeral track to create a saved one. Some fields are ok with defaults
|
|
def self.create_from_track(track, recording)
|
|
recorded_track = self.new
|
|
recorded_track.recording = recording
|
|
recorded_track.user = track.connection.user
|
|
recorded_track.instrument = track.instrument
|
|
recorded_track.sound = track.sound
|
|
recorded_track.save
|
|
recorded_track
|
|
end
|
|
|
|
def upload_start
|
|
self.upload_id = s3_bucket.objects[filename].multipart_upload.id
|
|
save
|
|
end
|
|
|
|
def upload_sign(filename, content_md5)
|
|
str_to_sign = "PUT\n#{content_md5}\n#{content_type}\n#{http_date_time}\n/#{aws_bucket}/#{filename}"
|
|
signature = Base64.encode64(HMAC::SHA1.digest(aws_secret_key, str_to_sign)).chomp
|
|
{ :signature => signature, :datetime => http_date_time, :upload_id => upload_id }
|
|
end
|
|
|
|
|
|
def upload_part_complete(part)
|
|
raise JamRuby::JamArgumentError unless part == next_part_to_upload
|
|
self.next_part_to_upload = part + 1
|
|
save
|
|
end
|
|
|
|
def upload_complete
|
|
s3_bucket.objects[filename].multipart_uploads[self.upload_id].upload_complete(:remote_parts)
|
|
self.fully_uploaded = true
|
|
save
|
|
end
|
|
|
|
|
|
# Format: "recording_#{saved_track_id}"
|
|
# File extension is irrelevant actually.
|
|
def self.filename_to_recorded_track_id(filename)
|
|
matches = /^recording_([\w-]+)$/.match(filename)
|
|
return nil unless matches && matches.length > 1
|
|
matches[1]
|
|
end
|
|
|
|
private
|
|
|
|
def s3_bucket
|
|
@s3 ||= AWS::S3.new
|
|
@s3.buckets[aws_bucket]
|
|
end
|
|
|
|
def filename
|
|
"recording_#{self.id}"
|
|
end
|
|
|
|
def aws_bucket
|
|
"jamkazam-dev"
|
|
end
|
|
|
|
def aws_secret_key
|
|
"XLq2mpJHNyA0bN7GBSdYyF/pWjfzGkDx92b1C+Wv"
|
|
end
|
|
|
|
def content_type
|
|
"application/octet-stream"
|
|
end
|
|
|
|
def http_date_time
|
|
Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
|
|
end
|
|
end
|
|
end
|