Files
yfan 3d175d75af
Some checks failed
Build and Push Docker Image / build (push) Has been cancelled
Sync to CNB / sync (push) Has been cancelled
Delete old workflow runs / del_runs (push) Has been cancelled
Upstream Sync / Sync latest commits from upstream repo (push) Has been cancelled
first commit
2026-01-30 16:57:44 +08:00

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 });
}
}