From b0b1dc5e0facb22346f3cb4fe1172fe9a3c6a709 Mon Sep 17 00:00:00 2001 From: Anna Janiszewska Date: Fri, 20 Oct 2023 11:35:50 +0000 Subject: [PATCH] [FIX] base: skip etag when attachment checksum is missing png images not shown on ir.attachment kanban Attachment that use db_datas have no checksum by default, which is used to compute the stream's http ETag. Skip updating the ETag when it is missing and determine freshness using the Last-Modified header instead. closes odoo/odoo#139498 X-original-commit: 2d43bbae72fddf17e7d9045e90dbc070b2f57a19 Signed-off-by: Julien Castiaux (juc) --- odoo/addons/base/models/ir_binary.py | 5 ++-- odoo/addons/test_http/tests/test_static.py | 31 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/odoo/addons/base/models/ir_binary.py b/odoo/addons/base/models/ir_binary.py index f49119ba2d5..3a2d01b17ca 100644 --- a/odoo/addons/base/models/ir_binary.py +++ b/odoo/addons/base/models/ir_binary.py @@ -220,13 +220,14 @@ class IrBinary(models.AbstractModel): if (width, height) == (0, 0): width, height = image_guess_size_from_field_name(field_name) - stream.etag += f'-{width}x{height}-crop={crop}-quality={quality}' + if isinstance(stream.etag, str): + stream.etag += f'-{width}x{height}-crop={crop}-quality={quality}' if isinstance(stream.last_modified, (int, float)): stream.last_modified = datetime.utcfromtimestamp(stream.last_modified) modified = werkzeug.http.is_resource_modified( request.httprequest.environ, - etag=stream.etag, + etag=stream.etag if isinstance(stream.etag, str) else None, last_modified=stream.last_modified ) diff --git a/odoo/addons/test_http/tests/test_static.py b/odoo/addons/test_http/tests/test_static.py index 6181023612f..4c946cd4d56 100644 --- a/odoo/addons/test_http/tests/test_static.py +++ b/odoo/addons/test_http/tests/test_static.py @@ -280,6 +280,37 @@ class TestHttpStatic(TestHttpStaticCommon): res = self.url_open('/web/content/test_http.earth?field=galaxy_picture') self.assertEqual(res.status_code, 404) + def test_static17_content_missing_checksum(self): + att = self.env['ir.attachment'].create({ + 'name': 'testhttp.txt', + 'db_datas': 'some data', + 'public': True, + }) + self.assertFalse(att.checksum) + self.assertDownload( + url=f'/web/content/{att.id}', + headers={}, + + assert_status_code=200, + assert_headers={ + 'Content-Length': '9', + 'Content-Type': 'text/plain; charset=utf-8', + 'Content-Disposition': 'inline; filename=testhttp.txt', + }, + assert_content=b'some data', + ) + + def test_static18_image_missing_checksum(self): + with file_open('test_http/static/src/img/gizeh.png', 'rb') as file: + att = self.env['ir.attachment'].create({ + 'name': 'gizeh.png', + 'db_datas': file.read(), + 'mimetype': 'image/png', + 'public': True, + }) + self.assertFalse(att.checksum) + self.assertDownloadGizeh(f'/web/image/{att.id}') + @tagged('post_install', '-at_install') class TestHttpStaticLogo(TestHttpStaticCommon):