Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] fs_attachment: allow serving field attachment with fs stream #419

Merged
merged 1 commit into from
Nov 10, 2024
Merged
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
27 changes: 18 additions & 9 deletions fs_attachment/models/ir_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,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 @@ -38,8 +36,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 @@ -101,6 +105,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 @@ -160,3 +161,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))
Loading