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

157 lines
4.8 KiB
Ruby

class ApiRecordingsController < ApiController
before_filter :api_signed_in_user
before_filter :look_up_recording, :only => [ :show, :stop, :claim, :discard, :keep ]
before_filter :parse_filename, :only => [ :download, :upload_next_part, :upload_sign, :upload_part_complete, :upload_complete ]
respond_to :json
@@log = Logging.logger[ApiRecordingsController]
# Returns all files this user should be uploading from his client
def list_uploads
begin
result = Recording.list_uploads(current_user, params[:limit], params[:since])
puts result.inspect
render :json => result, :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
begin
render :json => Recording.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 show
end
def download
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @recorded_track.can_download?(current_user)
redirect_to @recorded_track.sign_url
end
def start
music_session = MusicSession.find(params[:music_session_id])
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless music_session.users.exists?(current_user)
@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
@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
# claim will create a claimed recording for the creator
def claim
claim = @recording.claim(current_user, params[:name], params[:description], Genre.find_by_id(params[:genre]), 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
# discard will tell the server the user has no interest in the recording
def discard
@recorded_tracks = @recording.recorded_tracks_for_user(current_user)
RecordedTrack.update(@recorded_tracks, :discard => true)
render :json => {}, :status => 200
end
def upload_next_part
length = params[:length]
md5 = params[:md5]
@recorded_track.upload_next_part(length, md5)
if @recorded_track.errors.any?
response.status = :unprocessable_entity
# this is not typical, but please don't change this line unless you are sure it won't break anything
# this is needed because after_rollback in the RecordedTrackObserver touches the model and something about it's
# state doesn't cause errors to shoot out like normal.
render :json => { :errors => @recorded_track.errors }, :status => 422
else
result = {
:part => @recorded_track.next_part_to_upload,
:offset => @recorded_track.file_offset.to_s
}
render :json => result, :status => 200
end
end
def upload_sign
render :json => @recorded_track.upload_sign(params[:md5]), :status => 200
end
def upload_part_complete
part = params[:part]
offset = params[:offset]
@recorded_track.upload_part_complete(part, offset)
if @recorded_track.errors.any?
response.status = :unprocessable_entity
respond_with @recorded_track
else
render :json => {}, :status => 200
end
end
def upload_complete
@recorded_track.upload_complete
@recorded_track.recording.upload_complete
if @recorded_track.errors.any?
response.status = :unprocessable_entity
respond_with @recorded_track
return
else
render :json => {}, :status => 200
end
end
private
def parse_filename
@recorded_track = RecordedTrack.find_by_recording_id_and_client_track_id!(params[:id], params[:track_id])
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @recorded_track.recording.has_access?(current_user)
end
def look_up_recording
@recording = Recording.find(params[:id])
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR unless @recording.has_access?(current_user)
end
end