Skip to content

Commit

Permalink
[Ruby][faraday] fix download_file (#7842)
Browse files Browse the repository at this point in the history
* Fix download_file

* Generate samples with f1df6ac

* fixup! Fix download_file

* Generate samples with 3e3ea88
  • Loading branch information
tsuwatch committed Oct 31, 2020
1 parent d5a7102 commit 46b36c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 11 additions & 30 deletions samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,42 +172,13 @@ def build_request_body(header_params, form_params, body)
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.
Expand All @@ -232,7 +203,17 @@ def deserialize(response, return_type)
# 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 "\
Expand Down

0 comments on commit 46b36c2

Please sign in to comment.