diff options
| author | Anton Baklanov <antonbaklanov@gmail.com> | 2012-12-13 18:26:34 +0200 |
|---|---|---|
| committer | Florian Apolloner <florian@apolloner.eu> | 2013-01-01 11:54:56 +0100 |
| commit | 3aa4b8165da23a2f094d0eeffacbda5484f4c1f6 (patch) | |
| tree | 32df708f69941ec9d4b45272eb2b001ca0720f90 | |
| parent | bacb097ac31af2479c43934747b34fad7c91f55c (diff) | |
Fixed #19457 -- ImageField size detection failed for some files.
This was caused by PIL raising a zlib truncated stream error since we fed
the parser with chunks instead of the whole image.
| -rw-r--r-- | django/core/files/images.py | 14 | ||||
| -rw-r--r-- | tests/regressiontests/file_storage/magic.png | bin | 0 -> 70445 bytes | |||
| -rw-r--r-- | tests/regressiontests/file_storage/tests.py | 15 |
3 files changed, 28 insertions, 1 deletions
diff --git a/django/core/files/images.py b/django/core/files/images.py index 7d7eac65db..a8d3d8018b 100644 --- a/django/core/files/images.py +++ b/django/core/files/images.py @@ -4,7 +4,11 @@ Utility functions for handling images. Requires PIL, as you might imagine. """ +import zlib +import sys + from django.core.files import File +from django.utils import six class ImageFile(File): """ @@ -55,7 +59,15 @@ def get_image_dimensions(file_or_path, close=False): data = file.read(chunk_size) if not data: break - p.feed(data) + try: + p.feed(data) + except zlib.error as e: + # ignore zlib complaining on truncated stream, just feed more + # data to parser (ticket #19457). + if e.message.startswith("Error -5"): + pass + else: + six.reraise(*sys.exc_info()) if p.image: return p.image.size chunk_size = chunk_size*2 diff --git a/tests/regressiontests/file_storage/magic.png b/tests/regressiontests/file_storage/magic.png Binary files differnew file mode 100644 index 0000000000..a06449f9e9 --- /dev/null +++ b/tests/regressiontests/file_storage/magic.png diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py index b6d3e1ff0b..4872ce87b9 100644 --- a/tests/regressiontests/file_storage/tests.py +++ b/tests/regressiontests/file_storage/tests.py @@ -7,6 +7,7 @@ import shutil import sys import tempfile import time +import zlib from datetime import datetime, timedelta from io import BytesIO @@ -559,6 +560,20 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase): self.assertEqual(image_pil.size, size_1) self.assertEqual(size_1, size_2) + @unittest.skipUnless(Image, "PIL not installed") + def test_bug_19457(self): + """ + Regression test for #19457 + get_image_dimensions fails on some pngs, while Image.size is working good on them + """ + img_path = os.path.join(os.path.dirname(upath(__file__)), "magic.png") + try: + size = get_image_dimensions(img_path) + except zlib.error: + self.fail("Exception raised from get_image_dimensions().") + self.assertEqual(size, Image.open(img_path).size) + + class ContentFileTestCase(unittest.TestCase): def setUp(self): |
