import minio from "../services/minio"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); } const MINIO_BUCKET = process.env.MINIO_BUCKET; export async function listFile(path: string) { return await new Promise((resolve, reject) => { const item: string[] = []; const stream = minio.listObjectsV2(MINIO_BUCKET, path); stream.on("data", (v) => v && v.name && item.push(v.name.split("/").at(-1) || "")); stream.on("end", () => resolve(item)); stream.on("error", () => reject(new Error("MinIO error."))); }); } export async function getFile(path: string, exp = 60 * 60) { return await minio.presignedGetObject(MINIO_BUCKET, path, exp); } export async function setFile(path: string, exp = 6 * 60 * 60) { return await minio.presignedPutObject(MINIO_BUCKET, path, exp); } export async function deleteFile(path: string) { await minio.removeObject(MINIO_BUCKET, path, { forceDelete: true }); } export async function deleteFolder(path: string) { new Promise((resolve, reject) => { const item: string[] = []; const stream = minio.listObjectsV2(MINIO_BUCKET, path, true); stream.on("data", (v) => v && v.name && item.push(v.name)); stream.on("end", () => resolve(item)); stream.on("error", () => reject(new Error("MinIO error."))); }).then((list) => { list.map(async (v) => { await minio.removeObject(MINIO_BUCKET, v, { forceDelete: true }); }); }); } export const fileLocation = { branch: { line: (branchId: string) => `branch/line-qr-${branchId}`, bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`, img: (branchId: string, name?: string) => `branch/img-${branchId}/${name || ""}`, attachment: (branchId: string, name?: string) => `branch/attachment-${branchId}/${name || ""}`, }, user: { profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`, attachment: (userId: string, name?: string) => `user/attachment-${userId}/${name || ""}`, }, customer: { img: (customerId: string, name?: string) => `customer/img-${customerId}/${name || ""}`, }, customerBranch: { attachment: (customerBranchId: string, name?: string) => `customer-branch/attachment-${customerBranchId}/${name || ""}`, citizen: (customerBranchId: string, citizenId?: string) => `customer-branch/citizen-${customerBranchId}/${citizenId || ""}`, houseRegistration: (customerBranchId: string, houseRegistrationId?: string) => `customer-branch/house-registration-${customerBranchId}/${houseRegistrationId || ""}`, commercialRegistration: (customerBranchId: string, commercialRegistrationId?: string) => `customer-branch/commercial-registration-${customerBranchId}/${commercialRegistrationId || ""}`, vatRegistration: (customerBranchId: string, vatRegistrationId?: string) => `customer-branch/vat-registration-${customerBranchId}/${vatRegistrationId || ""}`, powerOfAttorney: (customerBranchId: string, powerOfAttorneyId?: string) => `customer-branch/power-of-attorney-${customerBranchId}/${powerOfAttorneyId || ""}`, }, employee: { img: (employeeId: string, name?: string) => `employee/img-${employeeId}/${name || ""}`, attachment: (employeeId: string, name?: string) => `employee/attachment-${employeeId}/${name || ""}`, visa: (employeeId: string, visaId?: string) => `employee/visa-${employeeId}/${visaId || ""}`, passport: (employeeId: string, passportId?: string) => `employee/passport-${employeeId}/${passportId || ""}`, inCountryNotice: (employeeId: string, noticeId?: string) => `employee/in-country-notice-${employeeId}/${noticeId || ""}`, }, product: { img: (productId: string, name?: string) => `product/img-${productId}/${name || ""}`, }, service: { img: (serviceId: string, name?: string) => `service/img-${serviceId}/${name || ""}`, }, quotation: { payment: (quotationId: string, paymentId: string) => `quotation/payment-${quotationId}/${paymentId || ""}`, }, request: { attachment: (requestId: string, name?: string) => `request/attachment-${requestId}/${name || ""}`, }, };