diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-21 16:51:04 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-21 16:51:04 -0500 |
commit | f0d7ce9e767ffbf7307ed6efaa4a189ae3ea3c0a (patch) | |
tree | 545ce85fc61a1e6bcc75110428648dbf9d59a0ac /lib/active_storage/variant.rb | |
parent | 986a71d26868d296f4c619df85909d1073b6c91f (diff) | |
parent | 6ac4fec964e67cf3d7dfbf7726bff9b05aca522c (diff) | |
download | rails-f0d7ce9e767ffbf7307ed6efaa4a189ae3ea3c0a.tar.gz rails-f0d7ce9e767ffbf7307ed6efaa4a189ae3ea3c0a.tar.bz2 rails-f0d7ce9e767ffbf7307ed6efaa4a189ae3ea3c0a.zip |
Merge pull request #63 from rails/variants
On-demand variants
Diffstat (limited to 'lib/active_storage/variant.rb')
-rw-r--r-- | lib/active_storage/variant.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/active_storage/variant.rb b/lib/active_storage/variant.rb new file mode 100644 index 0000000000..435033f980 --- /dev/null +++ b/lib/active_storage/variant.rb @@ -0,0 +1,35 @@ +require "active_storage/blob" +require "mini_magick" + +# Image blobs can have variants that are the result of a set of transformations applied to the original. +class ActiveStorage::Variant + attr_reader :blob, :variation + delegate :service, to: :blob + + def initialize(blob, variation) + @blob, @variation = blob, variation + end + + def processed + process unless service.exist?(key) + self + end + + def key + "variants/#{blob.key}/#{variation.key}" + end + + def url(expires_in: 5.minutes, disposition: :inline) + service.url key, expires_in: expires_in, disposition: disposition, filename: blob.filename + end + + + private + def process + service.upload key, transform(service.download(blob.key)) + end + + def transform(io) + File.open MiniMagick::Image.read(io).tap { |image| variation.transform(image) }.path + end +end |