From ffd65ebca175b8b33590aabc3bbe5af837703023 Mon Sep 17 00:00:00 2001 From: asabizanjo Date: Thu, 11 Dec 2025 19:09:07 +0000 Subject: [PATCH] error handling --- app/api/files/upload/route.ts | 105 ++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/app/api/files/upload/route.ts b/app/api/files/upload/route.ts index a56407f..39a3130 100644 --- a/app/api/files/upload/route.ts +++ b/app/api/files/upload/route.ts @@ -18,64 +18,69 @@ function serializeFile(file: Awaited>) { } export async function POST(req: Request) { - const session = await getSessionUser(); - if (!session) { - return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); - } + try { + const session = await getSessionUser(); + if (!session) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } - const formData = await req.formData(); - const files = formData.getAll("files") as File[]; + const formData = await req.formData(); + const files = formData.getAll("files") as File[]; - if (!files.length) { - return NextResponse.json( - { error: "No files received in 'files' field." }, - { status: 400 } - ); - } + if (!files.length) { + return NextResponse.json( + { error: "No files received in 'files' field." }, + { status: 400 } + ); + } - // Check total upload size - const totalSize = files.reduce((acc, file) => acc + (file instanceof File ? file.size : 0), 0); - if (totalSize > MAX_UPLOAD_SIZE) { - return NextResponse.json( - { error: "Total upload size exceeds 10GB limit." }, - { status: 413 } - ); - } + // Check total upload size + const totalSize = files.reduce((acc, file) => acc + (file instanceof File ? file.size : 0), 0); + if (totalSize > MAX_UPLOAD_SIZE) { + return NextResponse.json( + { error: "Total upload size exceeds 10GB limit." }, + { status: 413 } + ); + } - const uploaded = []; - for (const file of files) { - if (!(file instanceof File)) continue; + const uploaded = []; + for (const file of files) { + if (!(file instanceof File)) continue; - const arrayBuffer = await file.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); - const fileWithPath = file as File & { webkitRelativePath?: string }; - const relativePath = - fileWithPath.webkitRelativePath ?? fileWithPath.name ?? "unnamed"; - const normalizedPath = relativePath.replace(/\\/g, "/"); - const name = normalizedPath.split("/").pop() ?? normalizedPath; - const key = `${session.userId}/${Date.now()}-${normalizedPath}`; - const contentType = file.type || "application/octet-stream"; + const arrayBuffer = await file.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + const fileWithPath = file as File & { webkitRelativePath?: string }; + const relativePath = + fileWithPath.webkitRelativePath ?? fileWithPath.name ?? "unnamed"; + const normalizedPath = relativePath.replace(/\\/g, "/"); + const name = normalizedPath.split("/").pop() ?? normalizedPath; + const key = `${session.userId}/${Date.now()}-${normalizedPath}`; + const contentType = file.type || "application/octet-stream"; - await uploadToR2({ - key, - contentType, - body: buffer, - }); - - const record = await prisma.file.create({ - data: { + await uploadToR2({ key, - name, - relativePath: normalizedPath, contentType, - sizeBytes: BigInt(file.size), - userId: session.userId, - }, - }); + body: buffer, + }); - uploaded.push(serializeFile(record)); + const record = await prisma.file.create({ + data: { + key, + name, + relativePath: normalizedPath, + contentType, + sizeBytes: BigInt(file.size), + userId: session.userId, + }, + }); + + uploaded.push(serializeFile(record)); + } + + return NextResponse.json({ ok: true, files: uploaded }); + } catch (error) { + console.error("Upload error:", error); + const message = error instanceof Error ? error.message : "Upload failed"; + return NextResponse.json({ error: message }, { status: 500 }); } - - return NextResponse.json({ ok: true, files: uploaded }); } -