jws-backend/src/utils/minio.ts
Methapon Metanipat 4e3c51d84b
feat: doc template (#10)
* feat: doc-template

* fix: empty not converted to dash

* feat: also return province, district and sub district

* refactor: move some relation to outer

* feat: add more function

* chore: deps

* feat: add more relation

* feat: count employee by gender

* feat: count all employee

* feat: add more function

* feat: get employment office

* fix: error

---------

Co-authored-by: Methapon2001 <61303214+Methapon2001@users.noreply.github.com>
2025-01-22 11:27:47 +07:00

125 lines
5.2 KiB
TypeScript

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;
export async function listFile(path: string) {
return await new Promise<string[]>((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 getPresigned(method: string, path: string, exp = 60 * 60) {
return await minio.presignedUrl(method, MINIO_BUCKET, path, exp);
}
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);
}
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<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: {
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: {
attachment: (quotationId: string, name?: string) =>
`quotation/attachment-${quotationId}/${name || ""}`,
payment: (quotationId: string, paymentId: string, name?: string) =>
`quotation/payment-${quotationId}/${paymentId}/${name || ""}`,
},
request: {
attachment: (requestId: string, step: number, name?: string) =>
`request/attachment-${requestId}-${step}/${name || ""}`,
},
institution: {
attachment: (institutionId: string, name?: string) =>
`institution/attachment-${institutionId}/${name || ""}`,
img: (institutionId: string, name?: string) => `institution/img-${institutionId}/${name || ""}`,
},
task: {
attachment: (taskId: string, name?: string) => `task/attachment-${taskId}/${name || ""}`,
},
creditNote: {
slip: (creditNoteId: string, name?: string) => `credit-note/slip-${creditNoteId}/${name || ""}`,
attachment: (creditNoteId: string, name?: string) =>
`credit-note/attachment-${creditNoteId}/${name || ""}`,
},
};