refactor: rabbitmq implement

This commit is contained in:
Methapon2001 2023-11-27 09:45:30 +07:00
parent 24350a11a4
commit 3fc70daed0
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
12 changed files with 676 additions and 545 deletions

View file

@ -6,88 +6,100 @@ import {
Path,
Post,
Put,
Request,
Route,
Security,
SuccessResponse,
Tags,
Request,
Response,
} from "tsoa";
import * as Minio from "minio";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
import { listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
import minioClient from "../storage";
import minioClient from "../minio";
import esClient from "../elasticsearch";
import { copyCond, listFolder, listItem, replaceIllegalChars } from "../utils/minio";
import HttpStatusCode from "../interfaces/http-status";
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
import HttpError from "../interfaces/http-error";
const DEFAULT_BUCKET = process.env.MINIO_BUCKET;
const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX;
if (!DEFAULT_BUCKET) throw Error("Default MinIO bucket must be specified.");
if (!DEFAULT_INDEX) throw Error("Default ElasticSearch index must be specified.");
@Route("/cabinet/{cabinetName}/drawer/{drawerName}/folder")
export class FolderController extends Controller {
@Get("/")
@Tags("Folder")
@SuccessResponse(HttpStatusCode.OK)
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการแฟ้มได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
public async listFolder(
@Path() cabinetName: string,
@Path() drawerName: string,
): Promise<EhrFolder[]> {
const fullpath = [cabinetName, drawerName, ""].join("/");
if (!(await pathExist(fullpath))) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "Provided path does not exist.");
}
return listFolder(fullpath);
const list = await listFolder(DEFAULT_BUCKET!, `${cabinetName}/${drawerName}`).catch((e) =>
console.error(`Error List Folder: ${e}`),
);
if (!list) throw new Error("เกิดข้อผิดพลาด ไม่สามารถแสดงรายการแฟ้มได้ กรุณาลองใหม่ในภายหลัง");
return list;
}
@Post("/")
@Tags("Folder")
@Security("bearerAuth")
@SuccessResponse(HttpStatusCode.CREATED)
@Security("bearerAuth", ["admin"])
@Response(HttpStatusCode.NOT_FOUND, "ไม่พบของแฟ้ม")
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดกับระบบจัดการไฟล์")
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
public async createFolder(
@Request() request: { user: { preferred_username: string } },
@Body() body: { name: string },
@Path() cabinetName: string,
@Path() drawerName: string,
) {
if (!(await pathExist(`${cabinetName}/${drawerName}/`))) {
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "Cabinet or drawer cannot be found.");
const basePath = `${cabinetName}/${drawerName}/`;
if (
!Boolean(
await minioClient.statObject(DEFAULT_BUCKET!, `${basePath}.keep`).catch((e) => {
if (e.code === "NotFound") return false;
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
}),
)
) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแฟ้ม");
}
const uploaded = await minioClient
.putObject(
"ehr",
`${cabinetName}/${drawerName}/${replaceIllegalChars(body.name)}/.keep`,
"",
0,
{
createdAt: new Date().toISOString(),
createdBy: request.user.preferred_username,
},
)
const created = await minioClient
.putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, {
createdAt: new Date().toISOString(),
createdBy: request.user.preferred_username,
})
.catch((e) => console.error(e));
if (!uploaded) {
throw new Error("Object storage error occured.");
}
if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
return this.setStatus(HttpStatusCode.CREATED);
}
@Put("/{folderName}")
@Tags("Folder")
@Security("bearerAuth")
@SuccessResponse(HttpStatusCode.NO_CONTENT)
@Security("bearerAuth", ["admin"])
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดไม่สามารถย้ายไฟล์ได้")
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
public async editFolder(
@Body() body: { name: string },
@Path() cabinetName: string,
@Path() drawerName: string,
@Path() folderName: string,
) {
const fullpath = `${cabinetName}/${drawerName}/${folderName}/`;
const list = await listItem(fullpath, true);
const cond = new Minio.CopyConditions();
const path = `${cabinetName}/${drawerName}/${folderName}`;
const list = await listItem(DEFAULT_BUCKET!, path, true);
await Promise.all(
list.map(async (current) => {
@ -95,42 +107,36 @@ export class FolderController extends Controller {
const destination = `${cabinetName}/${drawerName}/${replaceIllegalChars(
body.name,
)}/${current.name.slice(fullpath.length)}`;
const source = `/ehr/${current.name}`;
)}/${current.name.slice(path.length)}`;
const source = `/${DEFAULT_BUCKET}/${current.name}`;
return await minioClient
.copyObject("ehr", destination, source, cond)
.copyObject(DEFAULT_BUCKET!, destination, source, copyCond)
.then(async () => {
if (!current.name) return;
await minioClient.removeObject("ehr", current.name);
if (current.name.includes(".keep")) return;
if (current.name.includes(".keep")) {
return await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
}
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
query: {
match: {
pathname: current.name,
},
},
index: DEFAULT_INDEX!,
query: { match: { pathname: current.name } },
});
if (search && search.hits.hits.length === 0) {
throw new Error("Data cannot be found in database.");
}
if (search && search.hits.hits.length === 0) throw new Error("ไม่พบข้อมูลในฐานข้อมูล");
const data = search.hits.hits[0];
await esClient.update({
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
index: DEFAULT_INDEX!,
id: data._id,
doc: { pathname: destination },
});
await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
})
.catch((e) => {
console.error(e);
throw new Error("Failed to move.");
throw new Error("เกิดข้อผิดพลาด ไม่สามารถย้ายไฟล์ได้");
});
}),
);
@ -140,8 +146,8 @@ export class FolderController extends Controller {
@Delete("/{folderName}")
@Tags("Folder")
@Security("bearerAuth")
@SuccessResponse(HttpStatusCode.NO_CONTENT)
@Security("bearerAuth", ["admin"])
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
public async deleteFolder(
@Path() cabinetName: string,
@Path() drawerName: string,
@ -150,42 +156,20 @@ export class FolderController extends Controller {
await new Promise<void>((resolve, reject) => {
const objects: string[] = [];
const stream = minioClient.listObjectsV2(
"ehr",
`${cabinetName}/${drawerName}/${folderName}/`,
DEFAULT_BUCKET!,
`${cabinetName}/${drawerName}/${folderName}`,
true,
);
stream.on("data", (v) => {
if (!(v && v.name)) return;
objects.push(v.name);
if (v && v.name) objects.push(v.name);
});
stream.on("close", async () => {
minioClient.removeObjects("ehr", objects);
resolve();
});
stream.on("error", () => reject(new Error("Object storage error occured.")));
stream.on("close", async () =>
resolve(await minioClient.removeObjects(DEFAULT_BUCKET!, objects)),
);
stream.on("error", () => reject(new Error("เกิดข้อผิดพลาด ไม่สามารถลบไฟล์ได้")));
});
const searchResult = await esClient.search({
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
query: {
prefix: { pathname: `${cabinetName}/${drawerName}/${folderName}/` },
},
});
await Promise.all(
searchResult.hits.hits.map(async (v) => {
return esClient
.delete({
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
id: v._id,
})
.catch((e) => console.error(`ElasticSearch Error: ${e}`));
}),
);
return this.setStatus(HttpStatusCode.NO_CONTENT);
}
}