class Shrine::DerivationEndpoint

  1. lib/shrine/plugins/derivation_endpoint.rb
Superclass: Object

Methods

Public Class

  1. new

Public Instance

  1. call
  2. handle_request
  3. inspect
  4. options
  5. shrine_class

Public Instance Aliases

to_s -> inspect

Attributes

Public Class methods

new(shrine_class:, options: {})
[show source]
    # File lib/shrine/plugins/derivation_endpoint.rb
354 def initialize(shrine_class:, options: {})
355   @shrine_class = shrine_class
356   @options      = options
357 end

Public Instance methods

call(env)
[show source]
    # File lib/shrine/plugins/derivation_endpoint.rb
359 def call(env)
360   request = Rack::Request.new(env)
361 
362   status, headers, body = catch(:halt) do
363     error!(405, "Method not allowed") unless request.get? || request.head?
364 
365     handle_request(request)
366   end
367 
368   headers = Rack::Headers[headers] if Rack.release >= "3"
369   headers["Content-Length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s :
370                                                               body.map(&:bytesize).inject(0, :+).to_s
371 
372   [status, headers, body]
373 end
handle_request(request)

Verifies validity of the URL, then extracts parameters from it (such as derivation name, arguments and source file), and generates a derivation response.

Returns “403 Forbidden” if signature is invalid, or if the URL has expired.

Returns “404 Not Found” if derivation block is not defined, or if source file was not found on the storage.

[show source]
    # File lib/shrine/plugins/derivation_endpoint.rb
384 def handle_request(request)
385   verify_signature!(request) if secret_key
386   check_expiry!(request)
387 
388   name, *args, serialized_file = request.path_info.split("/")[1..-1]
389 
390   name          = name.to_sym
391   uploaded_file = shrine_class::UploadedFile.urlsafe_load(serialized_file)
392 
393   # request params override statically configured options
394   options = self.options.dup
395   options[:type]        = request.params["type"]        if request.params["type"]
396   options[:disposition] = request.params["disposition"] if request.params["disposition"]
397   options[:filename]    = request.params["filename"]    if request.params["filename"]
398   options[:version]     = request.params["version"]     if request.params["version"]
399   options[:expires_in]  = expires_in(request)           if request.params["expires_at"]
400 
401   derivation = uploaded_file.derivation(name, *args, **options)
402 
403   begin
404     status, headers, body = derivation.response(request.env)
405   rescue Derivation::SourceNotFound
406     error!(404, "Source file not found")
407   rescue Derivation::NotFound
408     error!(404, "Unknown derivation \"#{name}\"")
409   end
410 
411   # tell clients to cache the derivation result if it was successful
412   if status == 200 || status == 206
413     headers["Cache-Control"] = derivation.option(:cache_control)
414   end
415 
416   [status, headers, body]
417 end
inspect()
[show source]
    # File lib/shrine/plugins/derivation_endpoint.rb
419 def inspect
420   "#<#{@shrine_class}::DerivationEndpoint>"
421 end