ruby: save file in response type

This commit is contained in:
suxa
2023-03-20 15:13:38 +05:00
committed by Sergey Linnik
parent d9a79e20d7
commit be1616d6b6
3 changed files with 11 additions and 8 deletions

View File

@ -102,7 +102,7 @@ class HomeController < ApplicationController
if DocumentHelper.convert_exts.include? (extension) # check if the file with such an extension can be converted if DocumentHelper.convert_exts.include? (extension) # check if the file with such an extension can be converted
key = ServiceConverter.generate_revision_id(file_uri) # generate document key key = ServiceConverter.generate_revision_id(file_uri) # generate document key
percent, new_file_uri = ServiceConverter.get_converted_uri(file_uri, extension.delete('.'), internal_extension.delete('.'), key, true, file_pass, lang) # get the url of the converted file and the conversion percentage percent, new_file_uri, new_file_type = ServiceConverter.get_converted_data(file_uri, extension.delete('.'), internal_extension.delete('.'), key, true, file_pass, lang) # get the url and file type of the converted file and the conversion percentage
# if the conversion isn't completed, write file name and step values to the response # if the conversion isn't completed, write file name and step values to the response
if percent != 100 if percent != 100
@ -111,7 +111,7 @@ class HomeController < ApplicationController
end end
# get the correct file name if such a name already exists # get the correct file name if such a name already exists
correct_name = DocumentHelper.get_correct_name(File.basename(file_name, extension) + internal_extension, nil) correct_name = DocumentHelper.get_correct_name(File.basename(file_name, extension) + "." + new_file_type, nil)
uri = URI.parse(new_file_uri) # create the request url uri = URI.parse(new_file_uri) # create the request url
http = Net::HTTP.new(uri.host, uri.port) # create a connection to the http server http = Net::HTTP.new(uri.host, uri.port) # create a connection to the http server

View File

@ -22,7 +22,7 @@ class ServiceConverter
class << self class << self
# get the url of the converted file # get the url of the converted file
def get_converted_uri(document_uri, from_ext, to_ext, document_revision_id, is_async, file_pass, lang = nil) def get_converted_data(document_uri, from_ext, to_ext, document_revision_id, is_async, file_pass, lang = nil)
from_ext = from_ext == nil ? File.extname(document_uri).downcase : from_ext # get the current document extension from_ext = from_ext == nil ? File.extname(document_uri).downcase : from_ext # get the current document extension
@ -81,7 +81,7 @@ class ServiceConverter
end end
json_data = JSON.parse(data) # parse response body json_data = JSON.parse(data) # parse response body
return get_response_uri(json_data) # get response url return get_response_data(json_data) # get response url
end end
# generate the document key value # generate the document key value
@ -132,7 +132,7 @@ class ServiceConverter
end end
# get the response url # get the response url
def get_response_uri(json_data) def get_response_data(json_data)
file_result = json_data file_result = json_data
@ -145,16 +145,19 @@ class ServiceConverter
result_percent = 0 # the conversion percentage result_percent = 0 # the conversion percentage
response_uri = '' response_uri = ''
response_file_type = ''
if is_end_convert # if the conversion is completed if is_end_convert # if the conversion is completed
file_url_element = file_result['fileUrl'] file_url_element = file_result['fileUrl']
file_type_element = file_result['fileType']
if file_url_element == nil # and the file url doesn't exist if file_url_element == nil # and the file url doesn't exist
raise 'Invalid answer format' # get ann error message raise 'Invalid answer format' # get ann error message
end end
response_uri = file_url_element # otherwise, get the file url response_uri = file_url_element # otherwise, get the file url
response_file_type = file_type_element # get the file type
result_percent = 100 result_percent = 100
else # if the conversion isn't completed else # if the conversion isn't completed
@ -169,7 +172,7 @@ class ServiceConverter
end end
return result_percent, response_uri return result_percent, response_uri, response_file_type
end end
end end

View File

@ -77,7 +77,7 @@ class TrackHelper
unless cur_ext.eql?(download_ext) unless cur_ext.eql?(download_ext)
key = ServiceConverter.generate_revision_id(download_uri) # get the document key key = ServiceConverter.generate_revision_id(download_uri) # get the document key
begin begin
percent, new_file_uri = ServiceConverter.get_converted_uri(download_uri, download_ext.delete('.'), cur_ext.delete('.'), key, false, nil) # get the url of the converted file percent, new_file_uri, new_file_type = ServiceConverter.get_converted_data(download_uri, download_ext.delete('.'), cur_ext.delete('.'), key, false, nil) # get the url of the converted file
if new_file_uri == nil || new_file_uri.empty? if new_file_uri == nil || new_file_uri.empty?
new_file_name = DocumentHelper.get_correct_name(File.basename(file_name, cur_ext) + download_ext, user_address) # get the correct file name if it already exists new_file_name = DocumentHelper.get_correct_name(File.basename(file_name, cur_ext) + download_ext, user_address) # get the correct file name if it already exists
else else
@ -156,7 +156,7 @@ class TrackHelper
unless cur_ext.eql?(download_ext) unless cur_ext.eql?(download_ext)
key = ServiceConverter.generate_revision_id(download_uri) # get the document key key = ServiceConverter.generate_revision_id(download_uri) # get the document key
begin begin
percent, new_file_uri = ServiceConverter.get_converted_uri(download_uri, download_ext.delete('.'), cur_ext.delete('.'), key, false, nil) # get the url of the converted file percent, new_file_uri, new_file_type = ServiceConverter.get_converted_data(download_uri, download_ext.delete('.'), cur_ext.delete('.'), key, false, nil) # get the url of the converted file
if new_file_uri == nil || new_file_uri.empty? if new_file_uri == nil || new_file_uri.empty?
new_file_name = true new_file_name = true
else else