module Shrine::Plugins::DownloadEndpoint::ClassMethods

  1. lib/shrine/plugins/download_endpoint.rb

Methods

Public Instance

  1. download_endpoint
  2. download_response

Public Instance methods

download_endpoint(**options)

Returns the Rack application that retrieves requested files.

[show source]
   # File lib/shrine/plugins/download_endpoint.rb
19 def download_endpoint(**options)
20   Shrine::DownloadEndpoint.new(
21     shrine_class: self,
22     **opts[:download_endpoint],
23     **options,
24   )
25 end
download_response(env, **options)

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.

[show source]
   # File lib/shrine/plugins/download_endpoint.rb
33 def download_response(env, **options)
34   script_name = env["SCRIPT_NAME"]
35   path_info   = env["PATH_INFO"]
36 
37   prefix = opts[:download_endpoint][:prefix]
38   match  = path_info.match(/^\/#{prefix}/)
39 
40   fail Error, "request path must start with \"/#{prefix}\", but is \"#{path_info}\"" unless match
41 
42   begin
43     env["SCRIPT_NAME"] += match.to_s
44     env["PATH_INFO"]    = match.post_match
45 
46     download_endpoint(**options).call(env)
47   ensure
48     env["SCRIPT_NAME"] = script_name
49     env["PATH_INFO"]   = path_info
50   end
51 end