Fix download_file

This commit is contained in:
Tomohiro Suwa
2020-10-30 21:36:14 +09:00
parent 70323adf9f
commit f1df6acdee
2 changed files with 2 additions and 30 deletions

View File

@@ -71,7 +71,8 @@ module {{moduleName}}
{{/isFaraday}}
{{#isFaraday}}
if return_type == 'File'
@tempfile.write(@stream)
@tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding)
@tempfile.write(@stream.join.force_encoding(body.encoding))
@tempfile.close
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\

View File

@@ -128,40 +128,11 @@
data
end
# Save response body into a file in (the defined) temporary folder, using the filename
# from the "Content-Disposition" header if provided, otherwise a random filename.
# The response body is written to the file in chunks in order to handle files which
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
# process can use.
#
# @see Configuration#temp_folder_path
def download_file(request)
tempfile = nil
encoding = nil
request.headers do |response|
content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
prefix = sanitize_filename(filename)
else
prefix = 'download-'
end
prefix = prefix + '-' unless prefix.end_with?('-')
encoding = response.body.encoding
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
@tempfile = tempfile
end
if tempfile.nil?
tempfile = Tempfile.open('download-', @config.temp_folder_path)
@tempfile = tempfile
end
@stream = []
# handle streaming Responses
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
@stream << chunk
end
end