Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion fs_attachment/fs_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from __future__ import annotations

from typing import TYPE_CHECKING

from odoo.http import STATIC_CACHE_LONG, Response, Stream, request

from .models.ir_attachment import IrAttachment
if TYPE_CHECKING:
from .models.ir_attachment import IrAttachment

try:
from werkzeug.utils import secure_filename
Expand Down
7 changes: 7 additions & 0 deletions fs_attachment/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from odoo.exceptions import AccessError, UserError
from odoo.fields import Domain

from ..fs_stream import FsStream
from .strtobool import strtobool

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -370,6 +371,12 @@ def _set_attachment_data(self, asbytes) -> None: # pylint: disable=missing-retu
super()._set_attachment_data(asbytes)
self._enforce_meaningful_storage_filename()

def _to_http_stream(self):
self.ensure_one()
if self.fs_filename:
return FsStream.from_fs_attachment(self)
return super()._to_http_stream()

##############################################
# Internal methods to use the object storage #
##############################################
Expand Down
11 changes: 11 additions & 0 deletions fs_attachment/tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ def assertDownload(
self.assertEqual(res.content, assert_content, "Wong content")
return res

def test_to_http_stream(self):
from ..fs_stream import FsStream

stream = self.attachment_binary._to_http_stream()
self.assertIsInstance(stream, FsStream)
self.assertEqual(stream.mimetype, "text/plain")

stream = self.attachment_image._to_http_stream()
self.assertIsInstance(stream, FsStream)
self.assertEqual(stream.mimetype, "image/png")

def test_content_url(self):
self.authenticate("admin", "admin")
url = f"/web/content/{self.attachment_binary.id}"
Expand Down
Loading