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-09-10 11:12:31 +07:00
|
|
|
export async function getFile(path: string, exp = 60 * 60) {
|
|
|
|
|
return await minio.presignedGetObject(MINIO_BUCKET, path, exp);
|
|
|
|
|
}
|
|
|
|
|
|
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-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-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-09-05 09:55:45 +07:00
|
|
|
};
|