Sha256: 9df97edd121e517196e63e5662e151b59d7a42b49a68e9e483248e34c86276cc

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

module OngakuRyohoServer
  #
  # { Application }
  #
  # === Info
  #
  # Sinatra application to handle the incoming
  # http requests or whatever

  class Application < Sinatra::Base
    set :environment, :production

    # CORS
    register Sinatra::CrossOrigin

    configure do
      enable :cross_origin
    end

    # allowed file formats
    FILE_FORMATS = %w{ mp3 mp4 m4a ogg flac wav wma }

    # root
    get "/" do
      callback = params[:callback]
      json = OngakuRyohoServer::List.get

      if callback and !callback.empty?
        content_type :js
        response = "#{callback}(#{json})"
      else
        content_type :json
        response = json
      end

      response
    end


    # compare filelists
    post "/check" do
      callback = params[:callback]
      file_list = params[:file_list]
      other_file_list = params[:other_file_list]

      missing_and_new = OngakuRyohoServer::Process.check_files(file_list, other_file_list)
      json = Oj.dump(missing_and_new)

      if callback and !callback.empty?
        content_type :js
        response = "#{callback}(#{json})"
      else
        content_type :json
        response = json
      end

      response
    end


    # music file
    get %r{.(#{FILE_FORMATS.join("|")})$}i do
      requested_item = URI.unescape(request.path_info[1..-1])
      File.exists?(requested_item) ? send_file(requested_item) : 404
    end


    # everything else
    get %r{.+} do
      403
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ongaku_ryoho_server-0.5.3 lib/ongaku_ryoho_server/application.rb
ongaku_ryoho_server-0.5.2 lib/ongaku_ryoho_server/application.rb
ongaku_ryoho_server-0.5.1 lib/ongaku_ryoho_server/application.rb