onlyoffice_documentserver_conversion_helper 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cddd11a1a80052f9232c9d8f1e6a2eb3fc066ed94dc1c407ec437869126abd49
|
4
|
+
data.tar.gz: da14479675d910bca3de929b55d4e9633f5f5a149438bccf52ce47b57e2793d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4e053b7993029e73996d969771891fc201add85ccb1e20c7a2de55bdbf77415488196c64de9dd777c66ff97eb01bd9d2e635f2ed4e87f8027dfe806962db02c
|
7
|
+
data.tar.gz: 364db58bb120d6688738a4ecd77dfd47a4bdbca0d58ce597edff31a21f3619e9e466bf7413a99cf2c46cd24b1ced9bd05eb74d2fa3c350f967316ed6c803a55f
|
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# onlyoffice_documentserver_conversion_helper
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
require 'net/http'
|
3
|
+
require 'securerandom'
|
4
|
+
require 'timeout'
|
5
|
+
require_relative 'onlyoffice_documentserver_conversion_helper/version'
|
6
|
+
|
7
|
+
# Stuff for working with conversion service
|
8
|
+
# See: https://api.onlyoffice.com/editors/conversionapi
|
9
|
+
module OnlyofficeDocumentserverConversionHelper
|
10
|
+
# ==== Examples
|
11
|
+
# ConvertFileData.new('https://doc-linux.teamlab.info').perform_convert('http://testrail-nct.tk/files/convertation_select/googerd.docx')
|
12
|
+
# ConvertFileData.new('https://doc-linux.teamlab.info').perform_convert({:url=>'http://testrail-nct.tk/files/convertation_select/googerd.docx'})
|
13
|
+
# ConvertFileData.new('https://doc-linux.teamlab.info').perform_convert({:url=>'http://testrail-nct.tk/files/convertation_select/googerd.docx', :outputtype => 'pdf'})
|
14
|
+
class ConvertFileData
|
15
|
+
attr_accessor :file_url
|
16
|
+
attr_accessor :key
|
17
|
+
attr_writer :input_filetype
|
18
|
+
attr_accessor :output_file_type
|
19
|
+
|
20
|
+
DOCUMENT_EXTENSIONS = %w[TXT HTML HTM ODT DOCT DOCX RTF DOC PDF].freeze
|
21
|
+
SPREADSHEET_EXTENSIONS = %w[XLS XLSX ODS XLST].freeze
|
22
|
+
PRESENTATION_EXTENSIONS = %w[PPT PPTX PPTT ODP].freeze
|
23
|
+
|
24
|
+
def initialize(server_path,
|
25
|
+
jwt_key: 'jwt_key',
|
26
|
+
jwt_header: 'AuthorizationJwt',
|
27
|
+
jwt_prefix: 'Bearer',
|
28
|
+
timeout: 300)
|
29
|
+
@server_path = server_path
|
30
|
+
@jwt_key = jwt_key
|
31
|
+
@jwt_header = jwt_header
|
32
|
+
@jwt_prefix = jwt_prefix
|
33
|
+
@timeout = timeout
|
34
|
+
end
|
35
|
+
|
36
|
+
def output_file_type_auto
|
37
|
+
return 'docx' if DOCUMENT_EXTENSIONS.include?(@input_filetype.upcase)
|
38
|
+
return 'xlsx' if SPREADSHEET_EXTENSIONS.include?(@input_filetype.upcase)
|
39
|
+
return 'pptx' if PRESENTATION_EXTENSIONS.include?(@input_filetype.upcase)
|
40
|
+
end
|
41
|
+
|
42
|
+
def key_auto
|
43
|
+
SecureRandom.uuid
|
44
|
+
end
|
45
|
+
|
46
|
+
def input_filetype
|
47
|
+
File.extname(@file_url).delete('.')
|
48
|
+
end
|
49
|
+
|
50
|
+
def convert_url
|
51
|
+
"#{@server_path}/ConvertService.ashx"
|
52
|
+
end
|
53
|
+
|
54
|
+
def autocomplete_missing_params(params)
|
55
|
+
params[:key] = key_auto unless params.key?(:key)
|
56
|
+
params[:outputtype] = output_file_type_auto unless params.key?(:outputtype)
|
57
|
+
params[:filetype] = input_filetype unless params.key?(:filetype)
|
58
|
+
params
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [String] with url to result file
|
62
|
+
# @param [String] data is a response body
|
63
|
+
# @param [String] file_format is a format of result file
|
64
|
+
# Method will get link from response body. Link start from 'https', and end from result file format
|
65
|
+
def get_url_from_responce(data, file_format)
|
66
|
+
res_result = /(http|https).*(#{file_format})/.match(data)
|
67
|
+
CGI.unescapeHTML(res_result.to_s)
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_jwt_data(request)
|
71
|
+
payload_to_encode = { 'payload' => '{}' }
|
72
|
+
jwt_encoded = JWT.encode payload_to_encode, @jwt_key
|
73
|
+
request[@jwt_header] = "#{@jwt_prefix} #{jwt_encoded}"
|
74
|
+
end
|
75
|
+
|
76
|
+
def request(convert_url, params)
|
77
|
+
uri = URI(convert_url)
|
78
|
+
req = Net::HTTP::Post.new(uri)
|
79
|
+
req.body = params.to_json
|
80
|
+
add_jwt_data(req)
|
81
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
82
|
+
http.read_timeout = @timeout
|
83
|
+
http.use_ssl = true if uri.scheme == 'https'
|
84
|
+
send_request(http, req)
|
85
|
+
end
|
86
|
+
|
87
|
+
# sending request every 5 second within @timeout
|
88
|
+
# responce will contain 504 if
|
89
|
+
# return responce body
|
90
|
+
def send_request(http, req)
|
91
|
+
Timeout.timeout(@timeout) do
|
92
|
+
(@timeout / 5).times do
|
93
|
+
responce = http.request(req)
|
94
|
+
return responce.body unless responce.code == '504'
|
95
|
+
sleep 5
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# @return [Hash] with usl for download file after conversion and response data
|
101
|
+
# @param [Hash] args collect all parameters of request OR [String] if you not need to use advensed params and want to set all etc params automaticly.
|
102
|
+
# All args, except of :url and if it is [Hash], will be attache in end of request
|
103
|
+
# ==== Examples
|
104
|
+
# perform_convert('https://google.com/filename.docx')
|
105
|
+
# perform_convert({:url => 'https://google.com/filename.docx'})
|
106
|
+
# perform_convert({:url => 'https://google.com/filename.docx',
|
107
|
+
# :key=>'askjdhaskdasdasdi',
|
108
|
+
# :outputtype => 'pdf'})
|
109
|
+
def perform_convert(args = {})
|
110
|
+
args = { url: args } if args.is_a?(String)
|
111
|
+
raise 'Parameter :url with link on file is necessary!!' if args[:url].nil? || args.nil?
|
112
|
+
@file_url = args[:url]
|
113
|
+
@input_filetype = File.extname(@file_url).delete('.')
|
114
|
+
advanced_params = autocomplete_missing_params(args)
|
115
|
+
data = request(convert_url, advanced_params)
|
116
|
+
url = get_url_from_responce(data, advanced_params[:outputtype])
|
117
|
+
{ url: url, data: data }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: onlyoffice_documentserver_conversion_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel Lobashov
|
8
|
+
- Dmitry Rotaty
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-08-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: jwt
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '2'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '2'
|
28
|
+
description: Helper method for using ONLYOFFICE DocumentServer conversion api
|
29
|
+
email:
|
30
|
+
- shockwavenn@gmail.com
|
31
|
+
- flaminestone@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- README.md
|
37
|
+
- lib/onlyoffice_documentserver_conversion_helper.rb
|
38
|
+
- lib/onlyoffice_documentserver_conversion_helper/version.rb
|
39
|
+
homepage: http://rubygems.org/gems/onlyoffice_documentserver_conversion_helper
|
40
|
+
licenses:
|
41
|
+
- AGPL-3.0
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '2.1'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.7.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: onlyoffice_documentserver_conversion_helper Gem
|
63
|
+
test_files: []
|