class Shrine::Derivation
Attributes
Public Class Methods
# 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
# File lib/shrine/plugins/derivation_endpoint.rb, line 186 def self.option(name, default: nil, result: nil) options[name] = { default: default, result: result } end
# File lib/shrine/plugins/derivation_endpoint.rb, line 182 def self.options @options ||= {} end
Public Instance Methods
Deletes the derivation result from the storage.
# File lib/shrine/plugins/derivation_endpoint.rb, line 178 def delete Derivation::Delete.new(self).call end
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
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
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
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
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
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
# File lib/shrine/plugins/derivation_endpoint.rb, line 233 def shrine_class source.shrine_class end
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
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
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
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
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
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
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