hrms-edm/Services/server/src/controllers/cabinetController.ts

186 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-11-17 16:43:07 +07:00
import {
Body,
Controller,
Delete,
Get,
Path,
Post,
Put,
Route,
Security,
SuccessResponse,
Tags,
Request,
2023-11-27 09:45:30 +07:00
Response,
2023-11-27 14:32:08 +07:00
Example,
2023-11-17 16:43:07 +07:00
} from "tsoa";
2023-11-17 09:03:31 +07:00
2023-11-27 09:45:30 +07:00
import minioClient from "../minio";
import esClient from "../elasticsearch";
2023-11-17 09:03:31 +07:00
2023-11-27 09:45:30 +07:00
import { copyCond, listFolder, listItem, replaceIllegalChars } from "../utils/minio";
import HttpStatusCode from "../interfaces/http-status";
2023-11-27 14:11:27 +07:00
import { StorageFile, StorageFolder } from "../interfaces/storage-fs";
2023-11-27 09:45:30 +07:00
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 09:03:31 +07:00
@Route("cabinet")
export class CabinetController extends Controller {
@Get("/")
2023-11-27 14:06:51 +07:00
@Tags("ตู้เอกสาร")
2023-11-27 09:45:30 +07:00
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการตู้เอกสารได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
2023-11-27 14:32:08 +07:00
@Example([
{
path: "ตู้เอกสาร 1/",
name: "ตู้เอกสาร 1",
createdAt: "2021-07-20T12:33:13.018Z",
createdBy: "admin",
},
{
path: "ตู้เอกสาร 2/",
name: "ตู้เอกสาร 2",
createdAt: "2022-01-23T16:05:02.114Z",
createdBy: "admin",
},
])
2023-11-27 14:11:27 +07:00
public async listCabinet(): Promise<StorageFolder[]> {
2023-11-27 09:45:30 +07:00
const list = await listFolder(DEFAULT_BUCKET!).catch((e) =>
console.error(`Error List Folder: ${e}`),
);
if (!list)
throw new Error("เกิดข้อผิดพลาด ไม่สามารถแสดงรายการตู้เอกสารได้ กรุณาลองใหม่ในภายหลัง");
return list;
2023-11-17 09:03:31 +07:00
}
@Post("/")
2023-11-27 14:06:51 +07:00
@Tags("ตู้เอกสาร")
@Security("bearerAuth", ["admin", "management-role"])
2023-11-27 09:45:30 +07:00
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดกับระบบจัดการไฟล์")
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
2023-11-17 16:43:07 +07:00
public async createCabinet(
@Request() request: { user: { preferred_username: string } },
2023-11-27 14:32:08 +07:00
@Body()
body: {
/**
* @example "ตู้เอกสาร 1"
*/
name: string;
},
2023-11-17 16:43:07 +07:00
) {
2023-11-27 09:45:30 +07:00
const created = await minioClient
.putObject(DEFAULT_BUCKET!, `${replaceIllegalChars(body.name)}/.keep`, "", 0, {
2023-11-17 09:03:31 +07:00
createdAt: new Date().toISOString(),
2023-11-17 16:43:07 +07:00
createdBy: request.user.preferred_username,
2023-11-17 09:03:31 +07:00
})
.catch((e) => console.error(e));
2023-11-27 09:45:30 +07:00
if (!created) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
2023-11-17 09:03:31 +07:00
return this.setStatus(HttpStatusCode.CREATED);
}
2023-11-27 14:32:08 +07:00
/**
* @example cabinetName "ตู้เอกสาร 1"
*/
2023-11-17 09:03:31 +07:00
@Put("/{cabinetName}")
2023-11-27 14:06:51 +07:00
@Tags("ตู้เอกสาร")
@Security("bearerAuth", ["admin", "management-role"])
2023-11-27 09:45:30 +07:00
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดไม่สามารถย้ายไฟล์ได้")
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
2023-11-17 09:03:31 +07:00
public async editCabinet(
@Path() cabinetName: string,
2023-11-27 14:32:08 +07:00
@Body()
body: {
/**
* @example "ตู้เอกสารใหม่"
*/
name: string;
},
2023-11-17 09:03:31 +07:00
): Promise<void> {
2023-11-27 09:45:30 +07:00
const path = `${cabinetName}/`;
const list = await listItem(DEFAULT_BUCKET!, path, true);
await Promise.all(
list.map(async (current) => {
if (!current.name) return;
2023-11-17 09:03:31 +07:00
2023-11-29 17:43:55 +07:00
const base = `${replaceIllegalChars(body.name)}/`;
const destination = `${base}${current.name.slice(path.length)}`;
2023-11-27 09:45:30 +07:00
const source = `/${DEFAULT_BUCKET}/${current.name}`;
2023-11-17 09:03:31 +07:00
return await minioClient
2023-11-27 09:45:30 +07:00
.copyObject(DEFAULT_BUCKET!, destination, source, copyCond)
.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-27 14:32:08 +07:00
const search = await esClient.search<
StorageFile & { attachment: Record<string, string> }
>({
2023-11-27 09:45:30 +07:00
index: DEFAULT_INDEX!,
query: { match: { pathname: current.name } },
});
2023-11-27 09:45:30 +07:00
if (search && search.hits.hits.length === 0) throw new Error("ไม่พบข้อมูลในฐานข้อมูล");
const data = search.hits.hits[0];
await esClient.update({
2023-11-27 09:45:30 +07:00
index: DEFAULT_INDEX!,
id: data._id,
2023-11-29 17:51:34 +07:00
doc: {
pathname: destination,
path: destination.split("/").slice(0, -1).join("/") + "/",
},
2023-11-29 17:18:08 +07:00
refresh: "wait_for",
});
2023-11-27 09:45:30 +07:00
await minioClient.removeObject(DEFAULT_BUCKET!, current.name);
})
.catch((e) => {
console.error(e);
2023-11-27 09:45:30 +07:00
throw new Error("เกิดข้อผิดพลาด ไม่สามารถย้ายไฟล์ได้");
});
}),
);
return this.setStatus(HttpStatusCode.NO_CONTENT);
2023-11-17 09:03:31 +07:00
}
2023-11-27 14:32:08 +07:00
/**
* @example cabinetName "ตู้เอกสาร 1"
*/
2023-11-17 09:03:31 +07:00
@Delete("/{cabinetName}")
2023-11-27 14:06:51 +07:00
@Tags("ตู้เอกสาร")
@Security("bearerAuth", ["admin", "management-role"])
2023-11-27 09:45:30 +07:00
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถลบไฟล์ได้")
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
2023-11-17 09:03:31 +07:00
public async deleteCabinet(@Path() cabinetName: string) {
await new Promise<void>((resolve, reject) => {
2023-11-17 09:03:31 +07:00
const objects: string[] = [];
2023-11-27 09:45:30 +07:00
const stream = minioClient.listObjectsV2(DEFAULT_BUCKET!, `${cabinetName}/`, true);
2023-11-17 09:03:31 +07:00
stream.on("data", (v) => {
2023-11-27 09:45:30 +07:00
if (v && v.name) objects.push(v.name);
});
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-17 09:03:31 +07:00
return this.setStatus(HttpStatusCode.NO_CONTENT);
}
2023-11-17 09:03:31 +07:00
}