79 lines
2.9 KiB
TypeScript
79 lines
2.9 KiB
TypeScript
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 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<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 || ""}`,
|
|
},
|
|
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 || ""}`,
|
|
reportInCountry: (employeeId: string, reportId?: string) =>
|
|
`employee/report-in-country-${employeeId}/${reportId || ""}`,
|
|
},
|
|
product: {
|
|
img: (productId: string, name?: string) => `product/img-${productId}/${name || ""}`,
|
|
},
|
|
service: {
|
|
img: (serviceId: string, name?: string) => `service/img-${serviceId}/${name || ""}`,
|
|
},
|
|
};
|