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.

96 lines
3.4 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 hashlib
  24. import logging
  25. from odoo import api, models, fields, SUPERUSER_ID
  26. from odoo.addons.muk_converter.service.unoconv import unoconv
  27. from odoo.addons.muk_converter.service.provider import provider
  28. _logger = logging.getLogger(__name__)
  29. class Converter(models.AbstractModel):
  30. _name = 'muk_converter.converter'
  31. _description = 'Converter'
  32. #----------------------------------------------------------
  33. # Functions
  34. #----------------------------------------------------------
  35. @api.model
  36. def formats(self):
  37. return self._provider().formats
  38. @api.model
  39. def imports(self):
  40. return self._provider().imports
  41. @api.model
  42. def convert(self, filename, content, format="pdf", recompute=False, store=True):
  43. binary_content = base64.b64decode(content)
  44. checksum = hashlib.sha1(binary_content).hexdigest()
  45. stored = self._retrieve(checksum, format)
  46. if not recompute and stored.exists():
  47. return base64.b64encode(stored.content)
  48. else:
  49. name = "%s.%s" % (filename, format)
  50. output = self._parse(filename, binary_content, format)
  51. if store:
  52. self._store(checksum, name, output, format, stored)
  53. return base64.b64encode(output)
  54. #----------------------------------------------------------
  55. # Helper
  56. #----------------------------------------------------------
  57. @api.model
  58. def _provider(self):
  59. params = self.env['ir.config_parameter'].sudo()
  60. service = params.get_param('muk_converter.service')
  61. if service == 'unoconv':
  62. return unoconv
  63. else:
  64. provider.env = self.env
  65. return provider
  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})