Class: OnlyofficeDocumentserverConversionHelper::ConvertFileData
- Inherits:
-
Object
- Object
- OnlyofficeDocumentserverConversionHelper::ConvertFileData
- Defined in:
- lib/onlyoffice_documentserver_conversion_helper.rb
Overview
Examples
converter = ConvertFileData.new(‘server’) converter.perform_convert(‘files/googerd.docx’) converter.perform_convert(href="http://files/googerd.docx">files/googerd.docx’) converter.perform_convert(href="http://files/googerd.docx">files/googerd.docx’,
:outputtype => 'pdf')
Constant Summary collapse
- DOCUMENT_EXTENSIONS =
Returns list of text formats.
%w[TXT HTML HTM ODT DOCT DOCX RTF DOC PDF].freeze
- SPREADSHEET_EXTENSIONS =
Returns list of spreadsheet formats.
%w[XLS XLSX ODS XLST].freeze
- PRESENTATION_EXTENSIONS =
Returns list of presentation formats.
%w[PPT PPTX PPTT ODP].freeze
Instance Attribute Summary collapse
-
#file_url ⇒ String
File_url to convert.
-
#input_filetype ⇒ String
Get input file name from url.
-
#key ⇒ String
Key for convert operation.
-
#output_file_type ⇒ String
Output_file_type format.
Instance Method Summary collapse
-
#add_jwt_data(request) ⇒ Net::HTTP::Post
Add jwt data to request.
-
#autocomplete_missing_params(params) ⇒ Hash
Complete missing params.
-
#convert_url ⇒ String
Convert service url.
-
#initialize(server_path, jwt_key: 'jwt_key', jwt_header: 'AuthorizationJwt', jwt_prefix: 'Bearer', timeout: 300) ⇒ ConvertFileData
constructor
A new instance of ConvertFileData.
-
#key_auto ⇒ String
Random generated key.
-
#output_file_type_auto ⇒ String
Auto detect output file format.
-
#perform_convert(args = {}) ⇒ Hash
after conversion and response data and want to set all etc params automaticly.
-
#request(convert_url, params) ⇒ String
Make request.
-
#send_request(http, req) ⇒ Object
sending request every 5 second within @timeout responce will contain 504 if return responce body.
Constructor Details
#initialize(server_path, jwt_key: 'jwt_key', jwt_header: 'AuthorizationJwt', jwt_prefix: 'Bearer', timeout: 300) ⇒ ConvertFileData
Returns a new instance of ConvertFileData.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 37 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 |
Instance Attribute Details
#file_url ⇒ String
Returns file_url to convert.
22 23 24 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 22 def file_url @file_url end |
#input_filetype ⇒ String
Get input file name from url
67 68 69 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 67 def input_filetype File.extname(@file_url).delete('.') end |
#key ⇒ String
Returns key for convert operation.
24 25 26 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 24 def key @key end |
#output_file_type ⇒ String
Returns output_file_type format.
28 29 30 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 28 def output_file_type @output_file_type end |
Instance Method Details
#add_jwt_data(request) ⇒ Net::HTTP::Post
Add jwt data to request
89 90 91 92 93 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 89 def add_jwt_data(request) payload_to_encode = { 'payload' => JSON.parse(request.body) } jwt_encoded = JWT.encode payload_to_encode, @jwt_key request[@jwt_header] = "#{@jwt_prefix} #{jwt_encoded}" end |
#autocomplete_missing_params(params) ⇒ Hash
Complete missing params
79 80 81 82 83 84 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 79 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 |
#convert_url ⇒ String
Returns convert service url.
72 73 74 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 72 def convert_url "#{@server_path}/ConvertService.ashx" end |
#key_auto ⇒ String
Returns random generated key.
61 62 63 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 61 def key_auto SecureRandom.uuid end |
#output_file_type_auto ⇒ String
Auto detect output file format
52 53 54 55 56 57 58 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 52 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) raise UnknownConvertFormatError, "Unknown convert auto format: #{@input_filetype}" end |
#perform_convert(args = {}) ⇒ Hash
after conversion and response data 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(=> ‘example.com/filename.docx’) perform_convert(=> ‘google.com/filename.docx’,
:key=>'askjdhaskdasdasdi',
:outputtype => 'pdf')
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 137 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 = XmlResponceParser.new(data, advanced_params[:outputtype], result_in_zip: result_in_zip?(advanced_params)).result_url { url: url, data: data } end |
#request(convert_url, params) ⇒ String
Make request
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 99 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 = (uri.scheme == 'https') send_request(http, req) end |
#send_request(http, req) ⇒ Object
sending request every 5 second within @timeout responce will contain 504 if return responce body
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 113 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 |