Spaces:
Running
Running
Download src/lib/fileValidation.ts from suplo6669/sharpify-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/suplo6669/sharpify-ai/resolve/main/src/lib/fileValidation.ts
- Command line
-
hf download hf://spaces/suplo6669/sharpify-ai/src/lib/fileValidation.ts
-
curl -L -o fileValidation.ts https://huggingface.co/spaces/suplo6669/sharpify-ai/resolve/main/src/lib/fileValidation.ts
1.02 kB
| export const MAX_UPLOAD_BYTES = 20_000_000; | |
| export const MAX_UPLOAD_LABEL = "20 MB"; | |
| const SUPPORTED_TYPES = new Set(["image/jpeg", "image/png", "image/webp"]); | |
| const SUPPORTED_EXTENSIONS = /\.(jpe?g|png|webp)$/i; | |
| export function validateImageFile(file: File): string | null { | |
| if (file.size === 0) { | |
| return "This file is empty. Choose a JPG, PNG, or WEBP image."; | |
| } | |
| if (file.size > MAX_UPLOAD_BYTES) { | |
| return `This image is larger than the ${MAX_UPLOAD_LABEL} upload limit.`; | |
| } | |
| const mediaType = file.type.trim().toLowerCase(); | |
| const hasSupportedType = SUPPORTED_TYPES.has(mediaType); | |
| const hasSupportedExtension = SUPPORTED_EXTENSIONS.test(file.name); | |
| if (!hasSupportedType && (mediaType !== "" || !hasSupportedExtension)) { | |
| return "Choose a JPG, PNG, or WEBP image."; | |
| } | |
| return null; | |
| } | |
| export function formatFileSize(bytes: number): string { | |
| if (bytes < 1_000_000) { | |
| return `${Math.max(1, Math.round(bytes / 1_000))} KB`; | |
| } | |
| return `${(bytes / 1_000_000).toFixed(1)} MB`; | |
| } | |