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(**)

Returns the Rack application that retrieves requested files.

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

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
36 def download_response(env, **)
37   script_name = env["SCRIPT_NAME"]
38   path_info   = env["PATH_INFO"]
39 
40   prefix = opts[:download_endpoint][:prefix]
41   match  = path_info.match(/^\/#{prefix}/)
42 
43   fail Error, "request path must start with \"/#{prefix}\", but is \"#{path_info}\"" unless match
44 
45   begin
46     env["SCRIPT_NAME"] += match.to_s
47     env["PATH_INFO"]    = match.post_match
48 
49     download_endpoint(**).call(env)
50   ensure
51     env["SCRIPT_NAME"] = script_name
52     env["PATH_INFO"]   = path_info
53   end
54 end