Skip to content

Commit

Permalink
check if image is not corrupted during upload (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy authored Feb 22, 2023
1 parent dfb2cb6 commit 43e052e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions avatar/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.template.defaultfilters import filesizeformat
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from PIL import Image, ImageOps

from avatar.conf import settings
from avatar.models import Avatar
Expand Down Expand Up @@ -82,6 +83,12 @@ def clean_avatar(self):
}
)

try:
image = Image.open(data)
ImageOps.exif_transpose(image)
except TypeError:
raise forms.ValidationError(_("Corrupted image"))

count = Avatar.objects.filter(user=self.user).count()
if 1 < settings.AVATAR_MAX_AVATARS_PER_USER <= count:
error = _(
Expand Down

0 comments on commit 43e052e

Please sign in to comment.