jam-cloud/ruby/lib/jam_ruby/models/jam_track_right.rb

52 lines
1.7 KiB
Ruby

module JamRuby
# describes what users have rights to which tracks
class JamTrackRight < ActiveRecord::Base
include JamRuby::S3ManagerMixin
attr_accessible :user, :jam_track, :user_id, :jam_track_id, :url, :md5, :length, :download_count
belongs_to :user, class_name: "JamRuby::User" # the owner, or purchaser of the jam_track
belongs_to :jam_track, class_name: "JamRuby::JamTrack"
validates :user, presence:true
validates :jam_track, presence:true
validate :verify_download_count
validates_uniqueness_of :user_id, scope: :jam_track_id
# Uploads the JKZ:
mount_uploader :url, JamTrackRightUploader
MAX_JAM_TRACK_DOWNLOADS = 1000
def store_dir
"#{jam_track.store_dir}/rights"
end
# create name of the file
def filename
"#{jam_track.name}.jkz"
end
def verify_download_count
if (self.download_count < 0 || self.download_count > MAX_JAM_TRACK_DOWNLOADS) && !@current_user.admin
errors.add(:download_count, "must be less than or equal to #{MAX_JAM_TRACK_DOWNLOADS}")
end
end
# Create user-specific JKZ for the associated jam_track:
def create_jkz
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 => 'jkz', :secure => false})
end
end
end