Class: OnlyofficeDocumentserverConversionHelper::ConvertFileData

Inherits:
Object
  • Object
show all
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.

Returns:

  • (Array<String>)

    list of text formats

%w[TXT HTML HTM ODT DOCT DOCX RTF DOC PDF].freeze
SPREADSHEET_EXTENSIONS =

Returns list of spreadsheet formats.

Returns:

  • (Array<String>)

    list of spreadsheet formats

%w[XLS XLSX ODS XLST].freeze
PRESENTATION_EXTENSIONS =

Returns list of presentation formats.

Returns:

  • (Array<String>)

    list of presentation formats

%w[PPT PPTX PPTT ODP].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlString

Returns file_url to convert.

Returns:

  • (String)

    file_url to convert



22
23
24
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 22

def file_url
  @file_url
end

#input_filetypeString

Get input file name from url

Returns:

  • (String)

    result file name



67
68
69
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 67

def input_filetype
  File.extname(@file_url).delete('.')
end

#keyString

Returns key for convert operation.

Returns:

  • (String)

    key for convert operation



24
25
26
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 24

def key
  @key
end

#output_file_typeString

Returns output_file_type format.

Returns:

  • (String)

    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

Parameters:

  • request (Net::HTTP::Post)

    to add data

Returns:

  • (Net::HTTP::Post)

    request with JWT



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

Parameters:

  • params (Hash)

    manually defined

Returns:

  • (Hash)

    filled params hash



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_urlString

Returns convert service url.

Returns:

  • (String)

    convert service url



72
73
74
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 72

def convert_url
  "#{@server_path}/ConvertService.ashx"
end

#key_autoString

Returns random generated key.

Returns:

  • (String)

    random generated key



61
62
63
# File 'lib/onlyoffice_documentserver_conversion_helper.rb', line 61

def key_auto
  SecureRandom.uuid
end

#output_file_type_autoString

Auto detect output file format

Returns:

  • (String)

    result format

Raises:



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')

Parameters:

  • args (Hash, Srting) (defaults to: {})

    collect all parameters of request OR string if you not need to use advenced params

Returns:

  • (Hash)

    with usl for download file



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

Parameters:

  • convert_url (String)

    to call

  • params (Hash)

    with options

Returns:

  • (String)

    body of responce



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