Class: Shrine::DerivationEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/plugins/derivation_endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shrine_class:, options: {}) ⇒ DerivationEndpoint

Returns a new instance of DerivationEndpoint.



353
354
355
356
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 353

def initialize(shrine_class:, options: {})
  @shrine_class = shrine_class
  @options      = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



351
352
353
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 351

def options
  @options
end

#shrine_classObject (readonly)

Returns the value of attribute shrine_class.



351
352
353
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 351

def shrine_class
  @shrine_class
end

Instance Method Details

#call(env) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 358

def call(env)
  request = Rack::Request.new(env)

  status, headers, body = catch(:halt) do
    error!(405, "Method not allowed") unless request.get? || request.head?

    handle_request(request)
  end

  headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s

  [status, headers, body]
end

#handle_request(request) ⇒ Object

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.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 381

def handle_request(request)
  verify_signature!(request)
  check_expiry!(request)

  name, *args, serialized_file = request.path_info.split("/")[1..-1]

  name          = name.to_sym
  uploaded_file = shrine_class::UploadedFile.urlsafe_load(serialized_file)

  # request params override statically configured options
  options = self.options.dup
  options[:type]        = request.params["type"]        if request.params["type"]
  options[:disposition] = request.params["disposition"] if request.params["disposition"]
  options[:filename]    = request.params["filename"]    if request.params["filename"]
  options[:expires_in]  = expires_in(request)           if request.params["expires_at"]

  derivation = uploaded_file.derivation(name, *args, **options)

  begin
    status, headers, body = derivation.response(request.env)
  rescue Derivation::SourceNotFound
    error!(404, "Source file not found")
  rescue Derivation::NotFound
    error!(404, "Unknown derivation \"#{name}\"")
  end

  # tell clients to cache the derivation result if it was successful
  if status == 200 || status == 206
    headers["Cache-Control"] = derivation.option(:cache_control)
  end

  [status, headers, body]
end

#inspectObject Also known as: to_s



415
416
417
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 415

def inspect
  "#<#{@shrine_class}::DerivationEndpoint>"
end