Test image using Pillow

This commit is contained in:
Alexandre Iooss 2021-10-15 14:59:18 +02:00
parent baf70d396c
commit 59ac6535af

View file

@ -17,6 +17,7 @@ from django.views.generic.edit import FormView
from photologue.models import Gallery, Photo from photologue.models import Gallery, Photo
from photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView from photologue.views import GalleryArchiveIndexView, GalleryYearArchiveView
from taggit.models import Tag from taggit.models import Tag
from PIL import Image
from .forms import UploadForm from .forms import UploadForm
from .models import PhotoExtended from .models import PhotoExtended
@ -115,6 +116,17 @@ class GalleryUpload(FormView):
gallery = form.get_or_create_gallery() gallery = form.get_or_create_gallery()
failed_upload = 0 failed_upload = 0
for photo_file in files: 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}" title = f"{gallery.title} - {photo_file.name}"
try: try:
photo = Photo(title=title, slug=slugify(title)) photo = Photo(title=title, slug=slugify(title))