mirror of https://github.com/muk-it/muk_base
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.6 KiB
89 lines
3.6 KiB
###################################################################################
|
|
#
|
|
# Copyright (C) 2018 MuK IT GmbH
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
###################################################################################
|
|
|
|
import base64
|
|
import logging
|
|
|
|
from odoo.addons.iap import jsonrpc
|
|
|
|
from odoo.addons.muk_utils.tools.cache import memoize
|
|
from odoo.addons.muk_utils.tools.file import guess_extension
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
CONVERTER_DEFAULT_ENDPOINT = 'https://iap-converter.mukit.at'
|
|
CONVERTER_ENDPOINT_FORMATS = '/iap/converter/1/formats'
|
|
CONVERTER_ENDPOINT_IMPORTS = '/iap/converter/1/imports'
|
|
CONVERTER_ENDPOINT_CONVERT = '/iap/converter/1/convert'
|
|
|
|
class RemoteConverter(object):
|
|
|
|
def __init__(self, env):
|
|
self.params = env['ir.config_parameter'].sudo()
|
|
self.account = env['iap.account'].get('muk_converter')
|
|
|
|
def endpoint(self, route):
|
|
return "%s%s" % (self.params.get_param('muk_converter.endpoint', CONVERTER_DEFAULT_ENDPOINT), route)
|
|
|
|
def payload(self, params={}):
|
|
params.update({
|
|
'account_token': self.account.account_token,
|
|
'database_uuid': self.params.get_param('database.uuid'),
|
|
})
|
|
return params
|
|
|
|
@property
|
|
@memoize(timeout=3600)
|
|
def formats(self):
|
|
return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_FORMATS), params=self.payload())
|
|
|
|
@property
|
|
@memoize(timeout=3600)
|
|
def imports(self):
|
|
return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_IMPORTS), params=self.payload())
|
|
|
|
def convert(self, binary, mimetype=None, filename=None, export="binary", doctype="document", format="pdf"):
|
|
""" Converts a binary value to the given format.
|
|
|
|
:param binary: The binary value.
|
|
:param mimetype: The mimetype of the binary value.
|
|
:param filename: The filename of the binary value.
|
|
:param export: The output format (binary, file, base64).
|
|
:param doctype: Specify the document type (document, graphics, presentation, spreadsheet).
|
|
:param format: Specify the output format for the document.
|
|
:return: Returns the output depending on the given format.
|
|
:raises ValueError: The file extension could not be determined or the format is invalid.
|
|
"""
|
|
params = {
|
|
'format': format,
|
|
'doctype': doctype,
|
|
'mimetype': mimetype,
|
|
'filename': filename,
|
|
'content': base64.b64encode(binary),
|
|
}
|
|
result = jsonrpc(self.endpoint(CONVERTER_ENDPOINT_CONVERT), params=self.payload(params))
|
|
if export == 'base64':
|
|
return result
|
|
if export == 'file':
|
|
output = io.BytesIO()
|
|
output.write(base64.b64decode(result))
|
|
output.close()
|
|
return output
|
|
else:
|
|
return base64.b64decode(result)
|