import { auth } from "@/lib/auth"; import { writeFile, mkdir } from "fs/promises"; import path from "path"; import { randomUUID } from "crypto"; const PIPELINE_ROOT = process.env.PIPELINE_ROOT || path.join(process.cwd(), "pipeline"); export async function POST(request: Request) { const session = await auth(); if (!session) return new Response("Unauthorized", { status: 401 }); const formData = await request.formData(); const files = formData.getAll("files") as File[]; if (files.length === 0) { return Response.json({ error: "No files provided" }, { status: 400 }); } const screenshotsDir = path.join(PIPELINE_ROOT, "assets", "screenshots"); await mkdir(screenshotsDir, { recursive: true }); const uploaded: { fileName: string; path: string }[] = []; for (const file of files) { if (!file.type.startsWith("image/")) continue; const ext = path.extname(file.name) || ".png"; const uniqueName = `${randomUUID()}${ext}`; const filePath = path.join(screenshotsDir, uniqueName); const buffer = Buffer.from(await file.arrayBuffer()); await writeFile(filePath, buffer); uploaded.push({ fileName: file.name, path: `assets/screenshots/${uniqueName}`, }); } return Response.json({ uploaded }); }