From 52d7f2c6faf3672da0e6b9079d622698f4b88371 Mon Sep 17 00:00:00 2001
From: Karl Hobley <karl@torchbox.com>
Date: Wed, 27 Aug 2014 12:12:03 +0100
Subject: [PATCH 1/2] Added failing test for #573

---
 wagtail/wagtailimages/tests.py | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py
index dabd5e0e8d..d040a922d8 100644
--- a/wagtail/wagtailimages/tests.py
+++ b/wagtail/wagtailimages/tests.py
@@ -32,7 +32,7 @@ from wagtail.tests.models import EventPage, EventPageCarouselItem
 from wagtail.wagtailcore.models import Page
 
 
-def get_test_image_file():
+def get_test_image_file(filename='test.png'):
     from six import BytesIO
     from PIL import Image
     from django.core.files.images import ImageFile
@@ -40,7 +40,7 @@ def get_test_image_file():
     f = BytesIO()
     image = Image.new('RGB', (640, 480), 'white')
     image.save(f, 'PNG')
-    return ImageFile(f, name='test.png')
+    return ImageFile(f, name=filename)
 
 
 Image = get_image_model()
@@ -1016,3 +1016,24 @@ class TestCropToPoint(TestCase):
             CropBox(125, 25, 275, 175),
         )
 
+
+class TestIssue573(TestCase):
+    """
+    This tests for a bug which causes filename limit on Renditions to be reached
+    when the Image has a long original filename and a big focal point key
+    """
+    def test_issue_573(self):
+        # Create an image with a big filename and focal point
+        image = Image.objects.create(
+            title="Test image",
+            file=get_test_image_file('thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocious.png'),
+            focal_point_x=1000,
+            focal_point_y=1000,
+            focal_point_width=1000,
+            focal_point_height=1000,
+        )
+
+        # Try creating a rendition from that image
+        # This would crash if the bug is present
+        image.get_rendition('fill-800x600')
+

From 01daf3fb74974429a733e8014e52d0733ae30f22 Mon Sep 17 00:00:00 2001
From: Karl Hobley <karl@torchbox.com>
Date: Wed, 27 Aug 2014 12:28:08 +0100
Subject: [PATCH 2/2] Prevent rendition filenames going over 60 chars even with
 large focal point key. Fixes #573

---
 wagtail/wagtailimages/models.py | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py
index 63151b6022..88aa1ee1c3 100644
--- a/wagtail/wagtailimages/models.py
+++ b/wagtail/wagtailimages/models.py
@@ -161,10 +161,9 @@ class AbstractImage(models.Model, TagSearchable):
 
             input_filename_parts = os.path.basename(file_field.file.name).split('.')
             filename_without_extension = '.'.join(input_filename_parts[:-1])
-            filename_without_extension = filename_without_extension[:60]  # trim filename base so that we're well under 100 chars
-            output_filename_parts = [filename_without_extension, focal_point_key, filter.spec] + input_filename_parts[-1:]
-            output_filename = '.'.join(output_filename_parts)
-
+            extension = '.'.join([focal_point_key, filter.spec] + input_filename_parts[-1:])
+            filename_without_extension = filename_without_extension[:(59-len(extension))] # Truncate filename to prevent it going over 60 chars
+            output_filename = filename_without_extension + '.' + extension
             generated_image_file = File(generated_image, name=output_filename)
 
             if self.focal_point: