fix: response does not wait process to finish
This commit is contained in:
parent
1c3ac35ed1
commit
adfb70b578
8 changed files with 253 additions and 144 deletions
|
|
@ -6,7 +6,6 @@ import {
|
|||
Path,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
|
|
@ -17,9 +16,10 @@ import * as Minio from "minio";
|
|||
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { listFolder, pathExist, replaceIllegalChars } from "../utils/minio";
|
||||
import { EhrFolder } from "../interfaces/ehr-fs";
|
||||
import { listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
|
||||
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
|
||||
import minioClient from "../storage";
|
||||
import esClient from "../elasticsearch";
|
||||
|
||||
@Route("/cabinet/{cabinetName}/drawer/{drawerName}/folder")
|
||||
export class FolderController extends Controller {
|
||||
|
|
@ -30,7 +30,7 @@ export class FolderController extends Controller {
|
|||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
): Promise<EhrFolder[]> {
|
||||
const fullpath = [cabinetName, drawerName].join("/") + "/";
|
||||
const fullpath = [cabinetName, drawerName, ""].join("/");
|
||||
|
||||
if (!(await pathExist(fullpath))) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "Provided path does not exist.");
|
||||
|
|
@ -79,44 +79,63 @@ export class FolderController extends Controller {
|
|||
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
||||
public async editFolder(
|
||||
@Body() body: { name: string },
|
||||
@Query() cabinetName: string,
|
||||
@Query() drawerName: string,
|
||||
@Query() folderName: string,
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
@Path() folderName: string,
|
||||
) {
|
||||
const fullpath = [cabinetName, drawerName, folderName].join("/") + "/";
|
||||
const fullpath = `${cabinetName}/${drawerName}/${folderName}`;
|
||||
|
||||
if (!(await pathExist(fullpath))) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.PRECONDITION_FAILED,
|
||||
"Provided resource location does not exist.",
|
||||
);
|
||||
}
|
||||
const list = await listItem(fullpath, true);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const stream = minioClient.listObjectsV2("ehr", fullpath, true);
|
||||
const cond = new Minio.CopyConditions();
|
||||
|
||||
stream.on("data", (v) => {
|
||||
if (!(v && v.name)) return;
|
||||
await Promise.all(
|
||||
list.map(async (current) => {
|
||||
if (!current.name) return;
|
||||
|
||||
const destination = `${cabinetName}/${drawerName}/${replaceIllegalChars(
|
||||
body.name,
|
||||
)}/${v.name.slice(fullpath.length)}`;
|
||||
const source = `/ehr/${v.name}`;
|
||||
const cond = new Minio.CopyConditions();
|
||||
)}/${current.name.slice(fullpath.length)}`;
|
||||
const source = `/ehr/${current.name}`;
|
||||
|
||||
minioClient.copyObject("ehr", destination, source, cond, (e) => {
|
||||
if (e) {
|
||||
return reject(new Error("Failed to move."));
|
||||
}
|
||||
return minioClient.removeObject("ehr", v.name);
|
||||
});
|
||||
});
|
||||
return await minioClient
|
||||
.copyObject("ehr", destination, source, cond)
|
||||
.then(async () => {
|
||||
if (!current.name) return;
|
||||
|
||||
stream.on("end", () => {
|
||||
resolve(this.setStatus(HttpStatusCode.NO_CONTENT));
|
||||
});
|
||||
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
||||
});
|
||||
await minioClient.removeObject("ehr", current.name);
|
||||
|
||||
if (current.name.includes(".keep")) return;
|
||||
|
||||
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
||||
query: {
|
||||
match: {
|
||||
pathname: current.name,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (search && search.hits.hits.length === 0) {
|
||||
throw new Error("Data cannot be found in database.");
|
||||
}
|
||||
|
||||
const data = search.hits.hits[0];
|
||||
|
||||
await esClient.update({
|
||||
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
||||
id: data._id,
|
||||
doc: { pathname: destination },
|
||||
});
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
throw new Error("Failed to move.");
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Delete("/{folderName}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue