2023-11-17 16:43:07 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Tags,
|
2023-11-27 09:45:30 +07:00
|
|
|
Request,
|
|
|
|
|
Response,
|
2023-11-17 16:43:07 +07:00
|
|
|
} from "tsoa";
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-27 09:45:30 +07:00
|
|
|
import minioClient from "../minio";
|
2023-11-23 10:34:26 +07:00
|
|
|
import esClient from "../elasticsearch";
|
2023-11-27 09:45:30 +07:00
|
|
|
|
|
|
|
|
import { copyCond, listFolder, listItem, replaceIllegalChars } from "../utils/minio";
|
|
|
|
|
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
2023-11-23 10:34:26 +07:00
|
|
|
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
|
2023-11-27 09:45:30 +07:00
|
|
|
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.");
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Route("/cabinet/{cabinetName}/drawer")
|
2023-11-17 14:43:23 +07:00
|
|
|
export class DrawerController extends Controller {
|
2023-11-21 09:24:46 +07:00
|
|
|
@Get("/")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-27 09:45:30 +07:00
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
|
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการลิ้นชักได้ กรุณาลองใหม่ในภายหลัง",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2023-11-23 10:34:26 +07:00
|
|
|
public async listDrawer(@Path() cabinetName: string): Promise<EhrFolder[]> {
|
2023-11-27 09:45:30 +07:00
|
|
|
const list = await listFolder(DEFAULT_BUCKET!, `${cabinetName}/`).catch((e) =>
|
|
|
|
|
console.error(`Error List Folder: ${e}`),
|
|
|
|
|
);
|
|
|
|
|
if (!list)
|
|
|
|
|
throw new Error("เกิดข้อผิดพลาด ไม่สามารถแสดงรายการลิ้นชักได้ กรุณาลองใหม่ในภายหลัง");
|
|
|
|
|
return list;
|
2023-11-17 14:43:23 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Post("/")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-27 09:45:30 +07:00
|
|
|
@Security("bearerAuth", ["admin"])
|
|
|
|
|
@Response(HttpStatusCode.NOT_FOUND, "ไม่พบลิ้นชัก")
|
|
|
|
|
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดกับระบบจัดการไฟล์")
|
|
|
|
|
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
|
2023-11-17 16:43:07 +07:00
|
|
|
public async createDrawer(
|
|
|
|
|
@Request() request: { user: { preferred_username: string } },
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Body() body: { name: string },
|
|
|
|
|
) {
|
2023-11-27 09:45:30 +07:00
|
|
|
const basePath = `${cabinetName}/`;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!Boolean(
|
|
|
|
|
await minioClient.statObject(DEFAULT_BUCKET!, `${basePath}.keep`).catch((e) => {
|
|
|
|
|
if (e.code === "NotFound") return false;
|
|
|
|
|
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
) {
|
2023-11-27 12:04:14 +07:00
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก");
|
2023-11-17 14:43:23 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-27 09:45:30 +07:00
|
|
|
const created = await minioClient
|
|
|
|
|
.putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, {
|
2023-11-17 14:43:23 +07:00
|
|
|
createdAt: new Date().toISOString(),
|
2023-11-17 16:43:07 +07:00
|
|
|
createdBy: request.user.preferred_username,
|
2023-11-17 14:43:23 +07:00
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
|
2023-11-27 09:45:30 +07:00
|
|
|
if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
|
2023-11-17 14:43:23 +07:00
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Put("/{drawerName}")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-27 09:45:30 +07:00
|
|
|
@Security("bearerAuth", ["admin"])
|
|
|
|
|
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดไม่สามารถย้ายไฟล์ได้")
|
|
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
|
2023-11-17 14:43:23 +07:00
|
|
|
public async editDrawer(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Body() body: { name: string },
|
|
|
|
|
): Promise<void> {
|
2023-11-27 09:45:30 +07:00
|
|
|
const path = `${cabinetName}/${drawerName}/`;
|
|
|
|
|
const list = await listItem(DEFAULT_BUCKET!, path, true);
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
await Promise.all(
|
|
|
|
|
list.map(async (current) => {
|
|
|
|
|
if (!current.name) return;
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const destination = `${cabinetName}/${replaceIllegalChars(body.name)}/${current.name.slice(
|
2023-11-27 09:45:30 +07:00
|
|
|
path.length,
|
2023-11-21 10:01:57 +07:00
|
|
|
)}`;
|
2023-11-27 09:45:30 +07:00
|
|
|
const source = `/${DEFAULT_BUCKET}/${current.name}`;
|
2023-11-23 10:34:26 +07:00
|
|
|
|
|
|
|
|
return await minioClient
|
2023-11-27 09:45:30 +07:00
|
|
|
.copyObject(DEFAULT_BUCKET!, destination, source, copyCond)
|
2023-11-23 10:34:26 +07:00
|
|
|
.then(async () => {
|
2023-11-27 09:45:30 +07:00
|
|
|
if (current.name.includes(".keep")) {
|
|
|
|
|
return await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
|
|
|
|
|
}
|
2023-11-23 10:34:26 +07:00
|
|
|
|
|
|
|
|
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
2023-11-27 09:45:30 +07:00
|
|
|
index: DEFAULT_INDEX!,
|
|
|
|
|
query: { match: { pathname: current.name } },
|
2023-11-23 10:34:26 +07:00
|
|
|
});
|
|
|
|
|
|
2023-11-27 09:45:30 +07:00
|
|
|
if (search && search.hits.hits.length === 0) throw new Error("ไม่พบข้อมูลในฐานข้อมูล");
|
2023-11-23 10:34:26 +07:00
|
|
|
|
|
|
|
|
const data = search.hits.hits[0];
|
|
|
|
|
|
|
|
|
|
await esClient.update({
|
2023-11-27 09:45:30 +07:00
|
|
|
index: DEFAULT_INDEX!,
|
2023-11-23 10:34:26 +07:00
|
|
|
id: data._id,
|
|
|
|
|
doc: { pathname: destination },
|
|
|
|
|
});
|
2023-11-27 09:45:30 +07:00
|
|
|
|
|
|
|
|
await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
|
2023-11-23 10:34:26 +07:00
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.error(e);
|
2023-11-27 09:45:30 +07:00
|
|
|
throw new Error("เกิดข้อผิดพลาด ไม่สามารถย้ายไฟล์ได้");
|
2023-11-23 10:34:26 +07:00
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
2023-11-17 14:43:23 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:24:46 +07:00
|
|
|
@Delete("/{drawerName}")
|
2023-11-17 14:43:23 +07:00
|
|
|
@Tags("Drawer")
|
2023-11-27 09:45:30 +07:00
|
|
|
@Security("bearerAuth", ["admin"])
|
|
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
|
2023-11-17 14:43:23 +07:00
|
|
|
public async deleteDrawer(@Path() cabinetName: string, @Path() drawerName: string) {
|
2023-11-24 11:32:04 +07:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2023-11-17 14:43:23 +07:00
|
|
|
const objects: string[] = [];
|
2023-11-27 09:45:30 +07:00
|
|
|
const stream = minioClient.listObjectsV2(
|
|
|
|
|
DEFAULT_BUCKET!,
|
|
|
|
|
`${cabinetName}/${drawerName}/`,
|
|
|
|
|
true,
|
|
|
|
|
);
|
2023-11-17 14:43:23 +07:00
|
|
|
|
|
|
|
|
stream.on("data", (v) => {
|
2023-11-27 09:45:30 +07:00
|
|
|
if (v && v.name) objects.push(v.name);
|
2023-11-17 14:43:23 +07:00
|
|
|
});
|
2023-11-27 09:45:30 +07:00
|
|
|
stream.on("close", async () =>
|
|
|
|
|
resolve(await minioClient.removeObjects(DEFAULT_BUCKET!, objects)),
|
|
|
|
|
);
|
|
|
|
|
stream.on("error", () => reject(new Error("เกิดข้อผิดพลาด ไม่สามารถลบไฟล์ได้")));
|
2023-11-24 11:32:04 +07:00
|
|
|
});
|
2023-11-17 14:43:23 +07:00
|
|
|
|
2023-11-24 11:32:04 +07:00
|
|
|
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
2023-11-17 14:43:23 +07:00
|
|
|
}
|
|
|
|
|
}
|