require 'aws-sdk'

class ApiMusicNotationsController < ApiController
  before_filter :api_signed_in_user

  respond_to :json

  def create
    client_id = params[:client_id]

    if client_id.nil?
      raise JamArgumentError, "client_id must be specified"
    end

    @music_notations = []

    params[:files].each do |file|
      music_notation = MusicNotation.create(params[:session_id], file, current_user)
      @music_notations.push music_notation
    end if params[:files]

    respond_with @music_notations, responder: ApiResponder, :status => 201
  end

  def download
    @music_notation = MusicNotation.find(params[:id])

    unless @music_notation.music_session.nil? || @music_notation.music_session.can_join?(current_user, true)
      render :text => "Permission denied", status:403
      return
    end
    if '_blank'==params[:target]
      redirect_to @music_notation.sign_url
    else
      render :text => @music_notation.sign_url
    end
  end
end
