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

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

Instance Method Summary collapse

Instance Method Details

#download_endpointObject

Returns the Rack application that retrieves requested files.



22
23
24
25
26
27
28
# File 'lib/shrine/plugins/download_endpoint.rb', line 22

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

#download_response(env) ⇒ 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.



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

def download_response(env, **)
  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(**).call(env)
  ensure
    env["SCRIPT_NAME"] = script_name
    env["PATH_INFO"]   = path_info
  end
end