forked from loafle/openapi-generator-original
[Ruby] Fix Content-Transfer-Encoding binary unpacking (#19132)
This commit is contained in:
parent
2940d3219c
commit
8938f9dea1
@ -117,35 +117,41 @@
|
|||||||
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
|
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
|
||||||
stream << chunk
|
stream << chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
stream
|
stream
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_file(response, stream)
|
def deserialize_file(response, stream)
|
||||||
body = response.body
|
body = response.body
|
||||||
if @config.return_binary_data == true
|
encoding = body.encoding
|
||||||
# return byte stream
|
|
||||||
encoding = body.encoding
|
# reconstruct content
|
||||||
stream.join.force_encoding(encoding)
|
content = stream.join
|
||||||
|
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
|
||||||
|
content = content.force_encoding(encoding)
|
||||||
|
|
||||||
|
# return byte stream
|
||||||
|
return content if @config.return_binary_data == true
|
||||||
|
|
||||||
|
# return file instead of binary data
|
||||||
|
content_disposition = response.headers['Content-Disposition']
|
||||||
|
if content_disposition && content_disposition =~ /filename=/i
|
||||||
|
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||||
|
prefix = sanitize_filename(filename)
|
||||||
else
|
else
|
||||||
# return file instead of binary data
|
prefix = 'download-'
|
||||||
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 = body.encoding
|
|
||||||
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
|
||||||
tempfile.write(stream.join.force_encoding(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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
tempfile
|
|
||||||
end
|
end
|
||||||
|
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||||
|
|
||||||
|
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||||
|
tempfile.write(content)
|
||||||
|
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`"
|
||||||
|
tempfile
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection(opts)
|
def connection(opts)
|
||||||
|
@ -164,35 +164,41 @@ module OpenapiClient
|
|||||||
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
|
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
|
||||||
stream << chunk
|
stream << chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
stream
|
stream
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_file(response, stream)
|
def deserialize_file(response, stream)
|
||||||
body = response.body
|
body = response.body
|
||||||
if @config.return_binary_data == true
|
encoding = body.encoding
|
||||||
# return byte stream
|
|
||||||
encoding = body.encoding
|
# reconstruct content
|
||||||
stream.join.force_encoding(encoding)
|
content = stream.join
|
||||||
|
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
|
||||||
|
content = content.force_encoding(encoding)
|
||||||
|
|
||||||
|
# return byte stream
|
||||||
|
return content if @config.return_binary_data == true
|
||||||
|
|
||||||
|
# return file instead of binary data
|
||||||
|
content_disposition = response.headers['Content-Disposition']
|
||||||
|
if content_disposition && content_disposition =~ /filename=/i
|
||||||
|
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||||
|
prefix = sanitize_filename(filename)
|
||||||
else
|
else
|
||||||
# return file instead of binary data
|
prefix = 'download-'
|
||||||
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 = body.encoding
|
|
||||||
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
|
||||||
tempfile.write(stream.join.force_encoding(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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
tempfile
|
|
||||||
end
|
end
|
||||||
|
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||||
|
|
||||||
|
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||||
|
tempfile.write(content)
|
||||||
|
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`"
|
||||||
|
tempfile
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection(opts)
|
def connection(opts)
|
||||||
|
@ -164,35 +164,41 @@ module Petstore
|
|||||||
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
|
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
|
||||||
stream << chunk
|
stream << chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
stream
|
stream
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_file(response, stream)
|
def deserialize_file(response, stream)
|
||||||
body = response.body
|
body = response.body
|
||||||
if @config.return_binary_data == true
|
encoding = body.encoding
|
||||||
# return byte stream
|
|
||||||
encoding = body.encoding
|
# reconstruct content
|
||||||
stream.join.force_encoding(encoding)
|
content = stream.join
|
||||||
|
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
|
||||||
|
content = content.force_encoding(encoding)
|
||||||
|
|
||||||
|
# return byte stream
|
||||||
|
return content if @config.return_binary_data == true
|
||||||
|
|
||||||
|
# return file instead of binary data
|
||||||
|
content_disposition = response.headers['Content-Disposition']
|
||||||
|
if content_disposition && content_disposition =~ /filename=/i
|
||||||
|
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
||||||
|
prefix = sanitize_filename(filename)
|
||||||
else
|
else
|
||||||
# return file instead of binary data
|
prefix = 'download-'
|
||||||
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 = body.encoding
|
|
||||||
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
|
||||||
tempfile.write(stream.join.force_encoding(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 "\
|
|
||||||
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
|
|
||||||
"explicitly with `tempfile.delete`"
|
|
||||||
tempfile
|
|
||||||
end
|
end
|
||||||
|
prefix = prefix + '-' unless prefix.end_with?('-')
|
||||||
|
|
||||||
|
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
|
||||||
|
tempfile.write(content)
|
||||||
|
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`"
|
||||||
|
tempfile
|
||||||
end
|
end
|
||||||
|
|
||||||
def connection(opts)
|
def connection(opts)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user