forked from loafle/openapi-generator-original
[Ruby][faraday] fix response streaming (#7734)
* test file return * fix stream response * use options * use local var * fix nil tempfile * fix tempfile * fix tempfile * use stream * check content * open temp file * test ruby file download * fix stream data * test faraday * catch connection error * catch Faraday::ConnectionFailed * catch all error * use sream * refactor * fi download file in faraday * local fix * fi streaming download * undo changess to spec, test * undo changes to spec
This commit is contained in:
parent
37743c059b
commit
fe38a50365
@ -66,7 +66,20 @@ module {{moduleName}}
|
|||||||
|
|
||||||
# handle file downloading - return the File instance processed in request callbacks
|
# 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
|
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||||
|
{{^isFaraday}}
|
||||||
return @tempfile if return_type == 'File'
|
return @tempfile if return_type == 'File'
|
||||||
|
{{/isFaraday}}
|
||||||
|
{{#isFaraday}}
|
||||||
|
if return_type == 'File'
|
||||||
|
@tempfile.write(@stream)
|
||||||
|
@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 "\
|
||||||
|
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||||
|
"explicitly with `tempfile.delete`"
|
||||||
|
return @tempfile
|
||||||
|
end
|
||||||
|
{{/isFaraday}}
|
||||||
|
|
||||||
return nil if body.nil? || body.empty?
|
return nil if body.nil? || body.empty?
|
||||||
|
|
||||||
@ -131,44 +144,6 @@ module {{moduleName}}
|
|||||||
end
|
end
|
||||||
end
|
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.on_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
|
|
||||||
request.on_body do |chunk|
|
|
||||||
chunk.force_encoding(encoding)
|
|
||||||
tempfile.write(chunk)
|
|
||||||
end
|
|
||||||
request.on_complete do |response|
|
|
||||||
if tempfile
|
|
||||||
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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sanitize filename by removing path.
|
# Sanitize filename by removing path.
|
||||||
# e.g. ../../sun.gif becomes sun.gif
|
# e.g. ../../sun.gif becomes sun.gif
|
||||||
#
|
#
|
||||||
|
@ -127,3 +127,41 @@
|
|||||||
end
|
end
|
||||||
data
|
data
|
||||||
end
|
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
|
||||||
|
@ -113,3 +113,41 @@
|
|||||||
end
|
end
|
||||||
data
|
data
|
||||||
end
|
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.on_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
|
||||||
|
request.on_body do |chunk|
|
||||||
|
chunk.force_encoding(encoding)
|
||||||
|
tempfile.write(chunk)
|
||||||
|
end
|
||||||
|
request.on_complete do |response|
|
||||||
|
if tempfile
|
||||||
|
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 "\
|
||||||
|
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||||
|
"explicitly with `tempfile.delete`"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
1
pom.xml
1
pom.xml
@ -1192,6 +1192,7 @@
|
|||||||
<!-- clients -->
|
<!-- clients -->
|
||||||
<!--<module>samples/client/petstore/perl</module>
|
<!--<module>samples/client/petstore/perl</module>
|
||||||
<module>samples/client/petstore/bash</module>-->
|
<module>samples/client/petstore/bash</module>-->
|
||||||
|
<module>samples/client/petstore/ruby-faraday</module>
|
||||||
<module>samples/client/petstore/ruby</module>
|
<module>samples/client/petstore/ruby</module>
|
||||||
<module>samples/client/petstore/c</module>
|
<module>samples/client/petstore/c</module>
|
||||||
<module>samples/client/petstore/cpp-qt5</module>
|
<module>samples/client/petstore/cpp-qt5</module>
|
||||||
|
@ -172,6 +172,44 @@ module Petstore
|
|||||||
data
|
data
|
||||||
end
|
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.
|
# Check if the given MIME is a JSON MIME.
|
||||||
# JSON MIME examples:
|
# JSON MIME examples:
|
||||||
# application/json
|
# application/json
|
||||||
@ -193,7 +231,15 @@ module Petstore
|
|||||||
|
|
||||||
# handle file downloading - return the File instance processed in request callbacks
|
# 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
|
# note that response body is empty when the file is written in chunks in request on_body callback
|
||||||
return @tempfile if return_type == 'File'
|
if return_type == 'File'
|
||||||
|
@tempfile.write(@stream)
|
||||||
|
@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 "\
|
||||||
|
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||||
|
"explicitly with `tempfile.delete`"
|
||||||
|
return @tempfile
|
||||||
|
end
|
||||||
|
|
||||||
return nil if body.nil? || body.empty?
|
return nil if body.nil? || body.empty?
|
||||||
|
|
||||||
@ -258,44 +304,6 @@ module Petstore
|
|||||||
end
|
end
|
||||||
end
|
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.on_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
|
|
||||||
request.on_body do |chunk|
|
|
||||||
chunk.force_encoding(encoding)
|
|
||||||
tempfile.write(chunk)
|
|
||||||
end
|
|
||||||
request.on_complete do |response|
|
|
||||||
if tempfile
|
|
||||||
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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sanitize filename by removing path.
|
# Sanitize filename by removing path.
|
||||||
# e.g. ../../sun.gif becomes sun.gif
|
# e.g. ../../sun.gif becomes sun.gif
|
||||||
#
|
#
|
||||||
|
@ -156,6 +156,44 @@ module Petstore
|
|||||||
data
|
data
|
||||||
end
|
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.on_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
|
||||||
|
request.on_body do |chunk|
|
||||||
|
chunk.force_encoding(encoding)
|
||||||
|
tempfile.write(chunk)
|
||||||
|
end
|
||||||
|
request.on_complete do |response|
|
||||||
|
if tempfile
|
||||||
|
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 "\
|
||||||
|
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||||
|
"explicitly with `tempfile.delete`"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Check if the given MIME is a JSON MIME.
|
# Check if the given MIME is a JSON MIME.
|
||||||
# JSON MIME examples:
|
# JSON MIME examples:
|
||||||
# application/json
|
# application/json
|
||||||
@ -242,44 +280,6 @@ module Petstore
|
|||||||
end
|
end
|
||||||
end
|
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.on_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
|
|
||||||
request.on_body do |chunk|
|
|
||||||
chunk.force_encoding(encoding)
|
|
||||||
tempfile.write(chunk)
|
|
||||||
end
|
|
||||||
request.on_complete do |response|
|
|
||||||
if tempfile
|
|
||||||
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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sanitize filename by removing path.
|
# Sanitize filename by removing path.
|
||||||
# e.g. ../../sun.gif becomes sun.gif
|
# e.g. ../../sun.gif becomes sun.gif
|
||||||
#
|
#
|
||||||
|
@ -155,6 +155,44 @@ module DynamicServers
|
|||||||
data
|
data
|
||||||
end
|
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.on_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
|
||||||
|
request.on_body do |chunk|
|
||||||
|
chunk.force_encoding(encoding)
|
||||||
|
tempfile.write(chunk)
|
||||||
|
end
|
||||||
|
request.on_complete do |response|
|
||||||
|
if tempfile
|
||||||
|
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 "\
|
||||||
|
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||||
|
"explicitly with `tempfile.delete`"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Check if the given MIME is a JSON MIME.
|
# Check if the given MIME is a JSON MIME.
|
||||||
# JSON MIME examples:
|
# JSON MIME examples:
|
||||||
# application/json
|
# application/json
|
||||||
@ -241,44 +279,6 @@ module DynamicServers
|
|||||||
end
|
end
|
||||||
end
|
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.on_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
|
|
||||||
request.on_body do |chunk|
|
|
||||||
chunk.force_encoding(encoding)
|
|
||||||
tempfile.write(chunk)
|
|
||||||
end
|
|
||||||
request.on_complete do |response|
|
|
||||||
if tempfile
|
|
||||||
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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sanitize filename by removing path.
|
# Sanitize filename by removing path.
|
||||||
# e.g. ../../sun.gif becomes sun.gif
|
# e.g. ../../sun.gif becomes sun.gif
|
||||||
#
|
#
|
||||||
|
@ -155,6 +155,44 @@ module Petstore
|
|||||||
data
|
data
|
||||||
end
|
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.on_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
|
||||||
|
request.on_body do |chunk|
|
||||||
|
chunk.force_encoding(encoding)
|
||||||
|
tempfile.write(chunk)
|
||||||
|
end
|
||||||
|
request.on_complete do |response|
|
||||||
|
if tempfile
|
||||||
|
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 "\
|
||||||
|
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
||||||
|
"explicitly with `tempfile.delete`"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Check if the given MIME is a JSON MIME.
|
# Check if the given MIME is a JSON MIME.
|
||||||
# JSON MIME examples:
|
# JSON MIME examples:
|
||||||
# application/json
|
# application/json
|
||||||
@ -241,44 +279,6 @@ module Petstore
|
|||||||
end
|
end
|
||||||
end
|
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.on_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
|
|
||||||
request.on_body do |chunk|
|
|
||||||
chunk.force_encoding(encoding)
|
|
||||||
tempfile.write(chunk)
|
|
||||||
end
|
|
||||||
request.on_complete do |response|
|
|
||||||
if tempfile
|
|
||||||
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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sanitize filename by removing path.
|
# Sanitize filename by removing path.
|
||||||
# e.g. ../../sun.gif becomes sun.gif
|
# e.g. ../../sun.gif becomes sun.gif
|
||||||
#
|
#
|
||||||
|
Loading…
x
Reference in New Issue
Block a user