promises, link

This commit is contained in:
2026-09-13 15:40:54 +05:30
parent 13c24e9db3
commit d8b42ece79
6 changed files with 315 additions and 110 deletions
+8 -13
View File
@@ -1,7 +1,6 @@
import { createReadStream } from "node:fs";
import { rm } from "node:fs/promises";
import { Readable } from "node:stream";
import { takeJob } from "@/lib/jobs";
import { getJobZip } from "@/lib/jobs";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
@@ -11,22 +10,18 @@ export async function GET(
context: { params: Promise<{ jobId: string }> },
): Promise<Response> {
const { jobId } = await context.params;
const job = takeJob(jobId);
const job = await getJobZip(jobId);
if (!job) {
return Response.json({ error: "Download expired or not found" }, { status: 404 });
return Response.json(
{ error: "Download expired or not found" },
{ status: 404 },
);
}
const file = createReadStream(job.zipPath);
file.on("close", () => {
void rm(job.dir, { recursive: true, force: true });
});
file.on("error", () => {
void rm(job.dir, { recursive: true, force: true });
});
return new Response(Readable.toWeb(file) as ReadableStream, {
return new Response(Readable.toWeb(createReadStream(job.zipPath)) as ReadableStream, {
headers: {
"Content-Type": "application/zip",
"Content-Length": String(job.size),
"Content-Disposition": 'attachment; filename="ids.zip"',
"Cache-Control": "no-store",
},