34 lines
1022 B
TypeScript
34 lines
1022 B
TypeScript
/** Allowed MIME types for receipt/item image uploads (server-side check). */
|
|
const ALLOWED_IMAGE_TYPES = new Set([
|
|
"image/jpeg",
|
|
"image/png",
|
|
"image/webp",
|
|
"image/heic",
|
|
"image/heif",
|
|
]);
|
|
|
|
/** Max size 10MB. */
|
|
const MAX_IMAGE_BYTES = 10 * 1024 * 1024;
|
|
|
|
/**
|
|
* Validates an image file for upload. Returns an error message if invalid, or null if valid/no file.
|
|
* Use in form actions before sending the file to PocketBase.
|
|
*/
|
|
export function validateImageUpload(file: File | null): string | null {
|
|
if (!file || file.size === 0) return null;
|
|
const type = (file.type ?? "").toLowerCase();
|
|
const name = file.name.toLowerCase();
|
|
const isHeic =
|
|
type === "image/heic" ||
|
|
type === "image/heif" ||
|
|
name.endsWith(".heic") ||
|
|
name.endsWith(".heif");
|
|
if (!isHeic && !ALLOWED_IMAGE_TYPES.has(type)) {
|
|
return "Invalid image type. Use JPEG, PNG, WebP, or HEIC.";
|
|
}
|
|
if (file.size > MAX_IMAGE_BYTES) {
|
|
return `Image must be under ${MAX_IMAGE_BYTES / 1024 / 1024}MB.`;
|
|
}
|
|
return null;
|
|
}
|