Throw error if images.Filter.run cannot create output (#8508)

If Filter.run is requested to create an unknown output format,
it should not fail silently without writing any output, but instead
throw an exception.

Fixes #8503
pull/8315/head
Christian Franke 2022-05-10 14:20:15 +02:00 zatwierdzone przez Matt Westcott
rodzic 7d8d900d10
commit 7d6240a489
7 zmienionych plików z 42 dodań i 2 usunięć

Wyświetl plik

@ -12,6 +12,7 @@ Changelog
* Use `InlinePanel`'s label when available for field comparison label (Sandil Ranasinghe)
* Drop support for Safari 13 by removing left/right positioning in favour of CSS logical properties (Thibaud Colas)
* Fix: Typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer)
* Fix: Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke)
3.0 (xx.xx.xxxx) - IN DEVELOPMENT

Wyświetl plik

@ -596,6 +596,7 @@ Contributors
* Simon Krull
* Przemysław Buczkowski
* Josh Woodcock
* Christian Franke
Translators
===========

Wyświetl plik

@ -22,6 +22,7 @@ depth: 1
* Fix issue where `ModelAdmin` index listings with export list enabled would show buttons with an incorrect layout (Josh Woodcock)
* Fix typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer)
* Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke)
## Upgrade considerations

Wyświetl plik

@ -1,2 +1,10 @@
class InvalidFilterSpecError(ValueError):
pass
class UnknownOutputImageFormatError(ValueError):
"""
Exception thrown when an unknown output format is requested for an image conversion
"""
pass

Wyświetl plik

@ -25,7 +25,10 @@ from willow.image import Image as WillowImage
from wagtail import hooks
from wagtail.admin.models import get_object_usage
from wagtail.coreutils import string_to_ascii
from wagtail.images.exceptions import InvalidFilterSpecError
from wagtail.images.exceptions import (
InvalidFilterSpecError,
UnknownOutputImageFormatError,
)
from wagtail.images.image_operations import (
FilterOperation,
ImageTransform,
@ -677,6 +680,9 @@ class Filter:
quality = getattr(settings, "WAGTAILIMAGES_WEBP_QUALITY", 85)
return willow.save_as_webp(output, quality=quality)
raise UnknownOutputImageFormatError(
f"Unknown output image format '{output_format}'"
)
def get_cache_key(self, image):
vary_parts = []

Wyświetl plik

@ -5,12 +5,16 @@ from django.test import TestCase, override_settings
from wagtail import hooks
from wagtail.images import image_operations
from wagtail.images.exceptions import InvalidFilterSpecError
from wagtail.images.exceptions import (
InvalidFilterSpecError,
UnknownOutputImageFormatError,
)
from wagtail.images.image_operations import TransformOperation
from wagtail.images.models import Filter, Image
from wagtail.images.tests.utils import (
get_test_image_file,
get_test_image_file_jpeg,
get_test_image_file_tiff,
get_test_image_file_webp,
)
@ -599,6 +603,18 @@ class TestFilter(TestCase):
self.assertEqual(run_mock.call_count, 2)
class TestUnknownOutputImageFormat(TestCase):
@hooks.register_temporarily(
"register_image_operations", register_image_operations_hook
)
def test_run_raises_error(self):
fil = Filter(spec="operation1|operation2")
image = Image.objects.create(
title="Test image", file=get_test_image_file_tiff()
)
self.assertRaises(UnknownOutputImageFormatError, fil.run, image, BytesIO())
class TestFormatFilter(TestCase):
def test_jpeg(self):
fil = Filter(spec="width-400|format-jpeg")

Wyświetl plik

@ -27,3 +27,10 @@ def get_test_image_file_webp(filename="test.webp", colour="white", size=(640, 48
image = PIL.Image.new("RGB", size, colour)
image.save(f, "WEBP")
return ImageFile(f, name=filename)
def get_test_image_file_tiff(filename="test.tiff", colour="white", size=(640, 480)):
f = BytesIO()
image = PIL.Image.new("RGB", size, colour)
image.save(f, "TIFF")
return ImageFile(f, name=filename)