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.



355
356
357
358
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 355

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#shrine_classObject (readonly)

Returns the value of attribute shrine_class.



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

def shrine_class
  @shrine_class
end

Instance Method Details

#call(env) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 360

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 = 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

#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.



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
414
415
416
417
418
419
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 385

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

  name, *args, serialized_file = request.path_info.split("/")[1..-1]
  serialized_file = serialized_file.sub(/\.\w+$/, "")

  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[:version]     = request.params["version"]     if request.params["version"]
  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



421
422
423
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 421

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