Module: Shrine::Plugins::DownloadEndpoint::ClassMethods

Defined in:
lib/shrine/plugins/download_endpoint.rb

Instance Method Summary collapse

Instance Method Details

#download_endpoint(**options) ⇒ Object

Returns the Rack application that retrieves requested files.


19
20
21
22
23
24
25
# File 'lib/shrine/plugins/download_endpoint.rb', line 19

def download_endpoint(**options)
  Shrine::DownloadEndpoint.new(
    shrine_class: self,
    **opts[:download_endpoint],
    **options,
  )
end

#download_response(env, **options) ⇒ Object

Calls the download endpoint passing the request information, and returns the Rack response triple.

It uses a trick where it removes the download path prefix from the path info before calling the Rack app, which is what web framework routers do before they're calling a mounted Rack app.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shrine/plugins/download_endpoint.rb', line 33

def download_response(env, **options)
  script_name = env["SCRIPT_NAME"]
  path_info   = env["PATH_INFO"]

  prefix = opts[:download_endpoint][:prefix]
  match  = path_info.match(/^\/#{prefix}/)

  fail Error, "request path must start with \"/#{prefix}\", but is \"#{path_info}\"" unless match

  begin
    env["SCRIPT_NAME"] += match.to_s
    env["PATH_INFO"]    = match.post_match

    download_endpoint(**options).call(env)
  ensure
    env["SCRIPT_NAME"] = script_name
    env["PATH_INFO"]   = path_info
  end
end