2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-18 13:14:26 -05:00
|
|
|
require "active_storage/downloading"
|
|
|
|
|
2017-07-21 17:34:28 -04:00
|
|
|
# Image blobs can have variants that are the result of a set of transformations applied to the original.
|
2017-07-24 13:05:15 -04:00
|
|
|
# These variants are used to create thumbnails, fixed-size avatars, or any other derivative image from the
|
|
|
|
# original.
|
|
|
|
#
|
2018-04-05 19:48:29 -04:00
|
|
|
# Variants rely on {ImageProcessing}[https://github.com/janko-m/image_processing] gem for the actual transformations
|
|
|
|
# of the file, so you must add <tt>gem "image_processing"</tt> to your Gemfile if you wish to use variants. By
|
|
|
|
# default, images will be processed with {ImageMagick}[http://imagemagick.org] using the
|
|
|
|
# {MiniMagick}[https://github.com/minimagick/minimagick] gem, but you can also switch to the
|
|
|
|
# {libvips}[http://jcupitt.github.io/libvips/] processor operated by the {ruby-vips}[https://github.com/jcupitt/ruby-vips]
|
|
|
|
# gem).
|
|
|
|
#
|
2018-04-22 17:40:42 -04:00
|
|
|
# Rails.application.config.active_storage.variant_processor
|
2018-04-05 19:48:29 -04:00
|
|
|
# # => :mini_magick
|
|
|
|
#
|
2018-04-22 17:40:42 -04:00
|
|
|
# Rails.application.config.active_storage.variant_processor = :vips
|
2018-04-05 19:48:29 -04:00
|
|
|
# # => :vips
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
2018-04-23 06:21:42 -04:00
|
|
|
# Note that to create a variant it's necessary to download the entire blob file from the service. The larger
|
|
|
|
# the image, the more memory is used. Because of this process, you also want to be considerate about when the variant
|
|
|
|
# is actually processed. You shouldn't be processing variants inline in a template, for example. Delay the processing
|
|
|
|
# to an on-demand controller, like the one provided in ActiveStorage::RepresentationsController.
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
|
|
|
# To refer to such a delayed on-demand variant, simply link to the variant through the resolved route provided
|
|
|
|
# by Active Storage like so:
|
|
|
|
#
|
2018-04-23 06:21:42 -04:00
|
|
|
# <%= image_tag Current.user.avatar.variant(resize: "100x100") %>
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
2018-04-08 07:56:41 -04:00
|
|
|
# This will create a URL for that specific blob with that specific variant, which the ActiveStorage::RepresentationsController
|
2017-07-24 13:05:15 -04:00
|
|
|
# can then produce on-demand.
|
|
|
|
#
|
2017-08-15 12:48:19 -04:00
|
|
|
# When you do want to actually produce the variant needed, call +processed+. This will check that the variant
|
2017-07-24 13:05:15 -04:00
|
|
|
# has already been processed and uploaded to the service, and, if so, just return that. Otherwise it will perform
|
|
|
|
# the transformations, upload the variant to the service, and return itself again. Example:
|
|
|
|
#
|
2018-04-23 06:21:42 -04:00
|
|
|
# avatar.variant(resize: "100x100").processed.service_url
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
|
|
|
# This will create and process a variant of the avatar blob that's constrained to a height and width of 100.
|
|
|
|
# Then it'll upload said variant to the service according to a derivative key of the blob and the transformations.
|
|
|
|
#
|
2018-04-23 06:21:42 -04:00
|
|
|
# You can combine any number of ImageMagick/libvips operations into a variant. In addition to that, you can also use
|
|
|
|
# any macros provided by the ImageProcessing gem (such as +resize_to_limit+).
|
|
|
|
#
|
|
|
|
# avatar.variant(resize_to_limit: [800, 800], monochrome: true, flip: "-90")
|
|
|
|
#
|
|
|
|
# Visit the following links for a list of available ImageProcessing commands and ImageMagick/libvips operations:
|
2018-04-05 19:48:29 -04:00
|
|
|
#
|
|
|
|
# * {ImageProcessing::MiniMagick}[https://github.com/janko-m/image_processing/blob/master/doc/minimagick.md#methods]
|
|
|
|
# * {ImageMagick reference}[https://www.imagemagick.org/script/mogrify.php]
|
|
|
|
# * {ImageProcessing::Vips}[https://github.com/janko-m/image_processing/blob/master/doc/vips.md#methods]
|
|
|
|
# * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
|
2017-07-11 12:53:17 -04:00
|
|
|
class ActiveStorage::Variant
|
2017-12-18 13:14:26 -05:00
|
|
|
include ActiveStorage::Downloading
|
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
WEB_IMAGE_CONTENT_TYPES = %w( image/png image/jpeg image/jpg image/gif )
|
|
|
|
|
2017-07-11 12:53:17 -04:00
|
|
|
attr_reader :blob, :variation
|
|
|
|
delegate :service, to: :blob
|
|
|
|
|
2017-09-28 16:43:37 -04:00
|
|
|
def initialize(blob, variation_or_variation_key)
|
|
|
|
@blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key)
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 13:05:15 -04:00
|
|
|
# Returns the variant instance itself after it's been processed or an existing processing has been found on the service.
|
2017-07-20 18:35:15 -04:00
|
|
|
def processed
|
2017-07-23 17:28:45 -04:00
|
|
|
process unless processed?
|
2017-07-20 18:35:15 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-07-24 13:05:15 -04:00
|
|
|
# Returns a combination key of the blob and the variation that together identifies a specific variant.
|
2017-07-21 16:51:31 -04:00
|
|
|
def key
|
2017-10-04 15:26:04 -04:00
|
|
|
"variants/#{blob.key}/#{Digest::SHA256.hexdigest(variation.key)}"
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 13:05:15 -04:00
|
|
|
# Returns the URL of the variant on the service. This URL is intended to be short-lived for security and not used directly
|
2017-08-10 23:36:07 -04:00
|
|
|
# with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL.
|
|
|
|
# Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And
|
|
|
|
# it allows permanent URLs that redirect to the +service_url+ to be cached in the view.
|
2017-07-24 13:05:15 -04:00
|
|
|
#
|
2017-08-15 12:48:19 -04:00
|
|
|
# Use <tt>url_for(variant)</tt> (or the implied form, like +link_to variant+ or +redirect_to variant+) to get the stable URL
|
2018-04-08 07:56:41 -04:00
|
|
|
# for a variant that points to the ActiveStorage::RepresentationsController, which in turn will use this +service_call+ method
|
2017-07-24 13:05:15 -04:00
|
|
|
# for its redirection.
|
2017-09-28 16:43:37 -04:00
|
|
|
def service_url(expires_in: service.url_expires_in, disposition: :inline)
|
2017-12-18 07:47:42 -05:00
|
|
|
service.url key, expires_in: expires_in, disposition: disposition, filename: filename, content_type: content_type
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
|
|
|
|
2017-10-13 07:52:39 -04:00
|
|
|
# Returns the receiving variant. Allows ActiveStorage::Variant and ActiveStorage::Preview instances to be used interchangeably.
|
2017-10-12 11:47:21 -04:00
|
|
|
def image
|
|
|
|
self
|
|
|
|
end
|
2017-08-15 15:57:27 -04:00
|
|
|
|
2017-07-11 12:53:17 -04:00
|
|
|
private
|
2017-07-23 17:28:45 -04:00
|
|
|
def processed?
|
|
|
|
service.exist?(key)
|
|
|
|
end
|
|
|
|
|
2017-07-20 18:35:15 -04:00
|
|
|
def process
|
2018-04-05 19:48:29 -04:00
|
|
|
download_blob_to_tempfile do |image|
|
2018-04-22 17:43:11 -04:00
|
|
|
transform image do |output|
|
|
|
|
upload output
|
|
|
|
end
|
2017-12-18 13:14:26 -05:00
|
|
|
end
|
2017-12-18 07:47:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def filename
|
|
|
|
if WEB_IMAGE_CONTENT_TYPES.include?(blob.content_type)
|
|
|
|
blob.filename
|
|
|
|
else
|
|
|
|
ActiveStorage::Filename.new("#{blob.filename.base}.png")
|
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
def content_type
|
|
|
|
blob.content_type.presence_in(WEB_IMAGE_CONTENT_TYPES) || "image/png"
|
|
|
|
end
|
|
|
|
|
2017-12-18 13:14:26 -05:00
|
|
|
def transform(image)
|
2018-04-05 19:48:29 -04:00
|
|
|
format = "png" unless WEB_IMAGE_CONTENT_TYPES.include?(blob.content_type)
|
2018-04-22 17:43:11 -04:00
|
|
|
result = variation.transform(image, format: format)
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield result
|
|
|
|
ensure
|
|
|
|
result.close!
|
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
2017-12-18 13:14:26 -05:00
|
|
|
|
2018-04-05 19:48:29 -04:00
|
|
|
def upload(file)
|
|
|
|
service.upload(key, file)
|
2017-12-18 13:14:26 -05:00
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|