From 32b301bdb42dcc64f5dd0c593aff0edc4c5780bd Mon Sep 17 00:00:00 2001 From: Asko Soukka Date: Thu, 23 Apr 2020 21:30:29 +0300 Subject: [PATCH 1/4] Fix image scaling to return the originals when no scaling is required --- plone/namedfile/scaling.py | 4 ++++ plone/namedfile/tests/test_scaling.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/plone/namedfile/scaling.py b/plone/namedfile/scaling.py index 97d7c41e..aabe38df 100644 --- a/plone/namedfile/scaling.py +++ b/plone/namedfile/scaling.py @@ -218,6 +218,10 @@ def __call__( if height is None and width is None: dummy, format_ = orig_value.contentType.split("/", 1) return None, format_, (orig_value._width, orig_value._height) + elif height == orig_value._height and width == orig_value._width: + if not parameters: + dummy, format_ = orig_value.contentType.split("/", 1) + return orig_value, format_, (orig_value._width, orig_value._height) orig_data = None try: orig_data = orig_value.open() diff --git a/plone/namedfile/tests/test_scaling.py b/plone/namedfile/tests/test_scaling.py index c044d962..a31c644d 100644 --- a/plone/namedfile/tests/test_scaling.py +++ b/plone/namedfile/tests/test_scaling.py @@ -85,6 +85,22 @@ def testCreateScale(self): self.assertEqual(foo.height, 80) assertImage(self, foo.data.data, 'PNG', (80, 80)) + def testCreateExactScale(self): + foo = self.scaling.scale('image', width=100, height=80) + self.assertIsNot(foo.data, self.item.image) + + # test that exact scale without parameters returns original + foo = self.scaling.scale('image', + width=self.item.image._width, + height=self.item.image._height) + self.assertIs(foo.data, self.item.image) + + foo = self.scaling.scale('image', + width=self.item.image._width, + height=self.item.image._height, + quality=80) + self.assertIsNot(foo.data, self.item.image) + def testCreateHighPixelDensityScale(self): self.scaling.getHighPixelDensityScales = lambda: [{'scale': 2, 'quality': 66}] foo = self.scaling.scale('image', width=100, height=80) From 942d34d120d3cd0b0a96e45f09cf486e2847bb38 Mon Sep 17 00:00:00 2001 From: Asko Soukka Date: Thu, 23 Apr 2020 21:36:54 +0300 Subject: [PATCH 2/4] Add changelog entry --- news/92.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/92.bugfix diff --git a/news/92.bugfix b/news/92.bugfix new file mode 100644 index 00000000..44b5211f --- /dev/null +++ b/news/92.bugfix @@ -0,0 +1 @@ +- Fix image scaling to re-use the original image when scaling is not required to allow Plone REST API to use cacheable scale URL for the original image without performance penalty [datakurre] From 5a4a41d1d3a746d601c5cf3e88397a1ba983bf9f Mon Sep 17 00:00:00 2001 From: Asko Soukka Date: Thu, 23 Apr 2020 23:35:22 +0300 Subject: [PATCH 3/4] Fix test failure in plone.formwidget.namedfile, where image bytes were directly saved as image value --- plone/namedfile/scaling.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plone/namedfile/scaling.py b/plone/namedfile/scaling.py index aabe38df..d7cd7a4a 100644 --- a/plone/namedfile/scaling.py +++ b/plone/namedfile/scaling.py @@ -218,10 +218,11 @@ def __call__( if height is None and width is None: dummy, format_ = orig_value.contentType.split("/", 1) return None, format_, (orig_value._width, orig_value._height) - elif height == orig_value._height and width == orig_value._width: - if not parameters: - dummy, format_ = orig_value.contentType.split("/", 1) - return orig_value, format_, (orig_value._width, orig_value._height) + elif not parameters and height and width \ + and height == getattr(orig_value, "_height", None) \ + and width == getattr(orig_value, "_width", None): + dummy, format_ = orig_value.contentType.split("/", 1) + return orig_value, format_, (orig_value._width, orig_value._height) orig_data = None try: orig_data = orig_value.open() From a68a6e63a320adedd7f91ba64560c963b24144c8 Mon Sep 17 00:00:00 2001 From: Asko Soukka Date: Fri, 24 Apr 2020 18:11:35 +0300 Subject: [PATCH 4/4] Update changelog item --- news/92.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/92.bugfix b/news/92.bugfix index 44b5211f..8713023d 100644 --- a/news/92.bugfix +++ b/news/92.bugfix @@ -1 +1 @@ -- Fix image scaling to re-use the original image when scaling is not required to allow Plone REST API to use cacheable scale URL for the original image without performance penalty [datakurre] +Fix image scaling to re-use the original image when scaling is not required to allow Plone REST API to use cacheable scale URL for the original image without performance penalty [datakurre]