2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-26 08:41:23 -04:00
|
|
|
require "mini_mime"
|
2020-09-25 15:00:04 -04:00
|
|
|
|
2017-08-03 14:41:23 -04:00
|
|
|
# A set of transformations that can be applied to a blob to create a variant. This class is exposed via
|
2017-08-25 16:47:26 -04:00
|
|
|
# the ActiveStorage::Blob#variant method and should rarely be used directly.
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
|
|
|
# In case you do need to use this directly, it's instantiated using a hash of transformations where
|
|
|
|
# the key is the command and the value is the arguments. Example:
|
|
|
|
#
|
2021-07-14 13:26:43 -04:00
|
|
|
# ActiveStorage::Variation.new(resize_to_limit: [100, 100], colourspace: "b-w", rotate: "-90", saver: { trim: true })
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
2018-04-05 19:48:29 -04:00
|
|
|
# The options map directly to {ImageProcessing}[https://github.com/janko-m/image_processing] commands.
|
2017-07-21 16:51:31 -04:00
|
|
|
class ActiveStorage::Variation
|
|
|
|
attr_reader :transformations
|
|
|
|
|
|
|
|
class << self
|
2017-10-12 13:40:25 -04:00
|
|
|
# Returns a Variation instance based on the given variator. If the variator is a Variation, it is
|
|
|
|
# returned unmodified. If it is a String, it is passed to ActiveStorage::Variation.decode. Otherwise,
|
|
|
|
# it is assumed to be a transformations Hash and is passed directly to the constructor.
|
|
|
|
def wrap(variator)
|
|
|
|
case variator
|
2017-09-28 16:43:37 -04:00
|
|
|
when self
|
2017-10-12 13:40:25 -04:00
|
|
|
variator
|
|
|
|
when String
|
|
|
|
decode variator
|
2017-09-28 16:43:37 -04:00
|
|
|
else
|
2017-10-12 13:40:25 -04:00
|
|
|
new variator
|
2017-09-28 16:43:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-12 13:40:25 -04:00
|
|
|
# Returns a Variation instance with the transformations that were encoded by +encode+.
|
2017-07-21 16:51:31 -04:00
|
|
|
def decode(key)
|
2017-07-23 16:51:01 -04:00
|
|
|
new ActiveStorage.verifier.verify(key, purpose: :variation)
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
2017-07-22 00:14:46 -04:00
|
|
|
|
2017-08-10 23:36:07 -04:00
|
|
|
# Returns a signed key for the +transformations+, which can be used to refer to a specific
|
2017-08-15 12:48:19 -04:00
|
|
|
# variation in a URL or combined key (like <tt>ActiveStorage::Variant#key</tt>).
|
2017-07-21 16:51:31 -04:00
|
|
|
def encode(transformations)
|
2017-07-23 16:51:01 -04:00
|
|
|
ActiveStorage.verifier.generate(transformations, purpose: :variation)
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(transformations)
|
2019-04-20 06:05:58 -04:00
|
|
|
@transformations = transformations.deep_symbolize_keys
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
|
|
|
|
2020-09-22 13:03:53 -04:00
|
|
|
def default_to(defaults)
|
|
|
|
self.class.new transformations.reverse_merge(defaults)
|
|
|
|
end
|
|
|
|
|
2018-04-05 19:48:29 -04:00
|
|
|
# Accepts a File object, performs the +transformations+ against it, and
|
2020-09-22 13:03:53 -04:00
|
|
|
# saves the transformed image into a temporary file.
|
|
|
|
def transform(file, &block)
|
2018-01-10 21:50:14 -05:00
|
|
|
ActiveSupport::Notifications.instrument("transform.active_storage") do
|
2018-08-10 12:10:13 -04:00
|
|
|
transformer.transform(file, format: format, &block)
|
2018-04-05 19:48:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-22 13:03:53 -04:00
|
|
|
def format
|
|
|
|
transformations.fetch(:format, :png).tap do |format|
|
2021-03-26 08:41:23 -04:00
|
|
|
if MiniMime.lookup_by_extension(format.to_s).nil?
|
2020-09-22 13:03:53 -04:00
|
|
|
raise ArgumentError, "Invalid variant format (#{format.inspect})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_type
|
2021-03-26 08:41:23 -04:00
|
|
|
MiniMime.lookup_by_extension(format.to_s).content_type
|
2020-09-22 13:03:53 -04:00
|
|
|
end
|
|
|
|
|
2018-04-05 19:48:29 -04:00
|
|
|
# Returns a signed key for all the +transformations+ that this variation was instantiated with.
|
|
|
|
def key
|
|
|
|
self.class.encode(transformations)
|
|
|
|
end
|
|
|
|
|
2019-12-05 22:21:24 -05:00
|
|
|
def digest
|
2021-03-22 05:25:49 -04:00
|
|
|
OpenSSL::Digest::SHA1.base64digest Marshal.dump(transformations)
|
2019-12-05 22:21:24 -05:00
|
|
|
end
|
|
|
|
|
2018-04-05 19:48:29 -04:00
|
|
|
private
|
2018-08-10 12:10:13 -04:00
|
|
|
def transformer
|
2020-05-11 13:31:37 -04:00
|
|
|
ActiveStorage::Transformers::ImageProcessingTransformer.new(transformations.except(:format))
|
2017-12-22 10:44:34 -05:00
|
|
|
end
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|