aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/variant.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-21 15:51:31 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-07-21 15:51:31 -0500
commit7f4111185ca6286adbe0ffc1346d416c5fa7dfd3 (patch)
treee3de354e39b5913e532910c2cfc1f90686dcf6bb /lib/active_storage/variant.rb
parent0c47740d858cb04c7165bce411584c3b05d155b6 (diff)
downloadrails-7f4111185ca6286adbe0ffc1346d416c5fa7dfd3.tar.gz
rails-7f4111185ca6286adbe0ffc1346d416c5fa7dfd3.tar.bz2
rails-7f4111185ca6286adbe0ffc1346d416c5fa7dfd3.zip
Extract variation value object
Diffstat (limited to 'lib/active_storage/variant.rb')
-rw-r--r--lib/active_storage/variant.rb66
1 files changed, 12 insertions, 54 deletions
diff --git a/lib/active_storage/variant.rb b/lib/active_storage/variant.rb
index 4145ee644d..8be51eba92 100644
--- a/lib/active_storage/variant.rb
+++ b/lib/active_storage/variant.rb
@@ -1,82 +1,40 @@
require "active_storage/blob"
-require "active_support/core_ext/object/inclusion"
require "mini_magick"
class ActiveStorage::Variant
- class_attribute :verifier
-
- ALLOWED_TRANSFORMATIONS = %i(
- resize rotate format flip fill monochrome orient quality roll scale sharpen shave shear size thumbnail
- transparent transpose transverse trim background bordercolor compress crop
- )
-
attr_reader :blob, :variation
delegate :service, to: :blob
- def self.find_or_process_by!(blob_key:, encoded_variant_key:)
- new(ActiveStorage::Blob.find_by!(key: blob_key), variation: verifier.verify(encoded_variant_key)).processed
- end
-
- def self.encode_key(variation)
- verifier.generate(variation)
+ class << self
+ def find_or_process_by!(blob_key:, variation_key:)
+ new(ActiveStorage::Blob.find_by!(key: blob_key), variation: ActiveStorage::Variation.decode(variation_key)).processed
+ end
end
- def initialize(blob, variation:)
+ def initialize(blob, variation)
@blob, @variation = blob, variation
end
def processed
- process unless processed?
+ process unless service.exist?(key)
self
end
- def url(expires_in: 5.minutes, disposition: :inline)
- service.url blob_variant_key, expires_in: expires_in, disposition: disposition, filename: blob.filename
+ def key
+ "variants/#{blob.key}/#{variation.key}"
end
- def key
- verifier.generate(variation)
+ def url(expires_in: 5.minutes, disposition: :inline)
+ service.url key, expires_in: expires_in, disposition: disposition, filename: blob.filename
end
private
- def processed?
- service.exist?(blob_variant_key)
- end
-
def process
- upload_variant transform(download_blob)
- end
-
- def download_blob
- service.download(blob.key)
- end
-
- def upload_variant(variant)
- service.upload blob_variant_key, variant
- end
-
- def blob_variant_key
- "variants/#{blob.key}/#{key}"
+ service.upload key, transform(service.download(blob.key))
end
def transform(io)
- File.open \
- MiniMagick::Image.read(io).tap { |transforming_image|
- variation.each do |(method, argument)|
- if method = allowed_transformational_method(method.to_sym)
- if argument.blank? || argument == true
- transforming_image.public_send(method)
- else
- # FIXME: Consider whitelisting allowed arguments as well?
- transforming_image.public_send(method, argument)
- end
- end
- end
- }.path
- end
-
- def allowed_transformational_method(method)
- method.presence_in(ALLOWED_TRANSFORMATIONS)
+ File.open MiniMagick::Image.read(io).tap { |image| variation.transform(image) }.path
end
end