Skip to content

Commit 46b36c2

Browse files
authored
[Ruby][faraday] fix download_file (#7842)
* Fix download_file * Generate samples with f1df6ac * fixup! Fix download_file * Generate samples with 3e3ea88
1 parent d5a7102 commit 46b36c2

File tree

3 files changed

+22
-60
lines changed

3 files changed

+22
-60
lines changed

modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,17 @@ module {{moduleName}}
7171
{{/isFaraday}}
7272
{{#isFaraday}}
7373
if return_type == 'File'
74-
@tempfile.write(@stream)
74+
content_disposition = response.headers['Content-Disposition']
75+
if content_disposition && content_disposition =~ /filename=/i
76+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
77+
prefix = sanitize_filename(filename)
78+
else
79+
prefix = 'download-'
80+
end
81+
prefix = prefix + '-' unless prefix.end_with?('-')
82+
encoding = body.encoding
83+
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
84+
@tempfile.write(@stream.join.force_encoding(encoding))
7585
@tempfile.close
7686
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
7787
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\

modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -128,40 +128,11 @@
128128
data
129129
end
130130

131-
# Save response body into a file in (the defined) temporary folder, using the filename
132-
# from the "Content-Disposition" header if provided, otherwise a random filename.
133-
# The response body is written to the file in chunks in order to handle files which
134-
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
135-
# process can use.
136-
#
137-
# @see Configuration#temp_folder_path
138131
def download_file(request)
139-
tempfile = nil
140-
encoding = nil
141-
request.headers do |response|
142-
content_disposition = response.headers['Content-Disposition']
143-
if content_disposition && content_disposition =~ /filename=/i
144-
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
145-
prefix = sanitize_filename(filename)
146-
else
147-
prefix = 'download-'
148-
end
149-
prefix = prefix + '-' unless prefix.end_with?('-')
150-
encoding = response.body.encoding
151-
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
152-
@tempfile = tempfile
153-
end
154-
155-
if tempfile.nil?
156-
tempfile = Tempfile.open('download-', @config.temp_folder_path)
157-
@tempfile = tempfile
158-
end
159-
160132
@stream = []
161133

162134
# handle streaming Responses
163135
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
164136
@stream << chunk
165137
end
166-
167138
end

samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,42 +172,13 @@ def build_request_body(header_params, form_params, body)
172172
data
173173
end
174174

175-
# Save response body into a file in (the defined) temporary folder, using the filename
176-
# from the "Content-Disposition" header if provided, otherwise a random filename.
177-
# The response body is written to the file in chunks in order to handle files which
178-
# size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
179-
# process can use.
180-
#
181-
# @see Configuration#temp_folder_path
182175
def download_file(request)
183-
tempfile = nil
184-
encoding = nil
185-
request.headers do |response|
186-
content_disposition = response.headers['Content-Disposition']
187-
if content_disposition && content_disposition =~ /filename=/i
188-
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
189-
prefix = sanitize_filename(filename)
190-
else
191-
prefix = 'download-'
192-
end
193-
prefix = prefix + '-' unless prefix.end_with?('-')
194-
encoding = response.body.encoding
195-
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
196-
@tempfile = tempfile
197-
end
198-
199-
if tempfile.nil?
200-
tempfile = Tempfile.open('download-', @config.temp_folder_path)
201-
@tempfile = tempfile
202-
end
203-
204176
@stream = []
205177

206178
# handle streaming Responses
207179
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
208180
@stream << chunk
209181
end
210-
211182
end
212183

213184
# Check if the given MIME is a JSON MIME.
@@ -232,7 +203,17 @@ def deserialize(response, return_type)
232203
# handle file downloading - return the File instance processed in request callbacks
233204
# note that response body is empty when the file is written in chunks in request on_body callback
234205
if return_type == 'File'
235-
@tempfile.write(@stream)
206+
content_disposition = response.headers['Content-Disposition']
207+
if content_disposition && content_disposition =~ /filename=/i
208+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
209+
prefix = sanitize_filename(filename)
210+
else
211+
prefix = 'download-'
212+
end
213+
prefix = prefix + '-' unless prefix.end_with?('-')
214+
encoding = body.encoding
215+
@tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
216+
@tempfile.write(@stream.join.force_encoding(encoding))
236217
@tempfile.close
237218
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
238219
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\

0 commit comments

Comments
 (0)