Class: Sinicum::Imaging::ImagingMiddleware

Inherits:
Rack::File
  • Object
show all
Defined in:
lib/sinicum/imaging/imaging_middleware.rb

Overview

Public: Rack middleware to handle a request to the imaging fuctionality.

Constant Summary collapse

SUFFIX_REGEX =
/\.\w+$/
DEFAULT_CACHE_TIME =
24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ImagingMiddleware

Returns a new instance of ImagingMiddleware.



8
9
10
# File 'lib/sinicum/imaging/imaging_middleware.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#_call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sinicum/imaging/imaging_middleware.rb', line 20

def _call(env)
  unless ALLOWED_VERBS.include? env["REQUEST_METHOD"]
    return fail(405, "Method Not Allowed")
  end
  request = Rack::Request.new(env)
  imaging_file = ImagingFile.new(request.path_info)
  if imaging_file.result?
    @path = imaging_file.path
    available = begin
                  F.file?(@path) && F.readable?(@path)
                rescue SystemCallError
                  false
                end
  end
  if available
    @headers = {}
    @headers["Content-Disposition"] = "inline; filename=\"#{imaging_file.filename}\""
    if Rails.configuration.action_controller.perform_caching
      @headers["Cache-Control"] = "max-age=#{imaging_file.cache_time}, public"
    end
    serving(env)
  else
    fail(404, "File not found: #{request.path_info}")
  end
end

#call(env) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/sinicum/imaging/imaging_middleware.rb', line 12

def call(env)
  if on_imaging_path?(env['PATH_INFO'])
    dup._call(env)
  else
    @app.call(env)
  end
end