aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/downloading.rb
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/active_storage/downloading.rb
parent2f9549d4f46ca1f9cc437d4f89bd8df405e28dbd (diff)
downloadrails-c392af365fc93921e6fe3537a0d2f30987878da3.tar.gz
rails-c392af365fc93921e6fe3537a0d2f30987878da3.tar.bz2
rails-c392af365fc93921e6fe3537a0d2f30987878da3.zip
Unlink internal tempfiles after use
Diffstat (limited to 'activestorage/lib/active_storage/downloading.rb')
-rw-r--r--activestorage/lib/active_storage/downloading.rb12
1 files changed, 11 insertions, 1 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