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.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6175 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0863a634f3
commit
63dd4f5322
|
@ -181,10 +181,15 @@ def isValidImage(field_data, all_data):
|
||||||
except TypeError:
|
except TypeError:
|
||||||
raise ValidationError, _("No file was submitted. Check the encoding type on the form.")
|
raise ValidationError, _("No file was submitted. Check the encoding type on the form.")
|
||||||
try:
|
try:
|
||||||
Image.open(StringIO(content))
|
# load() is the only method that can spot a truncated JPEG,
|
||||||
except (IOError, OverflowError): # Python Imaging Library doesn't recognize it as an image
|
# but it cannot be called sanely after verify()
|
||||||
# OverflowError is due to a bug in PIL with Python 2.4+ which can cause
|
trial_image = Image.open(StringIO(content))
|
||||||
# it to gag on OLE files.
|
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.")
|
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):
|
def isValidImageURL(field_data, all_data):
|
||||||
|
|
|
@ -393,10 +393,15 @@ class ImageField(FileField):
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
try:
|
try:
|
||||||
Image.open(StringIO(f.content))
|
# load() is the only method that can spot a truncated JPEG,
|
||||||
except (IOError, OverflowError): # Python Imaging Library doesn't recognize it as an image
|
# but it cannot be called sanely after verify()
|
||||||
# OverflowError is due to a bug in PIL with Python 2.4+ which can cause
|
trial_image = Image.open(StringIO(f.content))
|
||||||
# it to gag on OLE files.
|
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."))
|
raise ValidationError(ugettext(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."))
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue