fix: response does not wait process to finish

This commit is contained in:
Methapon2001 2023-11-23 10:34:26 +07:00
parent 1c3ac35ed1
commit adfb70b578
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
8 changed files with 253 additions and 144 deletions

View file

@ -1,4 +1,5 @@
import { EhrFolder } from "../interfaces/ehr-fs";
import * as Minio from "minio";
import minioClient from "../storage";
/**
@ -59,7 +60,12 @@ export function listFolder(path?: string): Promise<EhrFolder[]> {
stream.on("end", async () => {
for (let i = 0; i < folder.length; i++) {
const stat = await minioClient.statObject("ehr", `${folder[i].pathname}.keep`);
const stat = await minioClient
.statObject("ehr", `${folder[i].pathname}.keep`)
.catch((e) => console.error(`Error List Folder: ${folder[i].pathname}`, e));
if (!stat) continue;
folder[i] = {
...folder[i],
createdAt: stat.metaData.createdat ?? "N/A",
@ -72,3 +78,16 @@ export function listFolder(path?: string): Promise<EhrFolder[]> {
stream.on("error", () => reject(new Error("Object storage error occured.")));
});
}
export async function listItem(path: string, recursive = false): Promise<Minio.BucketItem[]> {
return new Promise((resolve, reject) => {
const stream = minioClient.listObjectsV2("ehr", path, recursive);
const item: Minio.BucketItem[] = [];
stream.on("data", (v) => {
if (v && v.name) item.push(v);
});
stream.on("end", () => resolve(item));
stream.on("error", () => reject(new Error("Object storage error occured.")));
});
}