2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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:
|
|
|
|
#
|
2019-01-24 11:46:42 -05:00
|
|
|
# ActiveStorage::Variation.new(resize_to_limit: [100, 100], monochrome: true, trim: true, rotate: "-90")
|
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
|
|
|
|
|
2018-04-05 19:48:29 -04:00
|
|
|
# Accepts a File object, performs the +transformations+ against it, and
|
|
|
|
# saves the transformed image into a temporary file. If +format+ is specified
|
|
|
|
# it will be the format of the result image, otherwise the result image
|
|
|
|
# retains the source format.
|
2018-08-10 12:10:13 -04:00
|
|
|
def transform(file, format: nil, &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
|
|
|
|
|
|
|
|
# Returns a signed key for all the +transformations+ that this variation was instantiated with.
|
|
|
|
def key
|
|
|
|
self.class.encode(transformations)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2018-08-10 12:10:13 -04:00
|
|
|
def transformer
|
|
|
|
if ActiveStorage.variant_processor
|
|
|
|
begin
|
|
|
|
require "image_processing"
|
|
|
|
rescue LoadError
|
2019-03-11 09:09:14 -04:00
|
|
|
ActiveSupport::Deprecation.warn <<~WARNING.squish
|
2018-08-10 12:10:13 -04:00
|
|
|
Generating image variants will require the image_processing gem in Rails 6.1.
|
|
|
|
Please add `gem 'image_processing', '~> 1.2'` to your Gemfile.
|
|
|
|
WARNING
|
|
|
|
|
|
|
|
ActiveStorage::Transformers::MiniMagickTransformer.new(transformations)
|
|
|
|
else
|
|
|
|
ActiveStorage::Transformers::ImageProcessingTransformer.new(transformations)
|
2018-04-05 19:48:29 -04:00
|
|
|
end
|
2018-08-10 12:10:13 -04:00
|
|
|
else
|
|
|
|
ActiveStorage::Transformers::MiniMagickTransformer.new(transformations)
|
2017-12-22 10:44:34 -05:00
|
|
|
end
|
|
|
|
end
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|