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

111 lines
3.2 KiB
Ruby

class ApiRecordingsController < ApiController
before_filter :api_signed_in_user
before_filter :look_up_recording, :only => [ :show, :stop, :claim, :keep ]
before_filter :parse_filename, :only => [ :upload_next_part, :upload_sign, :upload_part_complete, :upload_complete ]
respond_to :json
# Returns all files this user should have synced down to his computer
def list
begin
render :json => Recording.list(current_user), :status => 200
rescue
render :json => { :message => "could not produce list of files" }, :status => 403
end
end
def show
end
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)
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)
end
end
# keep will kick off a mix, as well as create a claimed recording for the creator
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)
end
end
def upload_next_part
if @recorded_track.next_part_to_upload == 0
if (!params[:length] || !params[:md5])
render :json => { :message => "missing fields" }, :status => 403
end
@recorded_track.upload_start(params[:length], params[:md5])
end
render :json => { :part => @recorded_track.next_part_to_upload }, :status => 200
end
def upload_sign
render :json => @recorded_track.upload_sign(params[:content_md5]), :status => 200
end
def upload_part_complete
begin
@recorded_track.upload_part_complete(params[:part])
rescue
render :json => { :message => "part out of order" }, :status => 403
end
respond_with responder: ApiResponder, :status => 204
end
def upload_complete
@recorded_track.upload_complete
respond_with responder: ApiResponder, :status => 204
end
private
def parse_filename
@recorded_track = RecordedTrack.find_by_upload_filename(params[:filename])
unless @recorded_track
render :json => { :message => RECORDING_NOT_FOUND }, :status => 404
end
end
def look_up_recording
@recording = Recording.find(params[:id])
end
end