From 59ac6535affb93b602a251c5cfbe92df33094306 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Fri, 15 Oct 2021 14:59:18 +0200 Subject: [PATCH] Test image using Pillow --- photologue_custom/views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/photologue_custom/views.py b/photologue_custom/views.py index 51d0517..f95ea07 100644 --- a/photologue_custom/views.py +++ b/photologue_custom/views.py @@ -17,6 +17,7 @@ from django.views.generic.edit import FormView from photologue.models import Gallery, Photo from photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView from taggit.models import Tag +from PIL import Image from .forms import UploadForm from .models import PhotoExtended @@ -115,6 +116,17 @@ class GalleryUpload(FormView): gallery = form.get_or_create_gallery() failed_upload = 0 for photo_file in files: + # Check that we have a valid image + print(photo_file, type(photo_file)) + try: + opened = Image.open(photo_file) + opened.verify() + except Exception: + # Pillow doesn't recognize it as an image, skip it + messages.error(self.request, f"{photo_file.name} was not recognized as an image") + failed_upload += 1 + continue + title = f"{gallery.title} - {photo_file.name}" try: photo = Photo(title=title, slug=slugify(title))