53 lines
1.8 KiB
TypeScript
53 lines
1.8 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 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}`,
|
|
image: (branchId: string) => `branch/branch-img-${branchId}`,
|
|
map: (branchId: string) => `branch/map-img-${branchId}`,
|
|
bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`,
|
|
img: (branchId: string, name?: string) => `user/img-${branchId}/${name || ""}`,
|
|
},
|
|
user: {
|
|
profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,
|
|
attachment: (userId: string, name?: string) => `user/attachment-${userId}/${name || ""}`,
|
|
},
|
|
};
|