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.

97 lines
3.5 KiB

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