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

219 lines
7.4 KiB
TypeScript
Raw Normal View History

2023-11-17 14:43:51 +07:00
import {
Body,
Controller,
Delete,
Get,
Path,
Post,
Put,
Route,
2023-11-17 16:43:07 +07:00
Security,
2023-11-17 14:43:51 +07:00
SuccessResponse,
Tags,
2023-11-27 09:45:30 +07:00
Request,
Response,
2023-11-27 16:55:52 +07:00
Example,
2023-11-17 14:43:51 +07:00
} from "tsoa";
2023-11-27 09:45:30 +07:00
import minioClient from "../minio";
import esClient from "../elasticsearch";
2023-11-27 16:07:32 +07:00
import { copyCond, listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
2023-11-27 09:45:30 +07:00
2023-11-17 09:03:31 +07:00
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
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 09:03:31 +07:00
2023-11-21 09:34:38 +07:00
@Route("/cabinet/{cabinetName}/drawer/{drawerName}/folder")
2023-11-17 09:03:31 +07:00
export class FolderController extends Controller {
2023-11-27 16:40:10 +07:00
/**
* @example cabinetName "ตู้เอกสาร 1"
* @example drawerName "ลิ้นชัก 1"
*/
2023-11-21 09:34:38 +07:00
@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 16:55:52 +07:00
@Example([
{
path: "ตู้เอกสาร 1/ลิ้นชัก 1/แฟ้ม 1",
name: "แฟ้ม 1",
createdAt: "2021-07-20T12:33:13.018Z",
createdBy: "admin",
},
{
path: "ตู้เอกสาร 1/ลิ้นชัก 1/แฟ้ม 2",
name: "แฟ้ม 2",
createdAt: "2022-01-23T16:05:02.114Z",
createdBy: "admin",
},
])
2023-11-17 09:03:31 +07:00
public async listFolder(
2023-11-17 14:43:51 +07:00
@Path() cabinetName: string,
@Path() drawerName: string,
2023-11-27 14:11:27 +07:00
): Promise<StorageFolder[]> {
2023-11-27 09:45:30 +07:00
const list = await listFolder(DEFAULT_BUCKET!, `${cabinetName}/${drawerName}`).catch((e) =>
console.error(`Error List Folder: ${e}`),
);
if (!list) throw new Error("เกิดข้อผิดพลาด ไม่สามารถแสดงรายการแฟ้มได้ กรุณาลองใหม่ในภายหลัง");
return list;
2023-11-17 09:03:31 +07:00
}
2023-11-27 16:40:10 +07:00
/**
* @example cabinetName "ตู้เอกสาร 1"
* @example drawerName "ลิ้นชัก 1"
*/
2023-11-21 09:34:38 +07:00
@Post("/")
2023-11-27 14:06:51 +07:00
@Tags("แฟ้ม")
2023-11-27 09:45:30 +07:00
@Security("bearerAuth", ["admin"])
2023-11-27 12:04:14 +07:00
@Response(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างแฟ้ม")
2023-11-27 09:45:30 +07:00
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดกับระบบจัดการไฟล์")
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
2023-11-17 09:03:31 +07:00
public async createFolder(
2023-11-17 16:43:07 +07:00
@Request() request: { user: { preferred_username: string } },
2023-11-27 16:40:10 +07:00
@Body()
body: {
/**
* @example "แฟ้ม 1"
*/
name: string;
},
2023-11-17 14:43:51 +07:00
@Path() cabinetName: string,
@Path() drawerName: string,
2023-11-17 09:03:31 +07:00
) {
2023-11-27 09:45:30 +07:00
const basePath = `${cabinetName}/${drawerName}/`;
2023-11-27 16:07:32 +07:00
if (!(await pathExist(DEFAULT_BUCKET!, basePath))) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างลิ้นชัก");
2023-11-17 09:03:31 +07:00
}
2023-11-27 09:45:30 +07:00
const created = await minioClient
.putObject(DEFAULT_BUCKET!, `${basePath}${replaceIllegalChars(body.name)}/.keep`, "", 0, {
createdAt: new Date().toISOString(),
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
2023-11-17 14:43:51 +07:00
return this.setStatus(HttpStatusCode.CREATED);
2023-11-17 09:03:31 +07:00
}
2023-11-27 16:40:10 +07:00
/**
* @example cabinetName "ตู้เอกสาร 1"
* @example drawerName "ลิ้นชัก 1"
* @example folderName "แฟ้ม 1"
*/
2023-11-21 09:34:38 +07:00
@Put("/{folderName}")
2023-11-27 14:06:51 +07:00
@Tags("แฟ้ม")
2023-11-27 09:45:30 +07:00
@Security("bearerAuth", ["admin"])
@Response(HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาดไม่สามารถย้ายไฟล์ได้")
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
2023-11-17 09:03:31 +07:00
public async editFolder(
2023-11-27 16:40:10 +07:00
@Body()
body: {
/**
* @example "แฟ้มใหม่"
*/
name: string;
},
@Path() cabinetName: string,
@Path() drawerName: string,
@Path() folderName: string,
2023-11-17 09:03:31 +07:00
) {
2023-11-30 09:33:12 +07:00
const path = `${cabinetName}/${drawerName}/${folderName}/`;
2023-11-27 09:45:30 +07:00
const list = await listItem(DEFAULT_BUCKET!, path, true);
2023-11-17 14:43:51 +07:00
await Promise.all(
list.map(async (current) => {
if (!current.name) return;
2023-11-17 14:43:51 +07:00
2023-11-29 17:43:55 +07:00
const base = `${cabinetName}/${drawerName}/${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}`;
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 16:07:32 +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 14:43:51 +07:00
}
2023-11-27 16:40:10 +07:00
/**
* @example cabinetName "ตู้เอกสาร 1"
* @example drawerName "ลิ้นชัก 1"
* @example folderName "แฟ้ม 1"
*/
2023-11-21 09:34:38 +07:00
@Delete("/{folderName}")
2023-11-27 14:06:51 +07:00
@Tags("แฟ้ม")
2023-11-27 09:45:30 +07:00
@Security("bearerAuth", ["admin"])
@SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ")
2023-11-17 14:43:51 +07:00
public async deleteFolder(
@Path() cabinetName: string,
@Path() drawerName: string,
@Path() folderName: string,
) {
await new Promise<void>((resolve, reject) => {
2023-11-17 14:43:51 +07:00
const objects: string[] = [];
const stream = minioClient.listObjectsV2(
2023-11-27 09:45:30 +07:00
DEFAULT_BUCKET!,
`${cabinetName}/${drawerName}/${folderName}`,
2023-11-17 14:43:51 +07:00
true,
);
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:51 +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-17 14:43:51 +07:00
});
return this.setStatus(HttpStatusCode.NO_CONTENT);
2023-11-17 09:03:31 +07:00
}
}