import crypto from "node:crypto"; import { Client } from "minio"; import { MINIO_ACCESS_KEY, MINIO_BUCKET_NAME, MINIO_ENDPOINT, MINIO_PORT, MINIO_SECRET_KEY, MINIO_USE_SSL, } from "@/lib/constants"; const globalForMinio = globalThis as unknown as { minioClient?: Client; minioBucketPromise?: Promise; }; export const minioClient = globalForMinio.minioClient ?? new Client({ endPoint: MINIO_ENDPOINT, port: MINIO_PORT, useSSL: MINIO_USE_SSL, accessKey: MINIO_ACCESS_KEY, secretKey: MINIO_SECRET_KEY, }); if (process.env.NODE_ENV !== "production") { globalForMinio.minioClient = minioClient; } export async function ensureMinioBucket() { if (!globalForMinio.minioBucketPromise) { globalForMinio.minioBucketPromise = (async () => { const exists = await minioClient.bucketExists(MINIO_BUCKET_NAME); if (!exists) { await minioClient.makeBucket(MINIO_BUCKET_NAME); } })(); } return globalForMinio.minioBucketPromise; } export function buildCaseFileStorageKey(caseId: string, originalFileName: string) { const random = crypto.randomBytes(12).toString("hex"); const sanitizedName = originalFileName.replace(/[^a-zA-Z0-9._-]/g, "_"); return `cases/${caseId}/${random}-${sanitizedName}`; }