From 9545550281a31ed84d877bd768ce7ea85b2a0f4c Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 16 Jul 2014 13:41:50 +0100 Subject: [PATCH] Added error for bad filter specs --- wagtail/wagtailimages/utils.py | 6 ++++++ wagtail/wagtailimages/views/frontend.py | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailimages/utils.py b/wagtail/wagtailimages/utils.py index 00c5476cd..c94bf96db 100644 --- a/wagtail/wagtailimages/utils.py +++ b/wagtail/wagtailimages/utils.py @@ -29,6 +29,10 @@ def validate_image_format(f): raise ValidationError(_("Not a valid %s image. Please use a gif, jpeg or png file with the correct file extension.") % (extension.upper())) +class InvalidFilterSpecError(RuntimeError): + pass + + # TODO: Cache results from this method in something like Python 3.2s LRU cache (available in Django 1.7 as django.utils.lru_cache) def parse_filter_spec(filter_spec): # parse the spec string and save the results to @@ -62,3 +66,5 @@ def parse_filter_spec(filter_spec): width = int(match.group(2)) height = int(match.group(3)) return OPERATION_NAMES[match.group(1)], (width, height) + + raise InvalidFilterSpecError(filter_spec) diff --git a/wagtail/wagtailimages/views/frontend.py b/wagtail/wagtailimages/views/frontend.py index 11c4d9525..1ae145d7b 100644 --- a/wagtail/wagtailimages/views/frontend.py +++ b/wagtail/wagtailimages/views/frontend.py @@ -2,10 +2,14 @@ from django.shortcuts import get_object_or_404 from django.http import HttpResponse from wagtail.wagtailimages.models import get_image_model +from wagtail.wagtailimages.utils import InvalidFilterSpecError from wagtail.wagtailimages import image_processor def serve(request, image_id, filter_spec): image = get_object_or_404(get_image_model(), id=image_id) - return image_processor.process_image(image.file.file, HttpResponse(content_type='image/jpeg'), filter_spec) + try: + return image_processor.process_image(image.file.file, HttpResponse(content_type='image/jpeg'), filter_spec) + except InvalidFilterSpecError: + return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400)