refactor: rabbitmq implement
This commit is contained in:
parent
24350a11a4
commit
3fc70daed0
12 changed files with 676 additions and 545 deletions
|
|
@ -6,120 +6,133 @@ import {
|
|||
Path,
|
||||
Post,
|
||||
Put,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
SuccessResponse,
|
||||
Tags,
|
||||
Request,
|
||||
Response,
|
||||
} from "tsoa";
|
||||
import * as Minio from "minio";
|
||||
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 HttpError from "../interfaces/http-error";
|
||||
import { listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
|
||||
import esClient from "../elasticsearch";
|
||||
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")
|
||||
export class DrawerController extends Controller {
|
||||
@Get("/")
|
||||
@Tags("Drawer")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการลิ้นชักได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
public async listDrawer(@Path() cabinetName: string): Promise<EhrFolder[]> {
|
||||
const fullpath = [cabinetName, ""].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}/`).catch((e) =>
|
||||
console.error(`Error List Folder: ${e}`),
|
||||
);
|
||||
if (!list)
|
||||
throw new Error("เกิดข้อผิดพลาด ไม่สามารถแสดงรายการลิ้นชักได้ กรุณาลองใหม่ในภายหลัง");
|
||||
return list;
|
||||
}
|
||||
|
||||
@Post("/")
|
||||
@Tags("Drawer")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.CREATED)
|
||||
@Security("bearerAuth", ["admin"])
|
||||
@Response(HttpStatusCode.NOT_FOUND, "ไม่พบลิ้นชัก")
|
||||
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดกับระบบจัดการไฟล์")
|
||||
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
|
||||
public async createDrawer(
|
||||
@Request() request: { user: { preferred_username: string } },
|
||||
@Path() cabinetName: string,
|
||||
@Body() body: { name: string },
|
||||
) {
|
||||
if (!(await pathExist(`${cabinetName}/`))) {
|
||||
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "Cabinet cannot be found.");
|
||||
const basePath = `${cabinetName}/`;
|
||||
|
||||
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}/${replaceIllegalChars(body.name)}/.keep`, "", 0, {
|
||||
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("/{drawerName}")
|
||||
@Tags("Drawer")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
||||
@Security("bearerAuth", ["admin"])
|
||||
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดไม่สามารถย้ายไฟล์ได้")
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
|
||||
public async editDrawer(
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
@Body() body: { name: string },
|
||||
): Promise<void> {
|
||||
const fullpath = `${cabinetName}/${drawerName}/`;
|
||||
|
||||
const list = await listItem(fullpath, true);
|
||||
|
||||
const cond = new Minio.CopyConditions();
|
||||
const path = `${cabinetName}/${drawerName}/`;
|
||||
const list = await listItem(DEFAULT_BUCKET!, path, true);
|
||||
|
||||
await Promise.all(
|
||||
list.map(async (current) => {
|
||||
if (!current.name) return;
|
||||
|
||||
const destination = `${cabinetName}/${replaceIllegalChars(body.name)}/${current.name.slice(
|
||||
fullpath.length,
|
||||
path.length,
|
||||
)}`;
|
||||
const source = `/ehr/${current.name}`;
|
||||
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("เกิดข้อผิดพลาด ไม่สามารถย้ายไฟล์ได้");
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
|
@ -129,44 +142,26 @@ export class DrawerController extends Controller {
|
|||
|
||||
@Delete("/{drawerName}")
|
||||
@Tags("Drawer")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
||||
@Security("bearerAuth", ["admin"])
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
|
||||
public async deleteDrawer(@Path() cabinetName: string, @Path() drawerName: string) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const objects: string[] = [];
|
||||
const stream = minioClient.listObjectsV2("ehr", `${cabinetName}/${drawerName}/`, true);
|
||||
const stream = minioClient.listObjectsV2(
|
||||
DEFAULT_BUCKET!,
|
||||
`${cabinetName}/${drawerName}/`,
|
||||
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}/` },
|
||||
},
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue