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

121 lines
3.5 KiB
Ruby
Raw Normal View History

2013-05-11 05:48:22 +00:00
class ApiRecordingsController < ApiController
2013-01-17 01:53:55 +00:00
before_filter :api_signed_in_user
before_filter :look_up_recording, :only => [ :show, :stop, :claim, :keep ]
2013-01-31 18:22:02 +00:00
before_filter :parse_filename, :only => [ :upload_next_part, :upload_sign, :upload_part_complete, :upload_complete ]
2013-01-17 01:53:55 +00:00
respond_to :json
2013-02-05 02:07:49 +00:00
# Returns all files this user should be uploading from his client
def list_uploads
begin
render :json => Recording.list_uploads(current_user, params[:limit], params[:since]), :status => 200
rescue
render :json => { :message => "could not produce list of files" }, :status => 403
end
end
# Returns all files that the user can download
def list_downloads
2013-05-14 05:09:29 +00:00
begin
render :json => Recording.list_downloads(current_user, params[:limit], params[:since]), :status => 200
2013-05-14 05:09:29 +00:00
rescue
render :json => { :message => "could not produce list of files" }, :status => 403
end
2013-02-05 02:07:49 +00:00
end
def show
end
2013-01-31 18:22:02 +00:00
def start
music_session = MusicSession.find(params[:music_session_id])
unless music_session.users.exists?(current_user)
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
end
@recording = Recording.start(music_session, current_user)
if @recording.errors.any?
response.status = :unprocessable_entity
respond_with @recording
else
respond_with @recording, responder: ApiResponder, :location => api_recordings_detail_url(@recording)
2013-01-31 18:22:02 +00:00
end
end
def stop
unless @recording.users.exists?(current_user)
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
end
@recording.stop
if @recording.errors.any?
response.status = :unprocessable_entity
respond_with @recording
else
respond_with @recording, responder: ApiResponder, :location => api_recordings_detail_url(@recording)
2013-05-11 05:48:22 +00:00
end
end
# keep will kick off a mix, as well as create a claimed recording for the creator
2013-05-11 05:48:22 +00:00
def claim
claim = @recording.claim(current_user, params[:name], Genre.find(params[:genre_id]), params[:is_public], params[:is_downloadable])
claim.save
if claim.errors.any?
response.status = :unprocessable_entity
respond_with claim
else
respond_with claim, responder: ApiResponder, :location => api_session_detail_url(claim)
2013-01-31 18:22:02 +00:00
end
end
2013-01-17 01:53:55 +00:00
def upload_next_part
2013-01-31 18:22:02 +00:00
if @recorded_track.next_part_to_upload == 0
2013-05-11 05:48:22 +00:00
if (!params[:length] || !params[:md5])
render :json => { :message => "missing fields" }, :status => 403
end
@recorded_track.upload_start(params[:length], params[:md5])
2013-01-17 01:53:55 +00:00
end
2013-01-31 18:22:02 +00:00
render :json => { :part => @recorded_track.next_part_to_upload }, :status => 200
2013-01-17 01:53:55 +00:00
end
def upload_sign
2013-04-25 06:50:02 +00:00
render :json => @recorded_track.upload_sign(params[:content_md5]), :status => 200
2013-01-17 01:53:55 +00:00
end
def upload_part_complete
begin
2013-01-31 18:22:02 +00:00
@recorded_track.upload_part_complete(params[:part])
2013-01-17 01:53:55 +00:00
rescue
render :json => { :message => "part out of order" }, :status => 403
end
respond_with responder: ApiResponder, :status => 204
end
def upload_complete
2013-01-31 18:22:02 +00:00
@recorded_track.upload_complete
2013-01-17 01:53:55 +00:00
respond_with responder: ApiResponder, :status => 204
end
private
def parse_filename
2013-04-25 06:50:02 +00:00
@recorded_track = RecordedTrack.find_by_upload_filename(params[:filename])
2013-01-31 18:22:02 +00:00
unless @recorded_track
2013-01-17 01:53:55 +00:00
render :json => { :message => RECORDING_NOT_FOUND }, :status => 404
end
end
2013-01-31 18:22:02 +00:00
def look_up_recording
@recording = Recording.find(params[:id])
2013-01-31 18:22:02 +00:00
end
2013-01-17 01:53:55 +00:00
end