Fix image preview when Willow optimizers are enabled (#12047)

Avoids https://github.com/wagtail/Willow/issues/147
pull/12010/head
Alex Tomkins 2024-06-13 13:51:07 +02:00 zatwierdzone przez Jake Howard
rodzic 77791767a2
commit 5fe8da9921
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 8198AEBFA7E86782
4 zmienionych plików z 41 dodań i 3 usunięć

Wyświetl plik

@ -27,6 +27,7 @@ Changelog
* Fix: Add separators when displaying multiple error messages on a StructBlock (Kyle Bayliss)
* Fix: Specify `verbose_name` on `TranslatableMixin.locale` so that it is translated when used as a label (Romein van Buren)
* Fix: Disallow null characters in API filter values (Jochen Wersdörfer)
* Fix: Fix image preview when Willow optimizers are enabled (Alex Tomkins)
* Docs: Remove duplicate section on frontend caching proxies from performance page (Jake Howard)
* Docs: Document `restriction_type` field on PageViewRestriction (Shlomo Markowitz)
* Docs: Document Wagtail's bug bounty policy (Jake Howard)

Wyświetl plik

@ -41,6 +41,7 @@ depth: 1
* Add separators when displaying multiple error messages on a StructBlock (Kyle Bayliss)
* Specify `verbose_name` on `TranslatableMixin.locale` so that it is translated when used as a label (Romein van Buren)
* Disallow null characters in API filter values (Jochen Wersdörfer)
* Fix image preview when Willow optimizers are enabled (Alex Tomkins)
### Documentation

Wyświetl plik

@ -1,6 +1,7 @@
import datetime
import json
import urllib
from unittest.mock import patch
from django.conf import settings
from django.contrib.auth.models import Group, Permission
@ -14,6 +15,8 @@ from django.utils.encoding import force_str
from django.utils.html import escape, escapejs
from django.utils.http import RFC3986_SUBDELIMS, urlencode
from django.utils.safestring import mark_safe
from willow.optimizers.base import OptimizerBase
from willow.registry import registry
from wagtail.admin.admin_url_finder import AdminURLFinder
from wagtail.images import get_image_model
@ -3535,6 +3538,35 @@ class TestPreviewView(WagtailTestUtils, TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "image/png")
def test_preview_with_optimizer(self):
"""
Test that preview works with optimizers
Willow optimizers require
"""
class DummyOptimizer(OptimizerBase):
library_name = "dummy"
image_format = "png"
@classmethod
def check_library(cls):
return True
@classmethod
def process(cls, file_path: str):
pass
# Get the image
with patch.object(registry, "_registered_optimizers", [DummyOptimizer]):
response = self.client.get(
reverse("wagtailimages:preview", args=(self.image.id, "fill-800x600"))
)
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response["Content-Type"], "image/png")
def test_get_invalid_filter_spec(self):
"""
Test that an invalid filter spec returns a 400 response

Wyświetl plik

@ -1,8 +1,9 @@
import os
from tempfile import SpooledTemporaryFile
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse, JsonResponse
from django.http import FileResponse, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.urls import reverse
@ -291,8 +292,11 @@ def preview(request, image_id, filter_spec):
image = get_object_or_404(get_image_model(), id=image_id)
try:
response = HttpResponse()
image = Filter(spec=filter_spec).run(image, response)
# Temporary image needs to be an instance that Willow can run optimizers on
temp_image = SpooledTemporaryFile(max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE)
image = Filter(spec=filter_spec).run(image, temp_image)
temp_image.seek(0)
response = FileResponse(temp_image)
response["Content-Type"] = "image/" + image.format_name
return response
except InvalidFilterSpecError: