Skip to content

Commit

Permalink
[17.0][FIX] fs_attachment: allow serving field attachment with fs stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kafai-lam authored and lmignon committed Nov 10, 2024
1 parent 81ef661 commit a223d28
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
27 changes: 18 additions & 9 deletions fs_attachment/models/ir_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
class IrBinary(models.AbstractModel):
_inherit = "ir.binary"

def _record_to_stream(self, record, field_name):
# Extend base implementation to support attachment stored into a
# filesystem storage
fs_attachment = None
def _get_fs_attachment_for_field(self, record, field_name):
if record._name == "ir.attachment" and record.fs_filename:
fs_attachment = record
return record

record.check_field_access_rights("read", [field_name])
field_def = record._fields[field_name]
if field_def.attachment and not field_def.compute and not field_def.related:
field_attachment = (
if field_def.attachment and field_def.store:
fs_attachment = (
self.env["ir.attachment"]
.sudo()
.search(
Expand All @@ -37,8 +35,14 @@ def _record_to_stream(self, record, field_name):
limit=1,
)
)
if field_attachment.fs_filename:
fs_attachment = field_attachment
if fs_attachment and fs_attachment.fs_filename:
return fs_attachment
return None

def _record_to_stream(self, record, field_name):
# Extend base implementation to support attachment stored into a
# filesystem storage
fs_attachment = self._get_fs_attachment_for_field(record, field_name)
if fs_attachment:
return FsStream.from_fs_attachment(fs_attachment)
return super()._record_to_stream(record, field_name)
Expand Down Expand Up @@ -100,6 +104,11 @@ def _get_image_stream_from(
if value:
record = value.attachment
field_name = "raw"
elif field_def.type in ("binary"):
fs_attachment = self._get_fs_attachment_for_field(record, field_name)
if fs_attachment:
record = fs_attachment
field_name = "raw"
stream = super()._get_image_stream_from(
record,
field_name=field_name,
Expand Down
29 changes: 29 additions & 0 deletions fs_attachment/tests/test_stream.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2023 ACSONE SA/NV (http://acsone.eu).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import base64
import io
import os
import shutil
Expand Down Expand Up @@ -163,3 +164,31 @@ def test_response_csp_header(self):
"Content-Security-Policy": "default-src 'none'",
},
)

def test_serving_field_image(self):
self.authenticate("admin", "admin")
demo_partner = self.env.ref("base.partner_demo")
demo_partner.with_context(
storage_location=self.temp_backend.code,
).write({"image_128": base64.encodebytes(self._create_image(128, 128))})
url = f"/web/image/{demo_partner._name}/{demo_partner.id}/image_128"
res = self.assertDownload(
url,
headers={},
assert_status_code=200,
assert_headers={
"Content-Type": "image/png",
},
)
self.assertEqual(Image.open(io.BytesIO(res.content)).size, (128, 128))

url = f"/web/image/{demo_partner._name}/{demo_partner.id}/avatar_128"
avatar_res = self.assertDownload(
url,
headers={},
assert_status_code=200,
assert_headers={
"Content-Type": "image/png",
},
)
self.assertEqual(Image.open(io.BytesIO(avatar_res.content)).size, (128, 128))

0 comments on commit a223d28

Please sign in to comment.