error handling
This commit is contained in:
@@ -18,64 +18,69 @@ function serializeFile(file: Awaited<ReturnType<typeof prisma.file.create>>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
const session = await getSessionUser();
|
try {
|
||||||
if (!session) {
|
const session = await getSessionUser();
|
||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
if (!session) {
|
||||||
}
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
const formData = await req.formData();
|
const formData = await req.formData();
|
||||||
const files = formData.getAll("files") as File[];
|
const files = formData.getAll("files") as File[];
|
||||||
|
|
||||||
if (!files.length) {
|
if (!files.length) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "No files received in 'files' field." },
|
{ error: "No files received in 'files' field." },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check total upload size
|
// Check total upload size
|
||||||
const totalSize = files.reduce((acc, file) => acc + (file instanceof File ? file.size : 0), 0);
|
const totalSize = files.reduce((acc, file) => acc + (file instanceof File ? file.size : 0), 0);
|
||||||
if (totalSize > MAX_UPLOAD_SIZE) {
|
if (totalSize > MAX_UPLOAD_SIZE) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Total upload size exceeds 10GB limit." },
|
{ error: "Total upload size exceeds 10GB limit." },
|
||||||
{ status: 413 }
|
{ status: 413 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploaded = [];
|
const uploaded = [];
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (!(file instanceof File)) continue;
|
if (!(file instanceof File)) continue;
|
||||||
|
|
||||||
const arrayBuffer = await file.arrayBuffer();
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
const buffer = Buffer.from(arrayBuffer);
|
const buffer = Buffer.from(arrayBuffer);
|
||||||
const fileWithPath = file as File & { webkitRelativePath?: string };
|
const fileWithPath = file as File & { webkitRelativePath?: string };
|
||||||
const relativePath =
|
const relativePath =
|
||||||
fileWithPath.webkitRelativePath ?? fileWithPath.name ?? "unnamed";
|
fileWithPath.webkitRelativePath ?? fileWithPath.name ?? "unnamed";
|
||||||
const normalizedPath = relativePath.replace(/\\/g, "/");
|
const normalizedPath = relativePath.replace(/\\/g, "/");
|
||||||
const name = normalizedPath.split("/").pop() ?? normalizedPath;
|
const name = normalizedPath.split("/").pop() ?? normalizedPath;
|
||||||
const key = `${session.userId}/${Date.now()}-${normalizedPath}`;
|
const key = `${session.userId}/${Date.now()}-${normalizedPath}`;
|
||||||
const contentType = file.type || "application/octet-stream";
|
const contentType = file.type || "application/octet-stream";
|
||||||
|
|
||||||
await uploadToR2({
|
await uploadToR2({
|
||||||
key,
|
|
||||||
contentType,
|
|
||||||
body: buffer,
|
|
||||||
});
|
|
||||||
|
|
||||||
const record = await prisma.file.create({
|
|
||||||
data: {
|
|
||||||
key,
|
key,
|
||||||
name,
|
|
||||||
relativePath: normalizedPath,
|
|
||||||
contentType,
|
contentType,
|
||||||
sizeBytes: BigInt(file.size),
|
body: buffer,
|
||||||
userId: session.userId,
|
});
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
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 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user