From a6d80e164f2f5566472370a84ab7cd98736666c4 Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Wed, 30 May 2018 20:01:07 -0400 Subject: Include blob ID in tempfile name for debugging convenience --- activestorage/app/models/active_storage/blob.rb | 10 ++++++++++ activestorage/lib/active_storage/downloader.rb | 6 +----- activestorage/test/models/blob_test.rb | 14 +++++++++----- 3 files changed, 20 insertions(+), 10 deletions(-) (limited to 'activestorage') diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index d36c951292..203e4118c5 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -168,6 +168,16 @@ class ActiveStorage::Blob < ActiveRecord::Base # Downloads the blob to a tempfile on disk. Yields the tempfile. # + # The tempfile's name is prefixed with +ActiveStorage-+ and the blob's ID. Its extension matches that of the blob. + # + # By default, the tempfile is created in Dir.tmpdir. Pass +tempdir:+ to create it in a different directory: + # + # blob.open(tempdir: "/path/to/tmp") do |file| + # # ... + # end + # + # The tempfile is automatically closed and unlinked after the given block is executed. + # # Raises ActiveStorage::IntegrityError if the downloaded data does not match the blob's checksum. def open(tempdir: nil, &block) ActiveStorage::Downloader.new(self, tempdir: tempdir).download_blob_to_tempfile(&block) diff --git a/activestorage/lib/active_storage/downloader.rb b/activestorage/lib/active_storage/downloader.rb index 2aa56a729a..87be6efb05 100644 --- a/activestorage/lib/active_storage/downloader.rb +++ b/activestorage/lib/active_storage/downloader.rb @@ -19,7 +19,7 @@ module ActiveStorage attr_reader :blob, :tempdir def open_tempfile - file = Tempfile.open([ "ActiveStorage", tempfile_extension_with_delimiter ], tempdir) + file = Tempfile.open([ "ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter ], tempdir) begin yield file @@ -40,9 +40,5 @@ module ActiveStorage raise ActiveStorage::IntegrityError end end - - def tempfile_extension_with_delimiter - blob.filename.extension_with_delimiter - end end end diff --git a/activestorage/test/models/blob_test.rb b/activestorage/test/models/blob_test.rb index 2d1857041d..788e9af7b8 100644 --- a/activestorage/test/models/blob_test.rb +++ b/activestorage/test/models/blob_test.rb @@ -85,11 +85,15 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase end test "open with integrity" do - create_file_blob(filename: "racecar.jpg").open do |file| - assert file.binmode? - assert_equal 0, file.pos - assert_match(/\.jpg\z/, file.path) - assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file" + create_file_blob(filename: "racecar.jpg").tap do |blob| + blob.open do |file| + assert file.binmode? + assert_equal 0, file.pos + byebug + assert File.basename(file.path).starts_with?("ActiveStorage-#{blob.id}-") + assert file.path.ends_with?(".jpg") + assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file" + end end end -- cgit v1.2.3