aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2018-01-26 19:48:32 -0500
committerGeorge Claghorn <george@basecamp.com>2018-01-26 19:48:32 -0500
commitc392af365fc93921e6fe3537a0d2f30987878da3 (patch)
tree7b78066a38bdb84816da3c57ee060a926b080137 /activestorage/lib
parent2f9549d4f46ca1f9cc437d4f89bd8df405e28dbd (diff)
downloadrails-c392af365fc93921e6fe3537a0d2f30987878da3.tar.gz
rails-c392af365fc93921e6fe3537a0d2f30987878da3.tar.bz2
rails-c392af365fc93921e6fe3537a0d2f30987878da3.zip
Unlink internal tempfiles after use
Diffstat (limited to 'activestorage/lib')
-rw-r--r--activestorage/lib/active_storage/downloading.rb12
-rw-r--r--activestorage/lib/active_storage/previewer.rb12
2 files changed, 22 insertions, 2 deletions
diff --git a/activestorage/lib/active_storage/downloading.rb b/activestorage/lib/active_storage/downloading.rb
index 295289c1e7..f2a1fffdcb 100644
--- a/activestorage/lib/active_storage/downloading.rb
+++ b/activestorage/lib/active_storage/downloading.rb
@@ -7,12 +7,22 @@ module ActiveStorage
private
# Opens a new tempfile in #tempdir and copies blob data into it. Yields the tempfile.
def download_blob_to_tempfile #:doc:
- Tempfile.open([ "ActiveStorage", blob.filename.extension_with_delimiter ], tempdir) do |file|
+ open_tempfile_for_blob do |file|
download_blob_to file
yield file
end
end
+ def open_tempfile_for_blob
+ tempfile = Tempfile.open([ "ActiveStorage", blob.filename.extension_with_delimiter ], tempdir)
+
+ begin
+ yield tempfile
+ ensure
+ tempfile.close!
+ end
+ end
+
# Efficiently downloads blob data into the given file.
def download_blob_to(file) #:doc:
file.binmode
diff --git a/activestorage/lib/active_storage/previewer.rb b/activestorage/lib/active_storage/previewer.rb
index dacab1e7df..cf19987d72 100644
--- a/activestorage/lib/active_storage/previewer.rb
+++ b/activestorage/lib/active_storage/previewer.rb
@@ -44,13 +44,23 @@ module ActiveStorage
# The output tempfile is opened in the directory returned by ActiveStorage::Downloading#tempdir.
def draw(*argv) #:doc:
ActiveSupport::Notifications.instrument("preview.active_storage") do
- Tempfile.open("ActiveStorage", tempdir) do |file|
+ open_tempfile_for_drawing do |file|
capture(*argv, to: file)
yield file
end
end
end
+ def open_tempfile_for_drawing
+ tempfile = Tempfile.open("ActiveStorage", tempdir)
+
+ begin
+ yield tempfile
+ ensure
+ tempfile.close!
+ end
+ end
+
def capture(*argv, to:)
to.binmode
IO.popen(argv, err: File::NULL) { |out| IO.copy_stream(out, to) }