Skip to content

Commit 3e3ea88

Browse files
committed
fixup! Fix download_file
1 parent d5c7e15 commit 3e3ea88

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ module {{moduleName}}
7171
{{/isFaraday}}
7272
{{#isFaraday}}
7373
if return_type == 'File'
74-
@tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding)
75-
@tempfile.write(@stream.join.force_encoding(body.encoding))
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))
7685
@tempfile.close
7786
@config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
7887
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\

0 commit comments

Comments
 (0)