Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import path from "path";
|
||||
import { promises as fs } from "fs";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const url = new URL(req.url);
|
||||
const fileName = url.searchParams.get("fileName");
|
||||
|
||||
if (!fileName) {
|
||||
return NextResponse.json({ error: "File name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), "uploads", fileName);
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
|
||||
return new Response(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Disposition": `attachment; filename="${fileName}"`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error reading file:", error);
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { db } from "~/server/db"; // Ensure this points to your Prisma client setup
|
||||
import { auth } from "~/server/auth";
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const files = await db.file.findMany({
|
||||
where: { uploadedById: session.user.id },
|
||||
orderBy: { uploadDate: "desc" }, // Replace 'uploadDate' with the correct field name from your schema
|
||||
});
|
||||
|
||||
return NextResponse.json({ files });
|
||||
} catch (error) {
|
||||
console.error("Error fetching files:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch files" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const clients: Set<any> = new Set();
|
||||
|
||||
export async function GET() {
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
const abortController = new AbortController();
|
||||
const signal = abortController.signal;
|
||||
|
||||
const client = {
|
||||
send: (data: string) => {
|
||||
controller.enqueue(new TextEncoder().encode(`data: ${data}\n\n`));
|
||||
},
|
||||
close: () => {
|
||||
controller.close();
|
||||
abortController.abort();
|
||||
},
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
"Content-Type": "text/event-stream",
|
||||
"Cache-Control": "no-cache",
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user