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.

132 lines
4.9 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Binary Stream Support
  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 io
  23. import logging
  24. import mimetypes
  25. from odoo import models
  26. _logger = logging.getLogger(__name__)
  27. class IrHttp(models.AbstractModel):
  28. _inherit = "ir.http"
  29. # ----------------------------------------------------------
  30. # Helper
  31. # ----------------------------------------------------------
  32. def _check_streamable(record, field):
  33. return False
  34. def _stream_content(
  35. self,
  36. record,
  37. field="content",
  38. filename=None,
  39. filename_field="content_fname",
  40. default_mimetype="application/octet-stream",
  41. ):
  42. if self._check_streamable(record, field):
  43. mimetype = "mimetype" in record and record.mimetype or False
  44. filehash = "checksum" in record and record["checksum"] or False
  45. stream = record.with_context({"stream": True})[field] or io.BytesIO()
  46. if not filename:
  47. if filename_field in record:
  48. filename = record[filename_field]
  49. if not filename:
  50. filename = "{}-{}-{}".format(record._name, record.id, field)
  51. if not mimetype and filename:
  52. mimetype = mimetypes.guess_type(filename)[0]
  53. if not mimetype:
  54. mimetype = default_mimetype
  55. _, existing_extension = os.path.splitext(filename)
  56. if not existing_extension:
  57. extension = mimetypes.guess_extension(mimetype)
  58. if extension:
  59. filename = "{}{}".format(filename, extension)
  60. if not filehash and stream:
  61. filehash = record.with_context({"checksum": True})[field]
  62. return stream and 200 or 404, stream, filename, mimetype, filehash
  63. return (404, [], None)
  64. # ----------------------------------------------------------
  65. # Functions
  66. # ----------------------------------------------------------
  67. def binary_stream(
  68. self,
  69. xmlid=None,
  70. model=None,
  71. id=None,
  72. field="content",
  73. unique=False,
  74. filename=None,
  75. filename_field="content_fname",
  76. download=False,
  77. mimetype=None,
  78. default_mimetype="application/octet-stream",
  79. access_token=None,
  80. ):
  81. """ Get file, attachment or downloadable content
  82. If the ``xmlid`` and ``id`` parameter is omitted, fetches the default value for the
  83. binary field (via ``default_get``), otherwise fetches the field for
  84. that precise record.
  85. :param str xmlid: xmlid of the record
  86. :param str model: name of the model to fetch the binary from
  87. :param int id: id of the record from which to fetch the binary
  88. :param str field: binary field
  89. :param bool unique: add a max-age for the cache control
  90. :param str filename: choose a filename
  91. :param str filename_field: if not create an filename with model-id-field
  92. :param bool download: apply headers to download the file
  93. :param str mimetype: mintype of the field (for headers)
  94. :param str default_mimetype: default mintype if no mintype found
  95. :param str access_token: optional token for unauthenticated access
  96. :returns: (status, headers, content)
  97. """
  98. record, status = self._get_record_and_check(
  99. xmlid=xmlid, model=model, id=id, field=field, access_token=access_token
  100. )
  101. if not record:
  102. return (status or 404, [], None)
  103. status, stream, filename, mimetype, filehash = self._stream_content(
  104. record,
  105. field=field,
  106. filename=filename,
  107. filename_field=filename_field,
  108. default_mimetype="application/octet-stream",
  109. )
  110. status, headers, stream = self._binary_set_headers(
  111. status,
  112. stream,
  113. filename,
  114. mimetype,
  115. unique,
  116. filehash=filehash,
  117. download=download,
  118. )
  119. return status, headers, stream