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 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"))

View File

@ -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"