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.

105 lines
3.8 KiB

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