feat: refactor file handling and enhance client notifications across components
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { db } from "~/server/db";
|
||||
import { auth } from "~/server/auth";
|
||||
import path from "path";
|
||||
import { promises as fs } from "fs";
|
||||
import { notifyClients } from "~/utils/notifyClients";
|
||||
|
||||
export async function DELETE(req: Request) {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = (await req.json()) as { id: string } | null;
|
||||
if (!body?.id) {
|
||||
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const resource = await db.file.findUnique({
|
||||
where: { id: body.id },
|
||||
});
|
||||
|
||||
if (!resource) {
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (resource.uploadedById !== session.user.id) {
|
||||
return NextResponse.json({ error: "You are not authorized to delete this file" }, { status: 403 });
|
||||
}
|
||||
|
||||
const filePath = path.join(process.cwd(), "uploads", path.basename(body.id));
|
||||
await fs.unlink(filePath).catch((err) => {
|
||||
console.error("Error deleting file from filesystem:", err);
|
||||
});
|
||||
|
||||
await db.file.delete({
|
||||
where: { id: body.id },
|
||||
});
|
||||
|
||||
notifyClients({ type: "file-removed", fileId: body.id });
|
||||
|
||||
return NextResponse.json({ message: "File deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error deleting file:", error);
|
||||
return NextResponse.json({ error: "Failed to delete file" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import path from "path";
|
||||
import { promises as fs } from "fs";
|
||||
import { db } from "~/server/db";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const url = new URL(req.url);
|
||||
const fileId = url.searchParams.get("id"); // Get the `id` parameter from the query
|
||||
|
||||
if (!fileId) {
|
||||
return NextResponse.json({ error: "File ID is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch file metadata from the database
|
||||
const file = await db.file.findFirst({
|
||||
where: { id: fileId },
|
||||
});
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Construct the file path
|
||||
const filePath = path.join(process.cwd(), "uploads", file.id);
|
||||
|
||||
// Read the file from the filesystem
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
|
||||
const mimeType = file.extension === ".mp4"
|
||||
? "video/mp4"
|
||||
: file.extension === ".webm"
|
||||
? "video/webm"
|
||||
: file.extension === ".ogg"
|
||||
? "video/ogg"
|
||||
: file.extension === ".jpg" || file.extension === ".jpeg"
|
||||
? "image/jpeg"
|
||||
: file.extension === ".png"
|
||||
? "image/png"
|
||||
: file.extension === ".gif"
|
||||
? "image/gif"
|
||||
: file.extension === ".svg"
|
||||
? "image/svg+xml"
|
||||
: file.extension === ".mp3"
|
||||
? "audio/mpeg"
|
||||
: file.extension === ".wav"
|
||||
? "audio/wav"
|
||||
: "application/octet-stream";
|
||||
|
||||
// Return the file as a binary response
|
||||
return new Response(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": mimeType,
|
||||
"Content-Disposition": `inline; filename="${file.name}"`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching file:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch file" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { db } from "~/server/db";
|
||||
import { auth } from "~/server/auth";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await auth();
|
||||
const url = new URL(req.url);
|
||||
const fileId = url.searchParams.get("id");
|
||||
|
||||
if (!fileId) {
|
||||
return NextResponse.json({ error: "File name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const file = await db.file.findFirst({
|
||||
where: { id: fileId },
|
||||
include: { uploadedBy: true },
|
||||
});
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
name: file.name,
|
||||
size: file.size,
|
||||
owner: file.uploadedBy?.name ?? null,
|
||||
ownerAvatar: file.uploadedBy?.image ?? null,
|
||||
uploadDate: file.uploadDate,
|
||||
id: file.id,
|
||||
isOwner: session?.user?.id === file.uploadedById,
|
||||
type: file.extension,
|
||||
url: file.url,
|
||||
description: file.description,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching file details:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch file details" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body = (await req.json()) as { id: string; description: string } | null;
|
||||
if (!body?.id || !body.description) {
|
||||
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const resource = await db.file.findUnique({
|
||||
where: { id: body.id },
|
||||
});
|
||||
|
||||
if (!resource) {
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (resource.uploadedById !== session.user.id) {
|
||||
return NextResponse.json({ error: "You are not authorized to modify this file" }, { status: 403 });
|
||||
}
|
||||
|
||||
await db.file.update({
|
||||
where: { id: body.id },
|
||||
data: { description: body.description },
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: "Description updated successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error updating description:", error);
|
||||
return NextResponse.json({ error: "Failed to update description" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user