Skip to content
Permalink
Browse files

Permit opening a blob in a custom tempdir

  • Loading branch information...
georgeclaghorn committed May 17, 2018
1 parent f018d4e commit 9f95767979579f5761cb0d2bcccb67f3662349c5
@@ -167,8 +167,8 @@ def download(&block)
end

# Downloads the blob to a tempfile on disk. Yields the tempfile.
def open(&block)
ActiveStorage::Downloader.new(self).download_blob_to_tempfile(&block)
def open(tempdir: nil, &block)
ActiveStorage::Downloader.new(self, tempdir: tempdir).download_blob_to_tempfile(&block)
end


@@ -24,11 +24,15 @@ def metadata
private
# Downloads the blob to a tempfile on disk. Yields the tempfile.
def download_blob_to_tempfile(&block) #:doc:
blob.open(&block)
blob.open tempdir: tempdir, &block
end

def logger #:doc:
ActiveStorage.logger
end

def tempdir #:doc:
Dir.tmpdir
end
end
end
@@ -1,9 +1,10 @@
# frozen_string_literal: true

module ActiveStorage
class Downloader
def initialize(blob)
@blob = blob
class Downloader #:nodoc:
def initialize(blob, tempdir: nil)
@blob = blob
@tempdir = tempdir
end

def download_blob_to_tempfile
@@ -14,10 +15,10 @@ def download_blob_to_tempfile
end

private
attr_reader :blob
attr_reader :blob, :tempdir

def open_tempfile
file = Tempfile.open([ "ActiveStorage", tempfile_extension_with_delimiter ])
file = Tempfile.open([ "ActiveStorage", tempfile_extension_with_delimiter ], tempdir)

begin
yield file
@@ -26,7 +26,7 @@ def preview
private
# Downloads the blob to a tempfile on disk. Yields the tempfile.
def download_blob_to_tempfile(&block) #:doc:
blob.open(&block)
blob.open tempdir: tempdir, &block
end

# Executes a system command, capturing its binary output in a tempfile. Yields the tempfile.
@@ -93,6 +93,18 @@ class UserWithHasOneAttachedDependentFalse < User
end
end

test "open in a custom tempdir" do
tempdir = Dir.mktmpdir

create_file_blob(filename: "racecar.jpg").open(tempdir: tempdir) do |file|
assert file.binmode?
assert_equal 0, file.pos
assert_match(/\.jpg\z/, file.path)
assert file.path.starts_with?(tempdir)
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
end
end

test "urls expiring in 5 minutes" do
blob = create_blob

0 comments on commit 9f95767

Please sign in to comment.
You can’t perform that action at this time.