Skip to content
Draft
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
19 changes: 16 additions & 3 deletions fs_attachment/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ def _storage(self):
storage = super()._storage()
return storage

@api.depends("store_fname", "db_datas")
def _compute_raw(self):
"""Always expose raw payload as bytes.

Some callers (e.g. account EDI helpers) slice the value returned by
``raw`` and crash when it is ``False`` for 0-byte attachments.
"""
res = super()._compute_raw()
false_attachments = self.filtered(lambda att: not att.raw)
false_attachments.raw = b""
return res

@api.model_create_multi
def create(self, vals_list):
"""
Expand Down Expand Up @@ -697,9 +709,10 @@ def _move_attachment_to_store(self):
self.ensure_one()
_logger.info("inspecting attachment %s (%d)", self.name, self.id)
fname = self.store_fname
storage = fname.partition("://")[0]
if self._is_storage_disabled(storage):
fname = False
if fname:
storage = fname.partition("://")[0]
if self._is_storage_disabled(storage):
fname = False
if fname:
# migrating from filesystem filestore
# or from the old 'store_fname' without the bucket name
Expand Down
6 changes: 6 additions & 0 deletions fs_attachment/tests/test_fs_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def test_create_attachment_with_meaningful_name(self):
with attachment.open("rb") as f:
self.assertEqual(f.read(), new_content)

def test_create_attachment_with_no_payload_has_bytes_raw(self):
attachment = self.ir_attachment_model.create({"name": "empty.txt"})

self.assertEqual(attachment.raw, b"")
self.assertEqual(attachment.file_size, 0)

def test_open_attachment_in_db(self):
self.env["ir.config_parameter"].sudo().set_param("ir_attachment.location", "db")
content = b"This is a test attachment in db"
Expand Down
Loading