Class: Refile::App
- Inherits:
-
Object
- Object
- Refile::App
- Defined in:
- lib/refile/app.rb
Defined Under Namespace
Classes: Proxy
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(logger: nil, allow_origin: nil) ⇒ App
constructor
A new instance of App.
Constructor Details
#initialize(logger: nil, allow_origin: nil) ⇒ App
Returns a new instance of App.
6 7 8 9 10 |
# File 'lib/refile/app.rb', line 6 def initialize(logger: nil, allow_origin: nil) @logger = logger @logger ||= ::Logger.new(nil) @allow_origin = allow_origin end |
Instance Method Details
#call(env) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/refile/app.rb', line 29 def call(env) @logger.info { "Refile: #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}" } backend_name, *args = env["PATH_INFO"].sub(/^\//, "").split("/") backend = Refile.backends[backend_name] if env["REQUEST_METHOD"] == "GET" and backend and args.length >= 2 *process_args, id, filename = args format = ::File.extname(filename)[1..-1] @logger.debug { "Refile: serving #{id.inspect} from #{backend_name} backend which is of type #{backend.class}" } file = backend.get(id) unless process_args.empty? name = process_args.shift processor = Refile.processors[name] unless processor @logger.debug { "Refile: no such processor #{name.inspect}" } return not_found end file = if format processor.call(file, *process_args, format: format) else processor.call(file, *process_args) end end peek = begin file.read(Refile.read_chunk_size) rescue => e log_error(e) return not_found end headers = {} headers["Access-Control-Allow-Origin"] = @allow_origin if @allow_origin [200, headers, Proxy.new(peek, file)] elsif env["REQUEST_METHOD"] == "POST" and backend and args.empty? and Refile.direct_upload.include?(backend_name) @logger.debug { "Refile: uploading to #{backend_name} backend which is of type #{backend.class}" } tempfile = Rack::Request.new(env).params.fetch("file").fetch(:tempfile) file = backend.upload(tempfile) [200, { "Content-Type" => "application/json" }, [{ id: file.id }.to_json]] else not_found end rescue => e log_error(e) [500, {}, ["error"]] end |