1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/lib/active_storage/downloading.rb

40 lines
976 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-01-22 17:09:13 -05:00
require "tmpdir"
module ActiveStorage
module Downloading
private
# Opens a new tempfile in #tempdir and copies blob data into it. Yields the tempfile.
2018-01-22 17:09:13 -05:00
def download_blob_to_tempfile #:doc:
2018-01-26 19:48:32 -05:00
open_tempfile_for_blob do |file|
download_blob_to file
yield file
end
end
2018-01-26 19:48:32 -05:00
def open_tempfile_for_blob
tempfile = Tempfile.open([ "ActiveStorage", blob.filename.extension_with_delimiter ], tempdir)
begin
yield tempfile
ensure
tempfile.close!
end
end
# Efficiently downloads blob data into the given file.
2018-01-22 17:09:13 -05:00
def download_blob_to(file) #:doc:
file.binmode
blob.download { |chunk| file.write(chunk) }
file.flush
file.rewind
end
# Returns the directory in which tempfiles should be opened. Defaults to +Dir.tmpdir+.
2018-01-22 17:09:13 -05:00
def tempdir #:doc:
Dir.tmpdir
end
end
end