Fix bug where filenames with no extension cause an infinite loop

This commit is contained in:
Ricky Robinett 2016-07-28 10:52:47 -04:00 committed by Matt Westcott
parent a108f16eff
commit 9c0968ad12
5 changed files with 39 additions and 1 deletions

View file

@ -35,6 +35,7 @@ Changelog
* Fix: Wagtail's UserProfile model now sets a related_name of ``wagtail_userprofile`` to avoid naming collisions with other user profile models (Matt Westcott)
* Fix: Non-text content is now preserved when adding or editing a link within rich text (Matt Westcott)
* Fix: Fixed preview when `SECURE_SSL_REDIRECT = True` (Aymeric Augustin)
* Fix: Prevent hang when truncating an image filename without an extension (Ricky Robinett)
1.6.3 (30.09.2016)

View file

@ -170,6 +170,7 @@ Contributors
* Stein Strindhaug
* Žan Anderle
* Mattias Loverot
* Ricky Robinett
Translators
===========

View file

@ -70,6 +70,7 @@ Bug fixes
* Wagtail's UserProfile model now sets a related_name of ``wagtail_userprofile`` to avoid naming collisions with other user profile models (Matt Westcott)
* Non-text content is now preserved when adding or editing a link within rich text (Matt Westcott)
* Fixed preview when ``SECURE_SSL_REDIRECT = True`` (Aymeric Augustin)
* Prevent hang when truncating an image filename without an extension (Ricky Robinett)
Upgrade considerations

View file

@ -127,7 +127,10 @@ class AbstractImage(CollectionMember, index.Indexed, models.Model):
# https://code.djangoproject.com/ticket/9893
while len(os.path.join(folder_name, filename)) >= 95:
prefix, dot, extension = filename.rpartition('.')
filename = prefix[:-1] + dot + extension
if prefix:
filename = prefix[:-1] + dot + extension
else:
filename = extension[:-1]
return os.path.join(folder_name, filename)
def get_usage(self):

View file

@ -492,3 +492,35 @@ class TestIssue312(TestCase):
height=rend1.height,
focal_point_key=rend1.focal_point_key,
)
class TestFilenameReduction(TestCase):
"""
This tests for a bug which results in filenames without extensions
causing an infinite loop
"""
def test_filename_reduction_no_ext(self):
# Create an image with a big filename and no extension
image = Image.objects.create(
title="Test image",
file=get_test_image_file(
'thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocioussuperlong'
)
)
# Saving file will result in infinite loop when bug is present
image.save()
self.assertEqual("original_images/thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpiali", image.file.name)
# Test for happy path. Long filename with extension
def test_filename_reduction_ext(self):
# Create an image with a big filename and extensions
image = Image.objects.create(
title="Test image",
file=get_test_image_file(
'thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexpialidocioussuperlong.png'
)
)
image.save()
self.assertEqual("original_images/thisisaverylongfilename-abcdefghijklmnopqrstuvwxyz-supercalifragilisticexp.png", image.file.name)