@@ -172,42 +172,13 @@ def build_request_body(header_params, form_params, body)
172
172
data
173
173
end
174
174
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
182
175
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
-
204
176
@stream = [ ]
205
177
206
178
# handle streaming Responses
207
179
request . options . on_data = Proc . new do |chunk , overall_received_bytes |
208
180
@stream << chunk
209
181
end
210
-
211
182
end
212
183
213
184
# Check if the given MIME is a JSON MIME.
@@ -232,7 +203,17 @@ def deserialize(response, return_type)
232
203
# handle file downloading - return the File instance processed in request callbacks
233
204
# note that response body is empty when the file is written in chunks in request on_body callback
234
205
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 ) )
236
217
@tempfile . close
237
218
@config . logger . info "Temp file written to #{ @tempfile . path } , please copy the file to a proper folder " \
238
219
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file " \
0 commit comments