aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2017-12-18 13:14:26 -0500
committerGeorge Claghorn <george@basecamp.com>2017-12-18 13:14:26 -0500
commita6eb2a27c3e73019bf1fa1b11794ebe55c7eaa96 (patch)
treea6367a0a6f3061591fa66ff093c40a420a131d63 /activestorage
parent0dfe220425d733b9af5082bcdb04a6a8064d3b6a (diff)
downloadrails-a6eb2a27c3e73019bf1fa1b11794ebe55c7eaa96.tar.gz
rails-a6eb2a27c3e73019bf1fa1b11794ebe55c7eaa96.tar.bz2
rails-a6eb2a27c3e73019bf1fa1b11794ebe55c7eaa96.zip
Ensure MiniMagick tempfiles are properly unlinked after image transformation
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/app/models/active_storage/variant.rb27
1 files changed, 18 insertions, 9 deletions
diff --git a/activestorage/app/models/active_storage/variant.rb b/activestorage/app/models/active_storage/variant.rb
index 8909fd628a..ece99518be 100644
--- a/activestorage/app/models/active_storage/variant.rb
+++ b/activestorage/app/models/active_storage/variant.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require "active_storage/downloading"
+
# Image blobs can have variants that are the result of a set of transformations applied to the original.
# These variants are used to create thumbnails, fixed-size avatars, or any other derivative image from the
# original.
@@ -35,6 +37,8 @@
#
# avatar.variant(resize: "100x100", monochrome: true, flip: "-90")
class ActiveStorage::Variant
+ include ActiveStorage::Downloading
+
WEB_IMAGE_CONTENT_TYPES = %w( image/png image/jpeg image/jpg image/gif )
attr_reader :blob, :variation
@@ -78,7 +82,11 @@ class ActiveStorage::Variant
end
def process
- service.upload key, transform(blob.download)
+ open_image do |image|
+ transform image
+ format image
+ upload image
+ end
end
@@ -95,23 +103,24 @@ class ActiveStorage::Variant
end
- def transform(io)
- read_image_from(io) do |image|
- mogrify image
- format image
- end
+ def open_image(&block)
+ download_image.tap(&block).destroy!
end
- def read_image_from(io, &block)
+ def download_image
require "mini_magick"
- File.open MiniMagick::Image.read(io).tap(&block).path
+ MiniMagick::Image.create { |file| download_blob_to(file) }
end
- def mogrify(image)
+ def transform(image)
variation.transform(image)
end
def format(image)
image.format("PNG") unless WEB_IMAGE_CONTENT_TYPES.include?(blob.content_type)
end
+
+ def upload(image)
+ File.open(image.path, "r") { |file| service.upload(key, file) }
+ end
end