Author: russellm
Date: 2007-09-14 02:18:27 -0500 (Fri, 14 Sep 2007)
New Revision: 6175

Modified:
   django/trunk/django/core/validators.py
   django/trunk/django/newforms/fields.py
Log:
Fixed #3848 -- Added more comprehensive checks to ImageField validation, 
checking for image truncation or corruption. Thanks to Andrew C <[EMAIL 
PROTECTED]> for the patch.


Modified: django/trunk/django/core/validators.py
===================================================================
--- django/trunk/django/core/validators.py      2007-09-14 07:12:47 UTC (rev 
6174)
+++ django/trunk/django/core/validators.py      2007-09-14 07:18:27 UTC (rev 
6175)
@@ -181,10 +181,15 @@
     except TypeError:
         raise ValidationError, _("No file was submitted. Check the encoding 
type on the form.")
     try:
-        Image.open(StringIO(content))
-    except (IOError, OverflowError): # Python Imaging Library doesn't 
recognize it as an image
-        # OverflowError is due to a bug in PIL with Python 2.4+ which can 
cause 
-        # it to gag on OLE files. 
+        # load() is the only method that can spot a truncated JPEG,
+        #  but it cannot be called sanely after verify()
+        trial_image = Image.open(StringIO(content))
+        trial_image.load()
+        # verify() is the only method that can spot a corrupt PNG,
+        #  but it must be called immediately after the constructor
+        trial_image = Image.open(StringIO(content))
+        trial_image.verify()
+    except Exception: # Python Imaging Library doesn't recognize it as an image
         raise ValidationError, _("Upload a valid image. The file you uploaded 
was either not an image or a corrupted image.")
 
 def isValidImageURL(field_data, all_data):

Modified: django/trunk/django/newforms/fields.py
===================================================================
--- django/trunk/django/newforms/fields.py      2007-09-14 07:12:47 UTC (rev 
6174)
+++ django/trunk/django/newforms/fields.py      2007-09-14 07:18:27 UTC (rev 
6175)
@@ -393,10 +393,15 @@
         from PIL import Image
         from cStringIO import StringIO
         try:
-            Image.open(StringIO(f.content))
-        except (IOError, OverflowError): # Python Imaging Library doesn't 
recognize it as an image
-            # OverflowError is due to a bug in PIL with Python 2.4+ which can 
cause 
-            # it to gag on OLE files. 
+            # load() is the only method that can spot a truncated JPEG,
+            #  but it cannot be called sanely after verify()
+            trial_image = Image.open(StringIO(f.content))
+            trial_image.load()
+            # verify() is the only method that can spot a corrupt PNG,
+            #  but it must be called immediately after the constructor
+            trial_image = Image.open(StringIO(f.content))
+            trial_image.verify()
+        except Exception: # Python Imaging Library doesn't recognize it as an 
image
             raise ValidationError(ugettext(u"Upload a valid image. The file 
you uploaded was either not an image or a corrupted image."))
         return f
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to