Files
file-hosting/src/app/api/files/serv/route.ts
T
zaremate 792b0eb275 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.
2025-05-09 07:10:23 +02:00

51 lines
1.7 KiB
TypeScript

import { NextResponse } from "next/server";
import { db } from "~/server/db";
import { minioClient } from "~/utils/minioClient";
import { getFileType } from "~/utils/fileType";
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 });
}
const bucketName = process.env.MINIO_BUCKET || "file-hosting";
const objectName = `${file.id}-${file.name}`; // Construct the object name in MinIO
// 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
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 from MinIO:", error);
return NextResponse.json({ error: "Failed to fetch file" }, { status: 500 });
}
}