2023-11-21 11:46:40 +07:00
|
|
|
import {
|
2023-11-27 13:41:46 +07:00
|
|
|
Body,
|
2023-11-21 11:46:40 +07:00
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Patch,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Request,
|
2023-11-27 13:41:46 +07:00
|
|
|
Response,
|
2023-11-21 11:46:40 +07:00
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
SuccessResponse,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
2023-11-27 13:41:46 +07:00
|
|
|
|
2023-11-21 11:46:40 +07:00
|
|
|
import esClient from "../elasticsearch";
|
2023-11-27 13:41:46 +07:00
|
|
|
import minioClient from "../minio";
|
|
|
|
|
|
2023-11-21 11:46:40 +07:00
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
|
|
|
import { EhrFile } from "../interfaces/ehr-fs";
|
2023-11-27 13:41:46 +07:00
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
|
|
|
|
|
import { copyCond, pathExist } from "../utils/minio";
|
|
|
|
|
|
|
|
|
|
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-21 11:46:40 +07:00
|
|
|
|
|
|
|
|
@Route(
|
|
|
|
|
"/cabinet/{cabinetName}/drawer/{drawerName}/folder/{folderName}/subfolder/{subFolderName}/file",
|
|
|
|
|
)
|
|
|
|
|
export class SubFolderFileController extends Controller {
|
|
|
|
|
@Post("/")
|
|
|
|
|
@Tags("SubFolder File")
|
2023-11-27 13:41:46 +07:00
|
|
|
@Security("bearerAuth", ["admin"])
|
|
|
|
|
@Response(
|
|
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ตำแหน่งที่ระบุไม่พบ กรุณาเตรียมตำแหน่งที่ต้องการก่อนดำเนินการ",
|
|
|
|
|
)
|
|
|
|
|
@SuccessResponse(HttpStatusCode.CREATED, "สำเร็จ")
|
2023-11-21 11:46:40 +07:00
|
|
|
public async uploadFile(
|
|
|
|
|
@Request() request: { user: { preferred_username: string } },
|
2023-11-27 13:41:46 +07:00
|
|
|
@Body()
|
|
|
|
|
body: {
|
|
|
|
|
file: string;
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
category: string;
|
|
|
|
|
keyword: string;
|
|
|
|
|
},
|
2023-11-21 11:46:40 +07:00
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
|
|
|
|
@Path() subFolderName: string,
|
|
|
|
|
) {
|
2023-11-27 13:41:46 +07:00
|
|
|
const pathname = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${body.file}`;
|
2023-11-21 11:46:40 +07:00
|
|
|
|
|
|
|
|
if (!(await pathExist(`${cabinetName}/${drawerName}/${folderName}/${subFolderName}`))) {
|
|
|
|
|
throw new HttpError(
|
2023-11-27 13:41:46 +07:00
|
|
|
HttpStatusCode.NOT_FOUND,
|
|
|
|
|
"ตำแหน่งที่ระบุไม่พบ กรุณาเตรียมตำแหน่งที่ต้องการก่อนดำเนินการ",
|
2023-11-21 11:46:40 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
const result = await esClient
|
|
|
|
|
.search<EhrFile & { attachment?: Record<string, unknown> }>({
|
|
|
|
|
index: DEFAULT_INDEX!,
|
|
|
|
|
query: { match: { pathname } },
|
2023-11-21 11:46:40 +07:00
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
// pathname is unique and should not have multiple record with same path
|
|
|
|
|
if (result && result.hits.hits.length > 0 && result.hits.hits[0]._source) {
|
|
|
|
|
await esClient
|
|
|
|
|
.delete({
|
|
|
|
|
index: DEFAULT_INDEX!,
|
|
|
|
|
id: result.hits.hits[0]._id,
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
}
|
2023-11-21 11:46:40 +07:00
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
const rec = result ? result.hits.hits[0]._source : false;
|
2023-11-21 11:46:40 +07:00
|
|
|
|
|
|
|
|
const metadata: Partial<EhrFile> = {
|
|
|
|
|
pathname,
|
2023-11-27 13:41:46 +07:00
|
|
|
fileName: body.file,
|
|
|
|
|
fileSize: 0,
|
|
|
|
|
fileType: "",
|
|
|
|
|
title: body.title,
|
|
|
|
|
description: body.description,
|
|
|
|
|
category: body.category.split(","),
|
|
|
|
|
keyword: body.keyword.split(","),
|
|
|
|
|
upload: false,
|
|
|
|
|
createdAt: new Date().toISOString(),
|
|
|
|
|
createdBy: rec ? rec.createdBy : "n/a",
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
updatedBy: request.user.preferred_username ?? "n/a",
|
2023-11-21 11:46:40 +07:00
|
|
|
};
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
await esClient.index({
|
|
|
|
|
index: DEFAULT_INDEX!,
|
|
|
|
|
document: metadata,
|
|
|
|
|
});
|
2023-11-21 11:46:40 +07:00
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
return {
|
|
|
|
|
...body,
|
|
|
|
|
createdAt: metadata.createdAt,
|
|
|
|
|
createdBy: metadata.createdBy,
|
|
|
|
|
updatedAt: metadata.updatedAt,
|
|
|
|
|
updatedBy: metadata.updatedBy,
|
|
|
|
|
upload: await minioClient.presignedPutObject(DEFAULT_BUCKET!, pathname),
|
|
|
|
|
};
|
2023-11-21 11:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("/")
|
|
|
|
|
@Tags("SubFolder File")
|
2023-11-27 13:41:46 +07:00
|
|
|
@Security("bearerAuth")
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2023-11-21 11:46:40 +07:00
|
|
|
public async getFile(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
|
|
|
|
@Path() subFolderName: string,
|
2023-11-27 13:41:46 +07:00
|
|
|
): Promise<EhrFile[]> {
|
|
|
|
|
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
|
|
|
|
index: DEFAULT_INDEX!,
|
2023-11-21 11:46:40 +07:00
|
|
|
query: {
|
|
|
|
|
prefix: {
|
|
|
|
|
pathname: `${cabinetName}/${drawerName}/${folderName}/${subFolderName}`,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-11-27 13:46:00 +07:00
|
|
|
size: 10000,
|
2023-11-21 11:46:40 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const records = search.hits.hits
|
|
|
|
|
.map((v) => {
|
2023-11-27 13:41:46 +07:00
|
|
|
if (v._source) {
|
|
|
|
|
const { attachment, ...rest } = v._source;
|
|
|
|
|
return rest satisfies EhrFile;
|
|
|
|
|
}
|
2023-11-21 11:46:40 +07:00
|
|
|
})
|
2023-11-27 13:41:46 +07:00
|
|
|
.filter((v: EhrFile | undefined): v is EhrFile => !!v);
|
2023-11-21 11:46:40 +07:00
|
|
|
|
|
|
|
|
return records;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch("/{fileName}")
|
|
|
|
|
@Tags("SubFolder File")
|
2023-11-27 13:41:46 +07:00
|
|
|
@Security("bearerAuth", ["admin"])
|
|
|
|
|
@Response(HttpStatusCode.NOT_FOUND, "ไม่พบตำแหน่งที่ต้องการสร้างแฟ้ม")
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2023-11-21 11:46:40 +07:00
|
|
|
public async updateFile(
|
|
|
|
|
@Request() request: { user: { preferred_username: string } },
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
|
|
|
|
@Path() subFolderName: string,
|
|
|
|
|
@Path() fileName: string,
|
2023-11-27 13:41:46 +07:00
|
|
|
@Body()
|
|
|
|
|
body: {
|
|
|
|
|
file?: string;
|
|
|
|
|
title?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
category?: string;
|
|
|
|
|
keyword?: string;
|
|
|
|
|
},
|
2023-11-21 11:46:40 +07:00
|
|
|
) {
|
2023-11-27 13:41:46 +07:00
|
|
|
const basePath = `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/`;
|
|
|
|
|
const pathname = `${basePath}${fileName}`;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!Boolean(
|
|
|
|
|
await minioClient.statObject(DEFAULT_BUCKET!, `${pathname}`).catch((e) => {
|
|
|
|
|
if (e.code === "NotFound") return false;
|
|
|
|
|
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบไฟล์");
|
2023-11-21 11:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
// assume user will probably replace file by re-upload but maybe just rename
|
|
|
|
|
if (body.file) {
|
|
|
|
|
const destination = `${basePath}${body.file}`;
|
|
|
|
|
const source = `/${DEFAULT_BUCKET}/${basePath}${fileName}`;
|
|
|
|
|
const copy = await minioClient.copyObject(DEFAULT_BUCKET!, destination, source, copyCond);
|
|
|
|
|
|
|
|
|
|
if (copy) {
|
|
|
|
|
const search = await esClient
|
|
|
|
|
.search<EhrFile & { attachment?: Record<string, unknown> }>({
|
|
|
|
|
index: DEFAULT_INDEX!,
|
|
|
|
|
query: { match: { pathname } },
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
|
|
|
|
|
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
|
|
|
|
const { _index: index, _id: id } = search.hits.hits[0];
|
|
|
|
|
await esClient
|
|
|
|
|
.update({
|
|
|
|
|
index,
|
|
|
|
|
id,
|
|
|
|
|
doc: {
|
|
|
|
|
pathname: destination,
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
updatedBy: request.user.preferred_username ?? "n/a",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.then(() => minioClient.removeObject(DEFAULT_BUCKET!, pathname));
|
|
|
|
|
} else {
|
|
|
|
|
await minioClient.removeObject(DEFAULT_BUCKET!, pathname);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-21 11:46:40 +07:00
|
|
|
} else {
|
2023-11-27 13:41:46 +07:00
|
|
|
const search = await esClient
|
|
|
|
|
.search<EhrFile & { attachment?: Record<string, unknown> }>({
|
|
|
|
|
index: DEFAULT_INDEX!,
|
|
|
|
|
query: { match: { pathname } },
|
2023-11-21 11:46:40 +07:00
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
if (search && search.hits.hits.length > 0 && search.hits.hits[0]._source) {
|
|
|
|
|
const { _index: index, _id: id } = search.hits.hits[0];
|
|
|
|
|
await esClient.update({
|
|
|
|
|
index,
|
|
|
|
|
id,
|
|
|
|
|
doc: {
|
|
|
|
|
...body,
|
|
|
|
|
keyword: body.keyword?.split(","),
|
|
|
|
|
category: body.category?.split(","),
|
|
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
|
updatedBy: request.user.preferred_username ?? "n/a",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-11-21 11:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
return body.file
|
|
|
|
|
? {
|
|
|
|
|
upload: await minioClient.presignedPutObject(
|
|
|
|
|
DEFAULT_BUCKET!,
|
|
|
|
|
`${basePath}${body.file ?? fileName}`,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
: this.setStatus(HttpStatusCode.NO_CONTENT);
|
2023-11-21 11:46:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("/{fileName}")
|
|
|
|
|
@Tags("SubFolder File")
|
2023-11-27 13:41:46 +07:00
|
|
|
@Security("bearerAuth", ["admin"])
|
|
|
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
2023-11-21 11:46:40 +07:00
|
|
|
public async deleteFile(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
|
|
|
|
@Path() subFolderName: string,
|
|
|
|
|
@Path() fileName: string,
|
|
|
|
|
) {
|
|
|
|
|
await minioClient.removeObject(
|
2023-11-27 13:41:46 +07:00
|
|
|
DEFAULT_BUCKET!,
|
|
|
|
|
`${cabinetName}/${drawerName}/${folderName}/${fileName}/${subFolderName}/`,
|
2023-11-21 11:46:40 +07:00
|
|
|
);
|
|
|
|
|
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
|
|
|
|
}
|
2023-11-22 14:45:56 +07:00
|
|
|
|
|
|
|
|
@Get("/{fileName}")
|
2023-11-27 13:41:46 +07:00
|
|
|
@Tags("Download")
|
2023-11-22 14:45:56 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.OK)
|
|
|
|
|
public async downloadFile(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
|
|
|
|
@Path() subFolderName: string,
|
|
|
|
|
@Path() fileName: string,
|
|
|
|
|
) {
|
|
|
|
|
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
2023-11-27 13:41:46 +07:00
|
|
|
index: DEFAULT_INDEX!,
|
2023-11-22 14:45:56 +07:00
|
|
|
query: {
|
|
|
|
|
match: {
|
|
|
|
|
pathname: `${cabinetName}/${drawerName}/${folderName}/${subFolderName}/${fileName}`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (search && search.hits.hits.length === 0) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found");
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 13:41:46 +07:00
|
|
|
const { attachment, ...rest } = search.hits.hits[0]._source!;
|
2023-11-22 14:45:56 +07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
download: await minioClient.presignedGetObject(
|
2023-11-27 13:41:46 +07:00
|
|
|
DEFAULT_BUCKET!,
|
|
|
|
|
`${cabinetName}/${drawerName}/${folderName}/${fileName}`,
|
2023-11-22 14:45:56 +07:00
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-11-21 11:46:40 +07:00
|
|
|
}
|