0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Serving images to external sites uses renditions

Previously, an image served to an external site was rendered on the fly.
This may have performance issues with large websites.

This commit changes the image serve view to use renditions so images are
only rendered when the url is first accessed. When the user hits the
url, they are now given a permanent redirect to the renditions image.
This commit is contained in:
Karl Hobley 2014-10-26 15:27:39 +00:00 committed by Karl Hobley
parent 3bd6178f29
commit 34579b6d43
3 changed files with 5 additions and 11 deletions

View File

@ -7,6 +7,7 @@ from django.conf import global_settings
WAGTAIL_ROOT = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(WAGTAIL_ROOT, 'test-static')
MEDIA_ROOT = os.path.join(WAGTAIL_ROOT, 'test-media')
MEDIA_URL = '/media/'
DATABASES = {

View File

@ -140,13 +140,7 @@ class TestFrontendServeView(TestCase):
response = self.client.get(reverse('wagtailimages_serve', args=(signature, self.image.id, 'fill-800x600')))
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'image/jpeg')
# Make sure the cache headers are set to expire after at least one month
self.assertIn('Cache-Control', response)
self.assertEqual(response['Cache-Control'].split('=')[0], 'max-age')
self.assertTrue(int(response['Cache-Control'].split('=')[1]) > datetime.timedelta(days=30).seconds)
self.assertRedirects(response, self.image.renditions.first().url, status_code=301, target_status_code=404)
def test_get_invalid_signature(self):
"""

View File

@ -1,13 +1,11 @@
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, redirect
from django.http import HttpResponse
from django.core.exceptions import PermissionDenied
from django.views.decorators.cache import cache_control
from wagtail.wagtailimages.models import get_image_model, Filter
from wagtail.wagtailimages.utils import verify_signature
@cache_control(max_age=60*60*24*60) # Cache for 60 days
def serve(request, signature, image_id, filter_spec):
image = get_object_or_404(get_image_model(), id=image_id)
@ -15,6 +13,7 @@ def serve(request, signature, image_id, filter_spec):
raise PermissionDenied
try:
return Filter(spec=filter_spec).process_image(image.file.file, HttpResponse(content_type='image/jpeg'), focal_point=image.get_focal_point())
rendition = image.get_rendition(filter_spec)
return redirect(rendition.url, permanent=True)
except Filter.InvalidFilterSpecError:
return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400)