shoc-frontend-new/src/domain/work-orders/utils/work-order-media-utils.ts
2026-07-17 13:59:31 -03:00

31 lines
835 B
TypeScript

import type {
FileCategory,
MediaCategory,
MediaFile,
} from "@/domain/work-orders/types/work-order-media";
let mediaIdCounter = 0;
export function classifyUpload(f: File): {
fileType: FileCategory;
category: MediaCategory;
} {
const t = f.type;
if (t.startsWith("image/")) return { fileType: "image", category: "Photo" };
if (t.startsWith("video/")) return { fileType: "video", category: "Video" };
if (t === "application/pdf" || /\.pdf$/i.test(f.name)) {
return { fileType: "pdf", category: "Report" };
}
return { fileType: "other", category: "Other" };
}
export function makeMediaFile(f: File): MediaFile {
const { fileType, category } = classifyUpload(f);
return {
id: `m-${Date.now()}-${mediaIdCounter++}`,
name: f.name,
fileType,
url: URL.createObjectURL(f),
category,
};
}