jam-cloud/web/app/controllers/api_jam_tracks_controller.rb

59 lines
1.8 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
respond_to :json
def index
data = JamTrack.index(params)
@jam_tracks, @next = data[0], data[1]
render "api_jam_tracks/index", :layout => nil
end
def downloads
begin
render :json => JamTrack.list_downloads(current_user, params[:limit], params[:since]), :status => 200
rescue
render :json => { :message => "could not produce list of files" }, :status => 403
end
end
def download
if @jam_track_right.valid?
if (@jam_track_right && @jam_track_right.signed && @jam_track_right.url.present? &&@jam_track_right.url.file.exists?)
@jam_track_right.update_download_count
@jam_track_right.save!
redirect_to @jam_track_right.sign_url
else
@jam_track_right.enqueue
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 keys
jamtrack_ids = params[:jamtracks]
unless jamtrack_ids.kind_of?(Array)
render :json => {message: 'jamtracks parameter must be an array'}, :status => 200
return
end
@jam_tracks = JamTrackRight.list_keys(current_user, params[:jamtracks])
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