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.
|
# 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.
|
# Raises ActiveStorage::IntegrityError if the downloaded data does not match the blob's checksum.
|
||||||
def open(tempdir: nil, &block)
|
def open(tempdir: nil, &block)
|
||||||
ActiveStorage::Downloader.new(self, tempdir: tempdir).download_blob_to_tempfile(&block)
|
ActiveStorage::Downloader.new(self, tempdir: tempdir).download_blob_to_tempfile(&block)
|
||||||
|
|
|
@ -19,7 +19,7 @@ module ActiveStorage
|
||||||
attr_reader :blob, :tempdir
|
attr_reader :blob, :tempdir
|
||||||
|
|
||||||
def open_tempfile
|
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
|
begin
|
||||||
yield file
|
yield file
|
||||||
|
@ -40,9 +40,5 @@ module ActiveStorage
|
||||||
raise ActiveStorage::IntegrityError
|
raise ActiveStorage::IntegrityError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def tempfile_extension_with_delimiter
|
|
||||||
blob.filename.extension_with_delimiter
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -85,11 +85,15 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "open with integrity" do
|
test "open with integrity" do
|
||||||
create_file_blob(filename: "racecar.jpg").open do |file|
|
create_file_blob(filename: "racecar.jpg").tap do |blob|
|
||||||
assert file.binmode?
|
blob.open do |file|
|
||||||
assert_equal 0, file.pos
|
assert file.binmode?
|
||||||
assert_match(/\.jpg\z/, file.path)
|
assert_equal 0, file.pos
|
||||||
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue