forked from loafle/openapi-generator-original
[Ruby][faraday] fix download_file (#7842)
* Fix download_file * Generate samples with f1df6acdee6 * fixup! Fix download_file * Generate samples with 3e3ea88b0dec7763c8
This commit is contained in:
parent
d5a7102785
commit
46b36c2e65
@ -71,7 +71,17 @@ module {{moduleName}}
|
||||
{{/isFaraday}}
|
||||
{{#isFaraday}}
|
||||
if return_type == 'File'
|
||||
@tempfile.write(@stream)
|
||||
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 = body.encoding
|
||||
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||
@tempfile.write(@stream.join.force_encoding(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 "\
|
||||
|
@ -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
|
||||
|
@ -172,42 +172,13 @@ module Petstore
|
||||
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
|
||||
|
||||
# Check if the given MIME is a JSON MIME.
|
||||
@ -232,7 +203,17 @@ module Petstore
|
||||
# handle file downloading - return the File instance processed in request callbacks
|
||||
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||
if return_type == 'File'
|
||||
@tempfile.write(@stream)
|
||||
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 = body.encoding
|
||||
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||
@tempfile.write(@stream.join.force_encoding(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 "\
|
||||
|
Loading…
x
Reference in New Issue
Block a user