jws-backend/src/utils/minio.ts

135 lines
5.5 KiB
TypeScript
Raw Normal View History

import { buffer } from "node:stream/consumers";
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.")));
});
}
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);
}
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 });
}
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 });
});
});
}
const ROOT = ".system";
export const fileLocation = {
branch: {
line: (branchId: string) => `${ROOT}/branch/line-qr-${branchId}`,
bank: (branchId: string, bankId: string) => `${ROOT}/branch/bank-qr-${branchId}-${bankId}`,
img: (branchId: string, name?: string) => `${ROOT}/branch/img-${branchId}/${name || ""}`,
2025-03-17 15:20:18 +07:00
attachment: (branchId: string, name?: string) =>
`${ROOT}/branch/attachment-${branchId}/${name || ""}`,
},
2024-09-05 14:43:33 +07:00
user: {
2025-03-17 15:20:18 +07:00
profile: (userId: string, name?: string) =>
`${ROOT}/user/profile-image-${userId}/${name || ""}`,
2025-03-17 15:20:18 +07:00
attachment: (userId: string, name?: string) =>
`${ROOT}/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) => `${ROOT}/customer/img-${customerId}/${name || ""}`,
2024-09-10 09:56:46 +07:00
},
2024-09-16 17:20:51 +07:00
customerBranch: {
attachment: (customerBranchId: string, name?: string) =>
`${ROOT}/customer-branch/attachment-${customerBranchId}/${name || ""}`,
2024-09-16 17:20:51 +07:00
citizen: (customerBranchId: string, citizenId?: string) =>
`${ROOT}/customer-branch/citizen-${customerBranchId}/${citizenId || ""}`,
2024-09-16 17:20:51 +07:00
houseRegistration: (customerBranchId: string, houseRegistrationId?: string) =>
`${ROOT}/customer-branch/house-registration-${customerBranchId}/${houseRegistrationId || ""}`,
2024-09-16 17:20:51 +07:00
commercialRegistration: (customerBranchId: string, commercialRegistrationId?: string) =>
`${ROOT}/customer-branch/commercial-registration-${customerBranchId}/${commercialRegistrationId || ""}`,
2024-09-16 17:20:51 +07:00
vatRegistration: (customerBranchId: string, vatRegistrationId?: string) =>
`${ROOT}/customer-branch/vat-registration-${customerBranchId}/${vatRegistrationId || ""}`,
2024-09-16 17:20:51 +07:00
powerOfAttorney: (customerBranchId: string, powerOfAttorneyId?: string) =>
`${ROOT}/customer-branch/power-of-attorney-${customerBranchId}/${powerOfAttorneyId || ""}`,
2024-09-16 17:20:51 +07:00
},
2024-09-11 09:42:05 +07:00
employee: {
img: (employeeId: string, name?: string) => `${ROOT}/employee/img-${employeeId}/${name || ""}`,
2024-09-11 09:42:05 +07:00
attachment: (employeeId: string, name?: string) =>
`${ROOT}/employee/attachment-${employeeId}/${name || ""}`,
2025-03-17 15:20:18 +07:00
visa: (employeeId: string, visaId?: string) =>
`${ROOT}/employee/visa-${employeeId}/${visaId || ""}`,
2024-09-13 09:16:26 +07:00
passport: (employeeId: string, passportId?: string) =>
`${ROOT}/employee/passport-${employeeId}/${passportId || ""}`,
2024-09-13 17:03:46 +07:00
inCountryNotice: (employeeId: string, noticeId?: string) =>
`${ROOT}/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) => `${ROOT}/product/img-${productId}/${name || ""}`,
2024-09-10 14:19:16 +07:00
},
service: {
img: (serviceId: string, name?: string) => `${ROOT}/service/img-${serviceId}/${name || ""}`,
2024-09-10 14:19:16 +07:00
},
quotation: {
2024-11-01 13:43:57 +07:00
attachment: (quotationId: string, name?: string) =>
`${ROOT}/quotation/attachment-${quotationId}/${name || ""}`,
2024-10-21 11:18:48 +07:00
payment: (quotationId: string, paymentId: string, name?: string) =>
`${ROOT}/quotation/payment-${quotationId}/${paymentId}/${name || ""}`,
},
2024-10-09 15:04:59 +07:00
request: {
attachment: (requestId: string, step: number, name?: string) =>
`${ROOT}/request/attachment-${requestId}-${step}/${name || ""}`,
2024-10-09 15:04:59 +07:00
},
institution: {
attachment: (institutionId: string, name?: string) =>
`${ROOT}/institution/attachment-${institutionId}/${name || ""}`,
2025-03-17 15:20:18 +07:00
img: (institutionId: string, name?: string) =>
`${ROOT}/institution/img-${institutionId}/${name || ""}`,
},
task: {
2025-03-17 15:20:18 +07:00
attachment: (taskId: string, name?: string) =>
`${ROOT}/task/attachment-${taskId}/${name || ""}`,
},
creditNote: {
2025-03-17 15:20:18 +07:00
slip: (creditNoteId: string, name?: string) =>
`${ROOT}/credit-note/slip-${creditNoteId}/${name || ""}`,
2025-01-13 11:00:13 +07:00
attachment: (creditNoteId: string, name?: string) =>
`${ROOT}/credit-note/attachment-${creditNoteId}/${name || ""}`,
},
};