error handling

This commit is contained in:
asabizanjo
2025-12-11 19:09:07 +00:00
parent 7c44246704
commit ffd65ebca1

View File

@@ -18,64 +18,69 @@ function serializeFile(file: Awaited<ReturnType<typeof prisma.file.create>>) {
}
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 });
}