feat: integrate MinIO for file storage and management
- Added MinIO as a dependency in package.json. - Updated FilePreview component to simplify file type checks. - Refactored file removal API to delete files from MinIO instead of the filesystem. - Modified file serving API to fetch files from MinIO. - Changed file upload API to upload files directly to MinIO and store metadata in the database. - Enhanced environment configuration to include MinIO settings. - Updated file type utility to handle file extensions more robustly. - Created a new utility for MinIO client configuration and bucket management.
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
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 { minioClient } from "~/utils/minioClient";
|
||||
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 });
|
||||
}
|
||||
@@ -18,32 +16,25 @@ export async function DELETE(req: Request) {
|
||||
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const resource = await db.file.findUnique({
|
||||
where: { id: body.id },
|
||||
});
|
||||
|
||||
if (!resource) {
|
||||
const file = await db.file.findUnique({ where: { id: body.id } });
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
if (resource.uploadedById !== session.user.id) {
|
||||
if (file.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);
|
||||
});
|
||||
const objectName = `${file.id}-${file.name}`;
|
||||
await minioClient.removeObject(process.env.MINIO_BUCKET || "file-hosting", objectName);
|
||||
|
||||
await db.file.delete({
|
||||
where: { id: body.id },
|
||||
});
|
||||
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);
|
||||
console.error("Error deleting file from MinIO:", error);
|
||||
return NextResponse.json({ error: "Failed to delete file" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import path from "path";
|
||||
import { promises as fs } from "fs";
|
||||
import { db } from "~/server/db";
|
||||
import { minioClient } from "~/utils/minioClient";
|
||||
import { getFileType } from "~/utils/fileType";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
@@ -22,24 +21,31 @@ export async function GET(req: Request) {
|
||||
return NextResponse.json({ error: "File not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Construct the file path
|
||||
const filePath = path.join(process.cwd(), "uploads", file.id);
|
||||
const bucketName = process.env.MINIO_BUCKET || "file-hosting";
|
||||
const objectName = `${file.id}-${file.name}`; // Construct the object name in MinIO
|
||||
|
||||
// Read the file from the filesystem
|
||||
const fileBuffer = await fs.readFile(filePath);
|
||||
|
||||
const mimeType = getFileType(path.extname(file.name)); // Get the MIME type based on the file extension
|
||||
// Fetch the file from MinIO
|
||||
const stream = await minioClient.getObject(bucketName, objectName);
|
||||
|
||||
const mimeType = getFileType(file.name); // Get the MIME type based on the file extension
|
||||
|
||||
// Return the file as a binary response
|
||||
return new Response(fileBuffer, {
|
||||
const readableStream = new ReadableStream({
|
||||
start(controller) {
|
||||
stream.on("data", (chunk) => controller.enqueue(chunk));
|
||||
stream.on("end", () => controller.close());
|
||||
stream.on("error", (err) => controller.error(err));
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(readableStream, {
|
||||
headers: {
|
||||
"Content-Type": mimeType,
|
||||
"Content-Disposition": `inline; filename="${file.name}"`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching file:", error);
|
||||
console.error("Error fetching file from MinIO:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch file" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user