2025-01-22 11:27:47 +07:00
|
|
|
import { buffer } from "node:stream/consumers";
|
2024-09-05 09:55:45 +07:00
|
|
|
import minio from "../services/minio";
|
|
|
|
|
|
|
|
|
|
if (!process.env.MINIO_BUCKET) {
|
|
|
|
|
throw Error("Require MinIO bucket.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
|
|
|
|
|
2024-09-05 14:43:33 +07:00
|
|
|
export async function listFile(path: string) {
|
|
|
|
|
return await new Promise<string[]>((resolve, reject) => {
|
|
|
|
|
const item: string[] = [];
|
|
|
|
|
|
|
|
|
|
const stream = minio.listObjectsV2(MINIO_BUCKET, path);
|
|
|
|
|
|
2024-09-06 14:07:44 +07:00
|
|
|
stream.on("data", (v) => v && v.name && item.push(v.name.split("/").at(-1) || ""));
|
2024-09-05 14:43:33 +07:00
|
|
|
stream.on("end", () => resolve(item));
|
|
|
|
|
stream.on("error", () => reject(new Error("MinIO error.")));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 09:36:04 +07:00
|
|
|
export async function getPresigned(method: string, path: string, exp = 60 * 60) {
|
|
|
|
|
return await minio.presignedUrl(method, MINIO_BUCKET, path, exp);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 11:12:31 +07:00
|
|
|
export async function getFile(path: string, exp = 60 * 60) {
|
|
|
|
|
return await minio.presignedGetObject(MINIO_BUCKET, path, exp);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 11:27:47 +07:00
|
|
|
export async function getFileBuffer(path: string) {
|
|
|
|
|
return await minio.getObject(MINIO_BUCKET, path).then(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 15:19:31 +07:00
|
|
|
export async function setFile(path: string, exp = 6 * 60 * 60) {
|
|
|
|
|
return await minio.presignedPutObject(MINIO_BUCKET, path, exp);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 14:43:33 +07:00
|
|
|
export async function deleteFile(path: string) {
|
|
|
|
|
await minio.removeObject(MINIO_BUCKET, path, { forceDelete: true });
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 09:55:45 +07:00
|
|
|
export async function deleteFolder(path: string) {
|
|
|
|
|
new Promise<string[]>((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: {
|
2024-09-05 13:18:47 +07:00
|
|
|
line: (branchId: string) => `branch/line-qr-${branchId}`,
|
|
|
|
|
bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`,
|
2024-09-09 15:46:48 +07:00
|
|
|
img: (branchId: string, name?: string) => `branch/img-${branchId}/${name || ""}`,
|
2024-09-12 13:29:48 +07:00
|
|
|
attachment: (branchId: string, name?: string) => `branch/attachment-${branchId}/${name || ""}`,
|
2024-09-05 09:55:45 +07:00
|
|
|
},
|
2024-09-05 14:43:33 +07:00
|
|
|
user: {
|
2024-09-06 14:03:02 +07:00
|
|
|
profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,
|
|
|
|
|
attachment: (userId: string, name?: string) => `user/attachment-${userId}/${name || ""}`,
|
2024-09-05 14:43:33 +07:00
|
|
|
},
|
2024-09-10 09:56:46 +07:00
|
|
|
customer: {
|
|
|
|
|
img: (customerId: string, name?: string) => `customer/img-${customerId}/${name || ""}`,
|
|
|
|
|
},
|
2024-09-16 17:20:51 +07:00
|
|
|
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 || ""}`,
|
|
|
|
|
},
|
2024-09-11 09:42:05 +07:00
|
|
|
employee: {
|
|
|
|
|
img: (employeeId: string, name?: string) => `employee/img-${employeeId}/${name || ""}`,
|
|
|
|
|
attachment: (employeeId: string, name?: string) =>
|
|
|
|
|
`employee/attachment-${employeeId}/${name || ""}`,
|
2024-09-13 09:16:26 +07:00
|
|
|
visa: (employeeId: string, visaId?: string) => `employee/visa-${employeeId}/${visaId || ""}`,
|
|
|
|
|
passport: (employeeId: string, passportId?: string) =>
|
|
|
|
|
`employee/passport-${employeeId}/${passportId || ""}`,
|
2024-09-13 17:03:46 +07:00
|
|
|
inCountryNotice: (employeeId: string, noticeId?: string) =>
|
|
|
|
|
`employee/in-country-notice-${employeeId}/${noticeId || ""}`,
|
2024-09-11 09:42:05 +07:00
|
|
|
},
|
2024-09-10 14:19:16 +07:00
|
|
|
product: {
|
|
|
|
|
img: (productId: string, name?: string) => `product/img-${productId}/${name || ""}`,
|
|
|
|
|
},
|
|
|
|
|
service: {
|
|
|
|
|
img: (serviceId: string, name?: string) => `service/img-${serviceId}/${name || ""}`,
|
|
|
|
|
},
|
2024-10-07 10:16:07 +07:00
|
|
|
quotation: {
|
2024-11-01 13:43:57 +07:00
|
|
|
attachment: (quotationId: string, name?: string) =>
|
|
|
|
|
`quotation/attachment-${quotationId}/${name || ""}`,
|
2024-10-21 11:18:48 +07:00
|
|
|
payment: (quotationId: string, paymentId: string, name?: string) =>
|
|
|
|
|
`quotation/payment-${quotationId}/${paymentId}/${name || ""}`,
|
2024-10-07 10:16:07 +07:00
|
|
|
},
|
2024-10-09 15:04:59 +07:00
|
|
|
request: {
|
2024-12-04 11:32:58 +07:00
|
|
|
attachment: (requestId: string, step: number, name?: string) =>
|
|
|
|
|
`request/attachment-${requestId}-${step}/${name || ""}`,
|
2024-10-09 15:04:59 +07:00
|
|
|
},
|
2024-11-08 13:26:34 +07:00
|
|
|
institution: {
|
|
|
|
|
attachment: (institutionId: string, name?: string) =>
|
|
|
|
|
`institution/attachment-${institutionId}/${name || ""}`,
|
2024-11-08 15:27:18 +07:00
|
|
|
img: (institutionId: string, name?: string) => `institution/img-${institutionId}/${name || ""}`,
|
2024-11-08 13:26:34 +07:00
|
|
|
},
|
2024-12-04 11:32:58 +07:00
|
|
|
task: {
|
|
|
|
|
attachment: (taskId: string, name?: string) => `task/attachment-${taskId}/${name || ""}`,
|
|
|
|
|
},
|
2025-01-10 09:05:03 +07:00
|
|
|
creditNote: {
|
2025-01-13 11:00:13 +07:00
|
|
|
slip: (creditNoteId: string, name?: string) => `credit-note/slip-${creditNoteId}/${name || ""}`,
|
|
|
|
|
attachment: (creditNoteId: string, name?: string) =>
|
|
|
|
|
`credit-note/attachment-${creditNoteId}/${name || ""}`,
|
2025-01-10 09:05:03 +07:00
|
|
|
},
|
2024-09-05 09:55:45 +07:00
|
|
|
};
|