87 lines
2.6 KiB
Ruby
87 lines
2.6 KiB
Ruby
class ApiJamTracksController < ApiController
|
|
|
|
# have to be signed in currently to see this screen
|
|
before_filter :api_signed_in_user
|
|
before_filter :lookup_jam_track_right, :only => [:download,:enqueue, :show_jam_track_right]
|
|
|
|
respond_to :json
|
|
|
|
def index
|
|
data = JamTrack.index(params, current_user)
|
|
@jam_tracks, @next = data[0], data[1]
|
|
|
|
render "api_jam_tracks/index", :layout => nil
|
|
end
|
|
|
|
def purchased
|
|
params[:show_purchased_only] = true
|
|
data = JamTrack.index(params, current_user)
|
|
@jam_tracks, @next = data[0], data[1]
|
|
|
|
response.headers['total-entries'] = @jam_tracks.total_entries.to_s
|
|
|
|
render "api_jam_tracks/purchased", :layout => nil
|
|
end
|
|
|
|
def downloads
|
|
begin
|
|
render :json => JamTrack.list_downloads(current_user, params[:limit], params[:since], params[:bitrate]), :status => 200
|
|
rescue
|
|
render :json => { :message => "could not produce list of files" }, :status => 403
|
|
end
|
|
end
|
|
|
|
def download
|
|
if @jam_track_right.valid?
|
|
bitrate = params[:bitrate]
|
|
if (@jam_track_right && @jam_track_right.ready?(bitrate))
|
|
@jam_track_right.update_download_count
|
|
@jam_track_right.last_downloaded_at = Time.now
|
|
@jam_track_right.save!
|
|
redirect_to @jam_track_right.sign_url(120, bitrate)
|
|
else
|
|
@jam_track_right.enqueue_if_needed(bitrate)
|
|
render :json => { :message => "not available, digitally signing Jam Track offline." }, :status => 202
|
|
end
|
|
else
|
|
render :json => { :message => "download limit surpassed", :errors=>@jam_track_right.errors }, :status => 403
|
|
end
|
|
end
|
|
|
|
def enqueue
|
|
@jam_track_right.enqueue_if_needed(params[:bitrate])
|
|
render :json => { :message => "enqueued" }, :status => 200
|
|
end
|
|
|
|
def show_jam_track_right
|
|
|
|
end
|
|
|
|
def keys
|
|
jamtrack_holder = params[:jamtracks]
|
|
|
|
unless jamtrack_holder.kind_of?(Hash)
|
|
render :json => {message: 'jamtracks parameter must be an hash'}, :status => 422
|
|
return
|
|
end
|
|
|
|
jamtrack_ids = jamtrack_holder[:tracks]
|
|
|
|
unless jamtrack_ids.kind_of?(Array)
|
|
render :json => {message: 'jamtracks:tracks parameter must be an array'}, :status => 422
|
|
return
|
|
end
|
|
|
|
@jam_tracks = JamTrackRight.list_keys(current_user, jamtrack_ids)
|
|
|
|
render "api_jam_tracks/list_keys", :layout => nil
|
|
end
|
|
|
|
private
|
|
def lookup_jam_track_right
|
|
@jam_track_right = JamTrackRight.where("jam_track_id=? AND user_id=?", params[:id], current_user.id).first
|
|
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @jam_track_right
|
|
end
|
|
|
|
end # class ApiJamTracksController
|