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.

98 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 hashlib
  21. import logging
  22. from odoo import api, models, fields, SUPERUSER_ID
  23. from odoo.addons.muk_converter.service.unoconv import UnoconvConverter
  24. from odoo.addons.muk_converter.service.provider import RemoteConverter
  25. _logger = logging.getLogger(__name__)
  26. class Converter(models.AbstractModel):
  27. _name = 'muk_converter.converter'
  28. _description = 'Converter'
  29. #----------------------------------------------------------
  30. # Functions
  31. #----------------------------------------------------------
  32. @api.model
  33. def formats(self):
  34. return self._provider().formats
  35. @api.model
  36. def imports(self):
  37. return self._provider().imports
  38. @api.model
  39. def convert(self, filename, content, format="pdf", recompute=False, store=True):
  40. binary_content = base64.b64decode(content)
  41. output = self.convert_raw(filename, binary_content, format=format, recompute=recompute, store=store)
  42. return base64.b64encode(output)
  43. @api.model
  44. def convert_raw(self, filename, binary_content, format="pdf", recompute=False, store=True):
  45. checksum = hashlib.sha1(binary_content).hexdigest()
  46. stored = self._retrieve(checksum, format)
  47. if not recompute and stored.exists():
  48. return stored.content
  49. else:
  50. name = "%s.%s" % (filename, format)
  51. output = self._parse(filename, binary_content, format)
  52. if store:
  53. self._store(checksum, name, output, format, stored)
  54. return output
  55. #----------------------------------------------------------
  56. # Helper
  57. #----------------------------------------------------------
  58. @api.model
  59. def _provider(self):
  60. params = self.env['ir.config_parameter'].sudo()
  61. service = params.get_param('muk_converter.service')
  62. if service == 'unoconv':
  63. return UnoconvConverter()
  64. else:
  65. return RemoteConverter(env=self.env)
  66. @api.model
  67. def _parse(self, filename, content, format):
  68. return self._provider().convert(content, filename=filename, format=format)
  69. @api.model
  70. def _retrieve(self, checksum, format):
  71. domain = [["checksum", "=", checksum], ["format", "=", format]]
  72. return self.env['muk_converter.store'].sudo().search(domain, limit=1)
  73. @api.model
  74. def _store(self, checksum, filename, content, format, stored):
  75. if stored and stored.exists():
  76. stored.write({'used_date': fields.Datetime.now})
  77. else:
  78. self.env['muk_converter.store'].sudo().create({
  79. 'checksum': checksum,
  80. 'format': format,
  81. 'content_fname': filename,
  82. 'content': content})