class ApiRecordingsController < ApiController before_filter :api_signed_in_user, :except => [ :add_like ] 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] def index # lists recordings created by for the current user @recordings = Recording.list_recordings(current_user, params[:created_by]) end # 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]) 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) @recorded_track.current_user = current_user @recorded_track.update_download_count @recorded_track.valid? if !@recorded_track.errors.any? @recorded_track.save! redirect_to @recorded_track.sign_url else render :json => { :message => "download limit surpassed" }, :status => 404 end end def start music_session = ActiveMusicSession.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]) 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 add_comment if params[:id].blank? render :json => { :message => "Recording ID is required" }, :status => 400 return end if params[:user_id].blank? render :json => { :message => "User ID is required" }, :status => 400 return end if params[:comment].blank? render :json => { :message => "Comment is required" }, :status => 400 return end comment = RecordingComment.new comment.recording_id = params[:id] comment.creator_id = params[:user_id] comment.comment = @@html_encoder.encode(params[:comment]) comment.ip_address = request.remote_ip comment.save if comment.errors.any? render :json => { :message => "Unexpected error occurred" }, :status => 500 return else render :json => {}, :status => 201 return end end def add_like if params[:id].blank? render :json => { :message => "Recording ID is required" }, :status => 400 return end liker = RecordingLiker.new liker.recording_id = params[:id] liker.liker_id = params[:user_id] liker.claimed_recording_id = params[:claimed_recording_id] liker.favorite = true liker.ip_address = request.remote_ip liker.save if liker.errors.any? render :json => { :message => "Unexpected error occurred" }, :status => 500 return else render :json => {}, :status => 201 return end end # discard will tell the server the user has no interest in the recording def discard @recording.discard(current_user) 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