diff --git a/fs_attachment/models/ir_binary.py b/fs_attachment/models/ir_binary.py index 65ff04fd6e..ef7e2e5bb9 100644 --- a/fs_attachment/models/ir_binary.py +++ b/fs_attachment/models/ir_binary.py @@ -44,6 +44,34 @@ def _record_to_stream(self, record, field_name): return FsStream.from_fs_attachment(fs_attachment) return super()._record_to_stream(record, field_name) + def _get_stream_from( + self, + record, + field_name="raw", + filename=None, + filename_field="name", + mimetype=None, + default_mimetype="application/octet-stream", + ): + stream = super()._get_stream_from( + record, + field_name=field_name, + filename=filename, + filename_field=filename_field, + mimetype=mimetype, + default_mimetype=default_mimetype, + ) + + if stream.type == "fs": + if mimetype: + stream.mimetype = mimetype + if filename: + stream.download_name = filename + elif record and filename_field in record: + stream.download_name = record[filename_field] or stream.download_name + + return stream + def _get_image_stream_from( self, record, diff --git a/fs_attachment/tests/test_stream.py b/fs_attachment/tests/test_stream.py index c7172877bf..f06acec6b1 100644 --- a/fs_attachment/tests/test_stream.py +++ b/fs_attachment/tests/test_stream.py @@ -108,6 +108,17 @@ def test_content_url(self): }, assert_content=self.content, ) + url = f"/web/content/{self.attachment_binary.id}/?filename=test2.txt&mimetype=text/csv" + self.assertDownload( + url, + headers={}, + assert_status_code=200, + assert_headers={ + "Content-Type": "text/csv; charset=utf-8", + "Content-Disposition": "inline; filename=test2.txt", + }, + assert_content=self.content, + ) def test_image_url(self): self.authenticate("admin", "admin")