Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ruby][faraday] fix download_file #7842

Merged
merged 4 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\
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|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not process this block.

content_disposition = response.headers['Content-Disposition']
if content_disposition && content_disposition =~ /filename=/i
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
wing328 marked this conversation as resolved.
Show resolved Hide resolved
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
32 changes: 2 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,8 @@ 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)
@tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding)
@tempfile.write(@stream.join.force_encoding(body.encoding))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stream is array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to force_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