Merge pull request #1000 from ae-utbm/imghdr

Remove call to deprecated `imghdr` module
This commit is contained in:
Bartuccio Antoine 2025-01-14 18:00:19 +01:00 committed by GitHub
commit 61170c0918
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 11 deletions

View File

@ -29,6 +29,7 @@ import os
import string import string
import unicodedata import unicodedata
from datetime import timedelta from datetime import timedelta
from io import BytesIO
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Optional, Self 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.timezone import localdate, now
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
from PIL import Image
if TYPE_CHECKING: if TYPE_CHECKING:
from pydantic import NonNegativeInt from pydantic import NonNegativeInt
@ -988,17 +990,11 @@ class SithFile(models.Model):
if self.is_folder: if self.is_folder:
if self.file: if self.file:
try: try:
import imghdr Image.open(BytesIO(self.file.read()))
except Image.UnidentifiedImageError as e:
if imghdr.what(None, self.file.read()) not in [ raise ValidationError(
"gif", _("This is not a valid folder thumbnail")
"png", ) from e
"jpeg",
]:
self.file.delete()
self.file = None
except: # noqa E722 I don't know the exception that can be raised
self.file = None
self.mime_type = "inode/directory" self.mime_type = "inode/directory"
if self.is_file and (self.file is None or self.file == ""): if self.is_file and (self.file is None or self.file == ""):
raise ValidationError(_("You must provide a file")) raise ValidationError(_("You must provide a file"))

View File

@ -2110,6 +2110,10 @@ msgstr ""
msgid "Duplicate file" msgid "Duplicate file"
msgstr "Un fichier de ce nom existe déjà" 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 #: core/models.py
msgid "You must provide a file" msgid "You must provide a file"
msgstr "Vous devez fournir un fichier" msgstr "Vous devez fournir un fichier"