Download apps/image_rotation/services/validation.py from jun2024/fc: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/image_rotation/services/validation.py
- Command line
-
hf download hf://spaces/jun2024/fc/apps/image_rotation/services/validation.py
-
curl -L -o validation.py https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/image_rotation/services/validation.py
1.08 kB
| """Business logic services.""" | |
| import base64 | |
| from django.utils.translation import gettext_lazy as _ | |
| from apps.core.exceptions import ValidationException | |
| def decode_base64_payload(data: str) -> bytes: | |
| try: | |
| value = data or "" | |
| if "," in value: | |
| value = value.split(",", 1)[1] | |
| return base64.b64decode(value) | |
| except Exception as e: | |
| raise ValidationException(message=_("图片数据解码失败"), code="IMAGE_DECODE_FAILED", errors={}) from e | |
| def validate_image_format(*, img_format: str, supported_formats: set[str]) -> str: | |
| value = (img_format or "jpeg").lower() | |
| if value not in supported_formats: | |
| raise ValidationException(message=f"不支持的图片格式: {value}", code="UNSUPPORTED_FORMAT", errors={}) | |
| return value | |
| def validate_file_size(*, image_bytes: bytes, max_file_size: int) -> None: | |
| if len(image_bytes) > max_file_size: | |
| raise ValidationException( | |
| message=f"文件大小超过 {max_file_size // (1024 * 1024)}MB 限制", code="FILE_TOO_LARGE", errors={} | |
| ) | |