schon/engine/core/validators.py
Egor fureunoir Gorbunov dc94841f40 feat(core/blog): add support for product videos and blog post images
This commit introduces support for uploading optional video files to products and image files to blog posts. Enhanced admin interfaces were added to preview these files directly. Also includes adjustments to GraphQL types and serializers to expose the new fields.
2026-03-02 01:57:57 +03:00

47 lines
1.5 KiB
Python

from contextlib import suppress
import filetype
from django.core.exceptions import ValidationError
from django.core.files.images import ImageFile, get_image_dimensions
from django.utils.translation import gettext_lazy as _
_BROWSER_VIDEO_MIMES = {"video/mp4", "video/webm", "video/ogg"}
def validate_browser_video(file) -> None:
if not file:
return
kind = None
with suppress(Exception):
kind = filetype.guess(file)
if not kind:
raise ValidationError(_("could not determine file type"))
file.seek(0)
if kind is None or kind.mime not in _BROWSER_VIDEO_MIMES:
supported = ", ".join(sorted(_BROWSER_VIDEO_MIMES))
raise ValidationError(
_(f"unsupported video format. supported formats: {supported}")
)
def validate_category_image_dimensions(
image: ImageFile, max_width: int | None = None, max_height: int | None = None
) -> None:
max_width = max_width or 7680
max_height = max_height or 4320
if image:
try:
width, height = get_image_dimensions(image.file) # ty: ignore[invalid-argument-type]
except (FileNotFoundError, OSError, ValueError):
return
if width is None or height is None:
return
if int(width) > max_width or int(height) > max_height:
raise ValidationError(
_(
f"image dimensions should not exceed w{max_width} x h{max_height} pixels"
)
)