working site

This commit is contained in:
2025-04-14 08:20:36 +02:00
parent 08d1278f11
commit bcdceb615c
17 changed files with 238 additions and 146 deletions
+5 -5
View File
@@ -4,20 +4,20 @@ import { promises as fs } from "fs";
export async function GET(req: Request) {
const url = new URL(req.url);
const fileName = url.searchParams.get("fileName");
const fileId = url.searchParams.get("fileName");
if (!fileName) {
return NextResponse.json({ error: "File name is required" }, { status: 400 });
if (!fileId) {
return NextResponse.json({ error: "File id is required" }, { status: 400 });
}
try {
const filePath = path.join(process.cwd(), "uploads", fileName);
const filePath = path.join(process.cwd(), "uploads", fileId);
const fileBuffer = await fs.readFile(filePath);
return new Response(fileBuffer, {
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="${fileName}"`,
"Content-Disposition": `attachment; filename="${fileId}"`,
},
});
} catch (error) {
+3 -24
View File
@@ -1,6 +1,4 @@
import { NextResponse } from "next/server";
const clients: Set<any> = new Set();
import { clients } from "~/utils/notifyClients";
export async function GET() {
const stream = new ReadableStream({
@@ -20,16 +18,9 @@ export async function GET() {
clients.add(client);
// Remove the client when the stream is closed
const abortListener = () => {
clients.delete(client);
controller.close(); // Ensure the stream is closed when the client disconnects
};
signal.addEventListener("abort", abortListener);
// Cleanup the abort listener when the stream is closed
signal.addEventListener("abort", () => {
signal.removeEventListener("abort", abortListener);
clients.delete(client);
controller.close();
});
},
});
@@ -41,16 +32,4 @@ export async function GET() {
Connection: "keep-alive",
},
});
}
// Notify all connected clients about a file change
export function notifyClients(data: any) {
const message = JSON.stringify(data);
clients.forEach((client) => {
try {
client.send(message);
} catch (error) {
console.error("Failed to send message to a client:", error);
}
});
}