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.

88 lines
3.6 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import base64
  20. import logging
  21. from odoo.addons.iap import jsonrpc
  22. from odoo.addons.muk_utils.tools.cache import memoize
  23. from odoo.addons.muk_utils.tools.file import guess_extension
  24. _logger = logging.getLogger(__name__)
  25. CONVERTER_DEFAULT_ENDPOINT = 'https://iap-converter.mukit.at'
  26. CONVERTER_ENDPOINT_FORMATS = '/iap/converter/1/formats'
  27. CONVERTER_ENDPOINT_IMPORTS = '/iap/converter/1/imports'
  28. CONVERTER_ENDPOINT_CONVERT = '/iap/converter/1/convert'
  29. class RemoteConverter(object):
  30. def __init__(self, env):
  31. self.params = env['ir.config_parameter'].sudo()
  32. self.account = env['iap.account'].get('muk_converter')
  33. def endpoint(self, route):
  34. return "%s%s" % (self.params.get_param('muk_converter.endpoint', CONVERTER_DEFAULT_ENDPOINT), route)
  35. def payload(self, params={}):
  36. params.update({
  37. 'account_token': self.account.account_token,
  38. 'database_uuid': self.params.get_param('database.uuid'),
  39. })
  40. return params
  41. @property
  42. @memoize(timeout=3600)
  43. def formats(self):
  44. return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_FORMATS), params=self.payload())
  45. @property
  46. @memoize(timeout=3600)
  47. def imports(self):
  48. return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_IMPORTS), params=self.payload())
  49. def convert(self, binary, mimetype=None, filename=None, export="binary", doctype="document", format="pdf"):
  50. """ Converts a binary value to the given format.
  51. :param binary: The binary value.
  52. :param mimetype: The mimetype of the binary value.
  53. :param filename: The filename of the binary value.
  54. :param export: The output format (binary, file, base64).
  55. :param doctype: Specify the document type (document, graphics, presentation, spreadsheet).
  56. :param format: Specify the output format for the document.
  57. :return: Returns the output depending on the given format.
  58. :raises ValueError: The file extension could not be determined or the format is invalid.
  59. """
  60. params = {
  61. 'format': format,
  62. 'doctype': doctype,
  63. 'mimetype': mimetype,
  64. 'filename': filename,
  65. 'content': base64.b64encode(binary),
  66. }
  67. result = jsonrpc(self.endpoint(CONVERTER_ENDPOINT_CONVERT), params=self.payload(params))
  68. if export == 'base64':
  69. return result
  70. if export == 'file':
  71. output = io.BytesIO()
  72. output.write(base64.b64decode(result))
  73. output.close()
  74. return output
  75. else:
  76. return base64.b64decode(result)