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
355 def initialize(shrine_class:, options: {})
356   @shrine_class = shrine_class
357   @options      = options
358 end

Public Instance methods

call(env)
[show source]
    # File lib/shrine/plugins/derivation_endpoint.rb
360 def call(env)
361   request = Rack::Request.new(env)
362 
363   status, headers, body = catch(:halt) do
364     error!(405, "Method not allowed") unless request.get? || request.head?
365 
366     handle_request(request)
367   end
368 
369   headers = Rack::Headers[headers] if Rack.release >= "3"
370   headers["Content-Length"] ||= body.respond_to?(:bytesize) ? body.bytesize.to_s :
371                                                               body.map(&:bytesize).inject(0, :+).to_s
372 
373   [status, headers, body]
374 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
385 def handle_request(request)
386   verify_signature!(request) if secret_key
387   check_expiry!(request)
388 
389   name, *args, serialized_file = request.path_info.split("/")[1..-1]
390   serialized_file = serialized_file.sub(/\.\w+$/, "")
391 
392   name          = name.to_sym
393   uploaded_file = shrine_class::UploadedFile.urlsafe_load(serialized_file)
394 
395   # request params override statically configured options
396   options = self.options.dup
397   options[:type]        = request.params["type"]        if request.params["type"]
398   options[:disposition] = request.params["disposition"] if request.params["disposition"]
399   options[:filename]    = request.params["filename"]    if request.params["filename"]
400   options[:version]     = request.params["version"]     if request.params["version"]
401   options[:expires_in]  = expires_in(request)           if request.params["expires_at"]
402 
403   derivation = uploaded_file.derivation(name, *args, **options)
404 
405   begin
406     status, headers, body = derivation.response(request.env)
407   rescue Derivation::SourceNotFound
408     error!(404, "Source file not found")
409   rescue Derivation::NotFound
410     error!(404, "Unknown derivation \"#{name}\"")
411   end
412 
413   # tell clients to cache the derivation result if it was successful
414   if status == 200 || status == 206
415     headers["Cache-Control"] = derivation.option(:cache_control)
416   end
417 
418   [status, headers, body]
419 end
inspect()
[show source]
    # File lib/shrine/plugins/derivation_endpoint.rb
421 def inspect
422   "#<#{@shrine_class}::DerivationEndpoint>"
423 end