class OnlyofficeDocumentserverConversionHelper::ConvertFileData
Examples¶ ↑
converter = ConvertFileData.new
('server') converter.perform_convert('files/googerd.docx') converter.perform_convert({:url=>'files/googerd.docx'}) converter.perform_convert({:url=>'files/googerd.docx',
:outputtype => 'pdf'})
Constants
- DOCUMENT_EXTENSIONS
@return [Array<String>] list of text formats
- PRESENTATION_EXTENSIONS
@return [Array<String>] list of presentation formats
- SPREADSHEET_EXTENSIONS
@return [Array<String>] list of spreadsheet formats
Attributes
@return [String] file_url
to convert
@return [String] input_filetype
format
@return [String] key for convert operation
@return [String] output_file_type
format
Public Class Methods
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 35 def initialize(server_path, jwt_key: 'jwt_key', jwt_header: 'AuthorizationJwt', jwt_prefix: 'Bearer', timeout: 300) @server_path = server_path @jwt_key = jwt_key @jwt_header = jwt_header @jwt_prefix = jwt_prefix @timeout = timeout end
Public Instance Methods
Add jwt data to request @param [Net::HTTP::Post] request to add data @return [Net::HTTP::Post] request with JWT
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 94 def add_jwt_data(request) payload_to_encode = { 'payload' => '{}' } jwt_encoded = JWT.encode payload_to_encode, @jwt_key request[@jwt_header] = "#{@jwt_prefix} #{jwt_encoded}" end
Complete missing params @param [Hash] params manually defined @return [Hash] filled params hash
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 74 def autocomplete_missing_params(params) params[:key] = key_auto unless params.key?(:key) params[:outputtype] = output_file_type_auto unless params.key?(:outputtype) params[:filetype] = input_filetype unless params.key?(:filetype) params end
@return [String] convert service url
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 67 def convert_url "#{@server_path}/ConvertService.ashx" end
@return [String] with url to result file @param [String] data is a response body @param [String] file_format is a format of result file Method will get link from response body. Link start from 'https', and end from result file format
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 86 def get_url_from_responce(data, file_format) res_result = /(http|https).*(#{file_format})/.match(data) CGI.unescapeHTML(res_result.to_s) end
Get input file name from url @return [String] result file name
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 62 def input_filetype File.extname(@file_url).delete('.') end
@return [String] random generated key
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 56 def key_auto SecureRandom.uuid end
Auto detect output file format @return [String] result format
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 49 def output_file_type_auto return 'docx' if DOCUMENT_EXTENSIONS.include?(@input_filetype.upcase) return 'xlsx' if SPREADSHEET_EXTENSIONS.include?(@input_filetype.upcase) return 'pptx' if PRESENTATION_EXTENSIONS.include?(@input_filetype.upcase) end
@return [Hash] with usl for download file after conversion and response data @param [Hash] args collect all parameters of request
OR [String] if you not need to use advenced params
and want to set all etc params automaticly. All args, except of :url and if it is [Hash], will be attache in end of request
Examples¶ ↑
perform_convert
('example.com/filename.docx') perform_convert
({:url => 'example.com/filename.docx'}) perform_convert
({:url => 'google.com/filename.docx',
:key=>'askjdhaskdasdasdi', :outputtype => 'pdf'})
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 142 def perform_convert(args = {}) args = { url: args } if args.is_a?(String) raise 'Parameter :url with link on file is necessary!!' if args[:url].nil? || args.nil? @file_url = args[:url] @input_filetype = File.extname(@file_url).delete('.') advanced_params = autocomplete_missing_params(args) data = request(convert_url, advanced_params) url = get_url_from_responce(data, advanced_params[:outputtype]) { url: url, data: data } end
Make request @param [String] convert_url
to call @param [Hash] params with options @return [String] body of responce
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 104 def request(convert_url, params) uri = URI(convert_url) req = Net::HTTP::Post.new(uri) req.body = params.to_json add_jwt_data(req) http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = @timeout http.use_ssl = true if uri.scheme == 'https' send_request(http, req) end
sending request every 5 second within @timeout responce will contain 504 if return responce body
# File lib/onlyoffice_documentserver_conversion_helper.rb, line 118 def send_request(http, req) Timeout.timeout(@timeout) do (@timeout / 5).times do responce = http.request(req) return responce.body unless responce.code == '504' sleep 5 end end end