mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Include blob ID in tempfile name for debugging convenience
This commit is contained in:
parent
e2e053608e
commit
a6d80e164f
3 changed files with 20 additions and 10 deletions
|
@ -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 <tt>Dir.tmpdir</tt>. 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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue