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.

93 lines
3.4 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 unoconv
  24. from odoo.addons.muk_converter.service.provider import provider
  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. checksum = hashlib.sha1(binary_content).hexdigest()
  42. stored = self._retrieve(checksum, format)
  43. if not recompute and stored.exists():
  44. return base64.b64encode(stored.content)
  45. else:
  46. name = "%s.%s" % (filename, format)
  47. output = self._parse(filename, binary_content, format)
  48. if store:
  49. self._store(checksum, name, output, format, stored)
  50. return base64.b64encode(output)
  51. #----------------------------------------------------------
  52. # Helper
  53. #----------------------------------------------------------
  54. @api.model
  55. def _provider(self):
  56. params = self.env['ir.config_parameter'].sudo()
  57. service = params.get_param('muk_converter.service')
  58. if service == 'unoconv':
  59. return unoconv
  60. else:
  61. provider.env = self.env
  62. return provider
  63. @api.model
  64. def _parse(self, filename, content, format):
  65. return self._provider().convert(content, filename=filename, format=format)
  66. @api.model
  67. def _retrieve(self, checksum, format):
  68. domain = [["checksum", "=", checksum], ["format", "=", format]]
  69. return self.env['muk_converter.store'].sudo().search(domain, limit=1)
  70. @api.model
  71. def _store(self, checksum, filename, content, format, stored):
  72. if stored and stored.exists():
  73. stored.write({'used_date': fields.Datetime.now})
  74. else:
  75. self.env['muk_converter.store'].sudo().create({
  76. 'checksum': checksum,
  77. 'format': format,
  78. 'content_fname': filename,
  79. 'content': content})