43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { getTask, getZipPathForTask } from "@/lib/testcase-backend";
|
|
import fs from "fs/promises";
|
|
|
|
export async function GET(
|
|
_req: NextRequest,
|
|
{ params }: { params: Promise<{ taskId: string }> }
|
|
) {
|
|
const { taskId } = await params;
|
|
if (!taskId) {
|
|
return Response.json({ error: "缺少 taskId" }, { status: 400 });
|
|
}
|
|
|
|
const task = getTask(taskId);
|
|
if (!task) {
|
|
return Response.json({ error: "任务未找到" }, { status: 404 });
|
|
}
|
|
|
|
if (task.status !== "completed") {
|
|
return Response.json({ error: "任务尚未完成" }, { status: 400 });
|
|
}
|
|
|
|
const zipPath = getZipPathForTask(taskId);
|
|
if (!zipPath) {
|
|
return Response.json({ error: "文件未找到" }, { status: 404 });
|
|
}
|
|
|
|
try {
|
|
const buffer = await fs.readFile(zipPath);
|
|
const filename = `${task.englishName}_testcases.zip`;
|
|
return new Response(buffer, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/zip",
|
|
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
|
|
},
|
|
});
|
|
} catch (e) {
|
|
console.error("[testcase] download read error:", e);
|
|
return Response.json({ error: "文件读取失败" }, { status: 500 });
|
|
}
|
|
}
|