Use 404 image when no image is available
All checks were successful
Deploy to Server / deploy (push) Successful in 10s

This commit is contained in:
Joakim Hellsén 2026-03-10 11:27:17 +01:00
commit 768d986556
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk
5 changed files with 88 additions and 6 deletions

View file

@ -1,8 +1,38 @@
import io
import logging
from typing import TYPE_CHECKING
from django.apps import AppConfig
from django.db.models.fields.files import FieldFile
if TYPE_CHECKING:
from collections.abc import Callable
class TwitchConfig(AppConfig):
"""AppConfig subclass for the 'twitch' application."""
"""Django app configuration for the Twitch app."""
default_auto_field = "django.db.models.BigAutoField"
name = "twitch"
def ready(self) -> None: # noqa: D102
logger: logging.Logger = logging.getLogger("ttvdrops.apps")
# Patch FieldFile.open to swallow FileNotFoundError and provide
# an empty in-memory file-like object so image dimension
# calculations don't crash when the on-disk file was removed.
try:
orig_open: Callable[..., FieldFile] = FieldFile.open
def _safe_open(self: FieldFile, mode: str = "rb") -> FieldFile:
try:
return orig_open(self, mode)
except FileNotFoundError:
# Provide an empty BytesIO so subsequent dimension checks
# read harmlessly and return (None, None).
self._file = io.BytesIO(b"") # pyright: ignore[reportAttributeAccessIssue]
return self
FieldFile.open = _safe_open
except (AttributeError, TypeError) as exc:
logger.debug("Failed to patch FieldFile.open: %s", exc)