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.

100 lines
3.6 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
  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):
  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. try:
  43. if base64.b64encode(base64.b64decode(value)) == value:
  44. lobject.write(base64.b64decode(value))
  45. else:
  46. lobject.write(value)
  47. except binascii.Error:
  48. lobject.write(value)
  49. elif isinstance(value, str):
  50. lobject.write(base64.b64decode(value.encode('ascii')))
  51. else:
  52. while True:
  53. chunk = value.read(4096)
  54. if not chunk:
  55. break
  56. lobject.write(chunk)
  57. return lobject.oid
  58. def convert_to_record(self, value, record):
  59. if value and isinstance(value, int):
  60. lobject = record.env.cr._cnx.lobject(value, 'rb')
  61. if record._context.get('human_size'):
  62. return human_size(lobject.seek(0, 2))
  63. elif record._context.get('bin_size'):
  64. return lobject.seek(0, 2)
  65. elif record._context.get('oid'):
  66. return lobject.oid
  67. elif record._context.get('base64'):
  68. return base64.b64encode(lobject.read())
  69. elif record._context.get('stream'):
  70. file = tempfile.TemporaryFile()
  71. while True:
  72. chunk = lobject.read(4096)
  73. if not chunk:
  74. file.seek(0)
  75. return file
  76. file.write(chunk)
  77. elif record._context.get('checksum'):
  78. checksum = hashlib.md5()
  79. while True:
  80. chunk = lobject.read(4096)
  81. if not chunk:
  82. return checksum.hexdigest()
  83. checksum.update(chunk)
  84. else:
  85. return lobject.read()
  86. return value
  87. def convert_to_export(self, value, record):
  88. if value:
  89. lobject = record.env.cr._cnx.lobject(value, 'rb')
  90. if record._context.get('export_raw_data'):
  91. return lobject.read()
  92. return base64.b64encode(lobject.read())
  93. return ''