2017-10-22 13:16:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveStorage
|
|
|
|
module Downloading
|
|
|
|
private
|
2017-11-02 15:07:04 -04:00
|
|
|
# Opens a new tempfile in #tempdir and copies blob data into it. Yields the tempfile.
|
2017-10-22 13:16:59 -04:00
|
|
|
def download_blob_to_tempfile # :doc:
|
2017-12-31 10:36:58 -05:00
|
|
|
Tempfile.open([ "ActiveStorage", blob.filename.extension_with_delimiter ], tempdir) do |file|
|
2017-10-22 13:16:59 -04:00
|
|
|
download_blob_to file
|
|
|
|
yield file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-22 23:14:44 -04:00
|
|
|
# Efficiently downloads blob data into the given file.
|
2017-10-22 13:16:59 -04:00
|
|
|
def download_blob_to(file) # :doc:
|
|
|
|
file.binmode
|
|
|
|
blob.download { |chunk| file.write(chunk) }
|
|
|
|
file.rewind
|
|
|
|
end
|
2017-11-02 15:07:04 -04:00
|
|
|
|
|
|
|
# Returns the directory in which tempfiles should be opened. Defaults to +Dir.tmpdir+.
|
|
|
|
def tempdir # :doc:
|
|
|
|
Dir.tmpdir
|
|
|
|
end
|
2017-10-22 13:16:59 -04:00
|
|
|
end
|
|
|
|
end
|