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.

102 lines
3.9 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 cached_property
  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. @property
  31. def env(self):
  32. return self._params
  33. @env.setter
  34. def env(self, env):
  35. self._params = env['ir.config_parameter'].sudo()
  36. self._account = env['iap.account'].get('muk_converter')
  37. @property
  38. def params(self):
  39. return self._params
  40. @property
  41. def account(self):
  42. return self._account
  43. @cached_property(timeout=3600)
  44. def formats(self):
  45. print("FORMATS")
  46. return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_FORMATS), params=self.payload())
  47. @cached_property(timeout=3600)
  48. def imports(self):
  49. return jsonrpc(self.endpoint(CONVERTER_ENDPOINT_IMPORTS), params=self.payload())
  50. def endpoint(self, route):
  51. return "%s%s" % (self.params.get_param('muk_converter.endpoint', CONVERTER_DEFAULT_ENDPOINT), route)
  52. def payload(self, params={}):
  53. params.update({
  54. 'account_token': self.account.account_token,
  55. 'database_uuid': self.params.get_param('database.uuid'),
  56. })
  57. return params
  58. def convert(self, binary, mimetype=None, filename=None, export="binary", doctype="document", format="pdf"):
  59. """ Converts a binary value to the given format.
  60. :param binary: The binary value.
  61. :param mimetype: The mimetype of the binary value.
  62. :param filename: The filename of the binary value.
  63. :param export: The output format (binary, file, base64).
  64. :param doctype: Specify the document type (document, graphics, presentation, spreadsheet).
  65. :param format: Specify the output format for the document.
  66. :return: Returns the output depending on the given format.
  67. :raises ValueError: The file extension could not be determined or the format is invalid.
  68. """
  69. params = {
  70. 'format': format,
  71. 'doctype': doctype,
  72. 'mimetype': mimetype,
  73. 'filename': filename,
  74. 'content': base64.b64encode(binary),
  75. }
  76. result = jsonrpc(self.endpoint(CONVERTER_ENDPOINT_CONVERT), params=self.payload(params))
  77. if export == 'base64':
  78. return result
  79. if export == 'file':
  80. output = io.BytesIO()
  81. output.write(base64.b64decode(result))
  82. output.close()
  83. return output
  84. else:
  85. return base64.b64decode(result)
  86. provider = RemoteConverter()