Django

Code

Changeset 6175

Show
Ignore:
Timestamp:
09/14/07 02:18:27 (8 months ago)
Author:
russellm
Message:

Fixed #3848 -- Added more comprehensive checks to ImageField? validation, checking for image truncation or corruption. Thanks to Andrew C <andrewc-djangotrac1@piffle.org> for the patch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/validators.py

    r6096 r6175  
    182182        raise ValidationError, _("No file was submitted. Check the encoding type on the form.") 
    183183    try: 
    184         Image.open(StringIO(content)) 
    185     except (IOError, OverflowError): # Python Imaging Library doesn't recognize it as an image 
    186         # OverflowError is due to a bug in PIL with Python 2.4+ which can cause  
    187         # it to gag on OLE files.  
     184        # load() is the only method that can spot a truncated JPEG, 
     185        #  but it cannot be called sanely after verify() 
     186        trial_image = Image.open(StringIO(content)) 
     187        trial_image.load() 
     188        # verify() is the only method that can spot a corrupt PNG, 
     189        #  but it must be called immediately after the constructor 
     190        trial_image = Image.open(StringIO(content)) 
     191        trial_image.verify() 
     192    except Exception: # Python Imaging Library doesn't recognize it as an image 
    188193        raise ValidationError, _("Upload a valid image. The file you uploaded was either not an image or a corrupted image.") 
    189194 
  • django/trunk/django/newforms/fields.py

    r6173 r6175  
    394394        from cStringIO import StringIO 
    395395        try: 
    396             Image.open(StringIO(f.content)) 
    397         except (IOError, OverflowError): # Python Imaging Library doesn't recognize it as an image 
    398             # OverflowError is due to a bug in PIL with Python 2.4+ which can cause  
    399             # it to gag on OLE files.  
     396            # load() is the only method that can spot a truncated JPEG, 
     397            #  but it cannot be called sanely after verify() 
     398            trial_image = Image.open(StringIO(f.content)) 
     399            trial_image.load() 
     400            # verify() is the only method that can spot a corrupt PNG, 
     401            #  but it must be called immediately after the constructor 
     402            trial_image = Image.open(StringIO(f.content)) 
     403            trial_image.verify() 
     404        except Exception: # Python Imaging Library doesn't recognize it as an image 
    400405            raise ValidationError(ugettext(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image.")) 
    401406        return f