class Shrine::Derivation

Attributes

args[R]
name[R]
options[R]
source[R]

Public Class Methods

new(name:, args:, source:, options:) click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 124
def initialize(name:, args:, source:, options:)
  @name    = name.to_sym
  @args    = args
  @source  = source
  @options = options
end
option(name, default: nil, result: nil) click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 186
def self.option(name, default: nil, result: nil)
  options[name] = { default: default, result: result }
end
options() click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 182
def self.options
  @options ||= {}
end

Public Instance Methods

delete() click to toggle source

Deletes the derivation result from the storage.

# File lib/shrine/plugins/derivation_endpoint.rb, line 178
def delete
  Derivation::Delete.new(self).call
end
generate(file = nil) click to toggle source

Calls the derivation block and returns the direct result.

# File lib/shrine/plugins/derivation_endpoint.rb, line 155
def generate(file = nil)
  Derivation::Generate.new(self).call(file)
end
opened() click to toggle source

Returns opened Shrine::UploadedFile object pointing to the uploaded derivative if it exists.

# File lib/shrine/plugins/derivation_endpoint.rb, line 173
def opened
  Derivation::Opened.new(self).call
end
option(name) click to toggle source

Retrieves the value of a derivation option.

  • If specified as a raw value, returns that value

  • If specified as a block, evaluates that it and returns the result

  • If unspecified, returns the default value

# File lib/shrine/plugins/derivation_endpoint.rb, line 216
def option(name)
  option_definition = self.class.options.fetch(name)

  value = options.fetch(name) { shrine_class.derivation_options[name] }
  value = instance_exec(&value) if value.is_a?(Proc) && value.arity == 0

  if value.nil?
    default = option_definition[:default]
    value   = instance_exec(&default) if default
  end

  result = option_definition[:result]
  value  = instance_exec(value, &result) if result

  value
end
processed() click to toggle source

Returns the derivation result as a File/Tempfile or a Shrine::UploadedFile object.

# File lib/shrine/plugins/derivation_endpoint.rb, line 150
def processed
  Derivation::Processed.new(self).call
end
response(env) click to toggle source

Returns the derivation result in form of a Rack response triple.

# File lib/shrine/plugins/derivation_endpoint.rb, line 144
def response(env)
  Derivation::Response.new(self).call(env)
end
retrieve() click to toggle source

Returns a Shrine::UploadedFile object pointing to the uploaded derivative if it exists.

# File lib/shrine/plugins/derivation_endpoint.rb, line 167
def retrieve
  Derivation::Retrieve.new(self).call
end
shrine_class() click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 233
def shrine_class
  source.shrine_class
end
upload(file = nil, **options) click to toggle source

Uploads the derivation result to a dedicated destination on the specified Shrine storage.

# File lib/shrine/plugins/derivation_endpoint.rb, line 161
def upload(file = nil, **options)
  Derivation::Upload.new(self).call(file, **options)
end
url(**options) click to toggle source

Returns an URL to the derivation.

# File lib/shrine/plugins/derivation_endpoint.rb, line 132
def url(**options)
  Derivation::Url.new(self).call(
    host:       option(:host),
    prefix:     option(:prefix),
    expires_in: option(:expires_in),
    version:    option(:version),
    metadata:   option(:metadata),
    **options,
  )
end

Private Instance Methods

default_cache_control() click to toggle source

Allows caching for 1 year or until the URL expires.

# File lib/shrine/plugins/derivation_endpoint.rb, line 267
def default_cache_control
  if option(:expires_in)
    "public, max-age=#{option(:expires_in)}"
  else
    "public, max-age=#{365*24*60*60}"
  end
end
default_filename() click to toggle source

For derivation “thumbnail” with arguments “600/400” and source id of “1f6375ad.ext”, returns “thumbnail-600-400-1f6375ad”.

# File lib/shrine/plugins/derivation_endpoint.rb, line 248
def default_filename
  [name, *args, File.basename(source.id, ".*")].join("-")
end
default_upload_location() click to toggle source

For derivation “thumbnail” with arguments “600/400” and source id of “1f6375ad.ext”, returns “1f6375ad/thumbnail-600-400”.

# File lib/shrine/plugins/derivation_endpoint.rb, line 254
def default_upload_location
  directory = source.id.sub(/\.[^\/]+/, "")
  filename  = [name, *args].join("-")

  [directory, filename].join("/")
end
default_upload_storage() click to toggle source

The source uploaded file storage is the default derivative storage.

# File lib/shrine/plugins/derivation_endpoint.rb, line 262
def default_upload_storage
  source.storage_key
end
upload_location(location) click to toggle source

When bumping the version, we also append it to the upload location to ensure we’re not retrieving old derivatives.

# File lib/shrine/plugins/derivation_endpoint.rb, line 241
def upload_location(location)
  location = location.sub(/(?=(\.\w+)?$)/, "-#{option(:version)}") if option(:version)
  location
end