Skip to content

Commit

Permalink
[IMP] fs_attachment: send the mimetype information for S3
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau authored and lmignon committed Nov 10, 2024
1 parent d75ceb5 commit 82bcfd0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions fs_attachment/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ def _get_datas_related_values(self, data, mimetype):
"db_datas": data,
}
return values
return super()._get_datas_related_values(data, mimetype)
return super(
IrAttachment, self.with_context(mimetype=mimetype)
)._get_datas_related_values(data, mimetype)

###########################################################
# Odoo methods that we override to use the object storage #
Expand Down Expand Up @@ -381,6 +383,14 @@ def _storage_file_read(self, fname: str) -> bytes | None:
)
return b""

def _storage_write_option(self, fs):
_fs = fs
while _fs:
if hasattr(_fs, "s3"):
return {"ContentType": self._context["mimetype"]}

Check warning on line 390 in fs_attachment/models/ir_attachment.py

View check run for this annotation

Codecov / codecov/patch

fs_attachment/models/ir_attachment.py#L390

Added line #L390 was not covered by tests
_fs = getattr(_fs, "fs", None)
return {}

@api.model
def _storage_file_write(self, bin_data: bytes) -> str:
"""Write the file to the filesystem storage"""
Expand All @@ -391,7 +401,8 @@ def _storage_file_write(self, bin_data: bytes) -> str:
if not fs.exists(dirname):
fs.makedirs(dirname)
fname = f"{storage}://{path}"
with fs.open(path, "wb") as f:
kwargs = self._storage_write_option(fs)
with fs.open(path, "wb", **kwargs) as f:
f.write(bin_data)
self._fs_mark_for_gc(fname)
return fname
Expand Down

0 comments on commit 82bcfd0

Please sign in to comment.