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.

94 lines
3.5 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. import binascii
  23. import tempfile
  24. from odoo import fields
  25. from odoo.tools import human_size
  26. _logger = logging.getLogger(__name__)
  27. class LargeObject(fields.Field):
  28. type = 'lobject'
  29. column_type = ('oid', 'oid')
  30. _slots = {
  31. 'prefetch': False,
  32. 'context_dependent': True,
  33. }
  34. def convert_to_column(self, value, record, values=None, validate=True):
  35. oid = record.with_context({'oid': True})[self.name]
  36. if oid:
  37. record.env.cr._cnx.lobject(oid, 'rb').unlink()
  38. if not value:
  39. return None
  40. lobject = record.env.cr._cnx.lobject(0, 'wb')
  41. if isinstance(value, bytes):
  42. lobject.write(value)
  43. elif isinstance(value, str):
  44. lobject.write(base64.b64decode(value))
  45. else:
  46. while True:
  47. chunk = value.read(4096)
  48. if not chunk:
  49. break
  50. lobject.write(chunk)
  51. return lobject.oid
  52. def convert_to_record(self, value, record):
  53. if value and isinstance(value, int):
  54. lobject = record.env.cr._cnx.lobject(value, 'rb')
  55. if record._context.get('human_size'):
  56. return human_size(lobject.seek(0, 2))
  57. elif record._context.get('bin_size'):
  58. return lobject.seek(0, 2)
  59. elif record._context.get('oid'):
  60. return lobject.oid
  61. elif record._context.get('base64'):
  62. return base64.b64encode(lobject.read())
  63. elif record._context.get('stream'):
  64. file = tempfile.TemporaryFile()
  65. while True:
  66. chunk = lobject.read(4096)
  67. if not chunk:
  68. file.seek(0)
  69. return file
  70. file.write(chunk)
  71. elif record._context.get('checksum'):
  72. checksum = hashlib.sha1()
  73. while True:
  74. chunk = lobject.read(4096)
  75. if not chunk:
  76. return checksum.hexdigest()
  77. checksum.update(chunk)
  78. else:
  79. return lobject.read()
  80. return value
  81. def convert_to_export(self, value, record):
  82. if value:
  83. lobject = record.env.cr._cnx.lobject(value, 'rb')
  84. if record._context.get('export_raw_data'):
  85. return lobject.read()
  86. return base64.b64encode(lobject.read())
  87. return ''