From c6bb509fc372ac582f009d951b3923b53d27bc44 Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 10 Jan 2025 14:10:27 +0100 Subject: [PATCH] remove call to deprecated `imghdr` module --- core/models.py | 18 +++++++----------- locale/fr/LC_MESSAGES/django.po | 4 ++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/models.py b/core/models.py index 1b0ee0a3..b1caa912 100644 --- a/core/models.py +++ b/core/models.py @@ -29,6 +29,7 @@ import os import string import unicodedata from datetime import timedelta +from io import BytesIO from pathlib import Path from typing import TYPE_CHECKING, Optional, Self @@ -50,6 +51,7 @@ from django.utils.html import escape from django.utils.timezone import localdate, now from django.utils.translation import gettext_lazy as _ from phonenumber_field.modelfields import PhoneNumberField +from PIL import Image if TYPE_CHECKING: from pydantic import NonNegativeInt @@ -988,17 +990,11 @@ class SithFile(models.Model): if self.is_folder: if self.file: try: - import imghdr - - if imghdr.what(None, self.file.read()) not in [ - "gif", - "png", - "jpeg", - ]: - self.file.delete() - self.file = None - except: # noqa E722 I don't know the exception that can be raised - self.file = None + Image.open(BytesIO(self.file.read())) + except Image.UnidentifiedImageError as e: + raise ValidationError( + _("This is not a valid folder thumbnail") + ) from e self.mime_type = "inode/directory" if self.is_file and (self.file is None or self.file == ""): raise ValidationError(_("You must provide a file")) diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 56b03b07..07eede14 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -2110,6 +2110,10 @@ msgstr "" msgid "Duplicate file" msgstr "Un fichier de ce nom existe déjà" +#: core/models.py +msgid "This is not a valid folder thumbnail" +msgstr "Ceci n'est pas une miniature de dossier valide" + #: core/models.py msgid "You must provide a file" msgstr "Vous devez fournir un fichier"