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