From 6ee16f9df8aeabfef0ca533f9f5b13daab3a3758 Mon Sep 17 00:00:00 2001 From: Maze Winther Date: Sun, 22 Jun 2025 19:28:03 +0200 Subject: feat: so much stuff --- apps/web/src/lib/media-processing.ts | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 apps/web/src/lib/media-processing.ts (limited to 'apps/web/src/lib/media-processing.ts') diff --git a/apps/web/src/lib/media-processing.ts b/apps/web/src/lib/media-processing.ts new file mode 100644 index 00000000..c51dde55 --- /dev/null +++ b/apps/web/src/lib/media-processing.ts @@ -0,0 +1,67 @@ +import { toast } from "sonner"; +import { + getFileType, + generateVideoThumbnail, + getMediaDuration, + getImageAspectRatio, + type MediaItem, +} from "@/stores/media-store"; + +export interface ProcessedMediaItem extends Omit {} + +export async function processMediaFiles( + files: FileList | File[] +): Promise { + const fileArray = Array.from(files); + const processedItems: ProcessedMediaItem[] = []; + + for (const file of fileArray) { + const fileType = getFileType(file); + + if (!fileType) { + toast.error(`Unsupported file type: ${file.name}`); + continue; + } + + const url = URL.createObjectURL(file); + let thumbnailUrl: string | undefined; + let duration: number | undefined; + let aspectRatio: number = 16 / 9; // Default fallback + + try { + if (fileType === "image") { + // Get image aspect ratio + aspectRatio = await getImageAspectRatio(file); + } else if (fileType === "video") { + // Generate thumbnail and get aspect ratio for videos + const videoResult = await generateVideoThumbnail(file); + thumbnailUrl = videoResult.thumbnailUrl; + aspectRatio = videoResult.aspectRatio; + } else if (fileType === "audio") { + // For audio, use a square aspect ratio + aspectRatio = 1; + } + + // Get duration for videos and audio + if (fileType === "video" || fileType === "audio") { + duration = await getMediaDuration(file); + } + + processedItems.push({ + name: file.name, + type: fileType, + file, + url, + thumbnailUrl, + duration, + aspectRatio, + }); + } catch (error) { + console.error("Error processing file:", file.name, error); + toast.error(`Failed to process ${file.name}`); + URL.revokeObjectURL(url); // Clean up on error + } + } + + return processedItems; +} -- cgit v1.3.1