From 3aa4b8165da23a2f094d0eeffacbda5484f4c1f6 Mon Sep 17 00:00:00 2001 From: Anton Baklanov Date: Thu, 13 Dec 2012 18:26:34 +0200 Subject: 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. --- django/core/files/images.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'django') 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 -- cgit v1.3