From 82bcfd027832f3e0a8c50199b213b7769765609d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Mon, 15 Jul 2024 14:34:41 +0200 Subject: [PATCH] [IMP] fs_attachment: send the mimetype information for S3 --- fs_attachment/models/ir_attachment.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fs_attachment/models/ir_attachment.py b/fs_attachment/models/ir_attachment.py index 60c42929a0..5c0df628ee 100644 --- a/fs_attachment/models/ir_attachment.py +++ b/fs_attachment/models/ir_attachment.py @@ -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 # @@ -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"]} + _fs = getattr(_fs, "fs", None) + return {} + @api.model def _storage_file_write(self, bin_data: bytes) -> str: """Write the file to the filesystem storage""" @@ -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