class Shrine::PresignEndpoint
Rack application that accepts GET request to the root URL, calls ‘#presign` on the specified storage, and returns that information in JSON format.
Constants
- CONTENT_TYPE_JSON
- CONTENT_TYPE_TEXT
Public Class Methods
Writes given options to instance variables.
# File lib/shrine/plugins/presign_endpoint.rb, line 67 def initialize(options) options.each do |name, value| instance_variable_set("@#{name}", value) end end
Public Instance Methods
Accepts a Rack env hash, routes GET requests to the root URL, and returns a Rack response triple.
If request isn’t to the root URL, a ‘404 Not Found` response is returned. If request verb isn’t GET, a ‘405 Method Not Allowed` response is returned.
# File lib/shrine/plugins/presign_endpoint.rb, line 79 def call(env) request = Rack::Request.new(env) status, headers, body = catch(:halt) do error!(404, "Not Found") unless ["", "/"].include?(request.path_info) if request.get? handle_request(request) elsif request.options? handle_options_request(request) else error!(405, "Method Not Allowed") end end headers = Rack::Headers[headers] if Rack.release >= "3" headers["Content-Length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s : body.map(&:bytesize).inject(0, :+).to_s [status, headers, body] end
# File lib/shrine/plugins/presign_endpoint.rb, line 101 def inspect "#<#{@shrine_class}::PresignEndpoint(:#{@storage_key})>" end
Private Instance Methods
Used for early returning an error response.
# File lib/shrine/plugins/presign_endpoint.rb, line 177 def error!(status, message) throw :halt, [status, { "Content-Type" => CONTENT_TYPE_TEXT }, [message]] end
Calls ‘#presign` on the storage, and returns the `url`, `fields`, and `headers` information in a serialializable format. If `:presign` option is given, calls that instead of calling `#presign`.
# File lib/shrine/plugins/presign_endpoint.rb, line 148 def generate_presign(location, options, request) if @presign data = @presign.call(location, options, request) else data = storage.presign(location, **options) end { fields: {}, headers: {} }.merge(data.to_h) end
Generates the location using ‘Shrine#generate_uid`, and extracts the extension from the `filename` query parameter. If `:presign_location` option is given, calls that instead.
# File lib/shrine/plugins/presign_endpoint.rb, line 129 def get_presign_location(request) if @presign_location @presign_location.call(request) else extension = File.extname(request.params["filename"].to_s) uploader.send(:generate_uid, nil) + extension end end
Calls ‘:presign_options` option block if given.
# File lib/shrine/plugins/presign_endpoint.rb, line 139 def get_presign_options(request) options = @presign_options options = options.call(request) if options.respond_to?(:call) options || {} end
Uppy client sends an OPTIONS request to fetch information about the Uppy Companion. Since our Rack app is only acting as Uppy Companion, we just return a successful response.
# File lib/shrine/plugins/presign_endpoint.rb, line 122 def handle_options_request(request) [200, {}, []] end
Accepts a ‘Rack::Request` object, generates the presign, and returns a Rack response.
# File lib/shrine/plugins/presign_endpoint.rb, line 110 def handle_request(request) location = get_presign_location(request) options = get_presign_options(request) presign = generate_presign(location, options, request) make_response(presign, request) end
Transforms the presign hash into a JSON response. It returns a Rack response triple - an array consisting of a status number, hash of headers, and a body enumerable. If ‘:rack_response` option is given, calls that instead.
# File lib/shrine/plugins/presign_endpoint.rb, line 162 def make_response(object, request) status, headers, body = if @rack_response @rack_response.call(object, request) else [200, { "Content-Type" => CONTENT_TYPE_JSON }, [object.to_json]] end headers = Rack::Headers[headers] if Rack.release >= "3" # prevent browsers from caching the response headers["Cache-Control"] = "no-store" unless headers.key?("Cache-Control") [status, headers, body] end
Returns the storage object.
# File lib/shrine/plugins/presign_endpoint.rb, line 187 def storage @shrine_class.find_storage(@storage_key) end
Returns the uploader around the specified storage.
# File lib/shrine/plugins/presign_endpoint.rb, line 182 def uploader @shrine_class.new(@storage_key) end