2023-11-17 14:43:51 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
2023-11-17 16:43:07 +07:00
|
|
|
Request,
|
2023-11-17 14:43:51 +07:00
|
|
|
Route,
|
2023-11-17 16:43:07 +07:00
|
|
|
Security,
|
2023-11-17 14:43:51 +07:00
|
|
|
SuccessResponse,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import * as Minio from "minio";
|
|
|
|
|
|
2023-11-17 09:03:31 +07:00
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
2023-11-23 10:34:26 +07:00
|
|
|
import { listFolder, listItem, pathExist, replaceIllegalChars } from "../utils/minio";
|
|
|
|
|
import { EhrFile, EhrFolder } from "../interfaces/ehr-fs";
|
2023-11-17 09:03:31 +07:00
|
|
|
import minioClient from "../storage";
|
2023-11-23 10:34:26 +07:00
|
|
|
import esClient from "../elasticsearch";
|
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-21 09:34:38 +07:00
|
|
|
@Get("/")
|
2023-11-17 09:03:31 +07:00
|
|
|
@Tags("Folder")
|
2023-11-17 14:43:51 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.OK)
|
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-17 09:03:31 +07:00
|
|
|
): Promise<EhrFolder[]> {
|
2023-11-23 10:34:26 +07:00
|
|
|
const fullpath = [cabinetName, drawerName, ""].join("/");
|
2023-11-17 09:03:31 +07:00
|
|
|
|
|
|
|
|
if (!(await pathExist(fullpath))) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "Provided path does not exist.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return listFolder(fullpath);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:34:38 +07:00
|
|
|
@Post("/")
|
2023-11-17 09:03:31 +07:00
|
|
|
@Tags("Folder")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 14:43:51 +07:00
|
|
|
@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-17 09:03:31 +07:00
|
|
|
@Body() body: { 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-17 14:43:51 +07:00
|
|
|
if (!(await pathExist(`${cabinetName}/${drawerName}/`))) {
|
|
|
|
|
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "Cabinet or drawer cannot be found.");
|
2023-11-17 09:03:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uploaded = await minioClient
|
2023-11-21 10:01:57 +07:00
|
|
|
.putObject(
|
|
|
|
|
"ehr",
|
|
|
|
|
`${cabinetName}/${drawerName}/${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));
|
|
|
|
|
|
|
|
|
|
if (!uploaded) {
|
|
|
|
|
throw new Error("Object storage error occured.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 14:43:51 +07:00
|
|
|
return this.setStatus(HttpStatusCode.CREATED);
|
2023-11-17 09:03:31 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:34:38 +07:00
|
|
|
@Put("/{folderName}")
|
2023-11-17 09:03:31 +07:00
|
|
|
@Tags("Folder")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 14:43:51 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
2023-11-17 09:03:31 +07:00
|
|
|
public async editFolder(
|
|
|
|
|
@Body() body: { name: string },
|
2023-11-23 10:34:26 +07:00
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
2023-11-17 09:03:31 +07:00
|
|
|
) {
|
2023-11-23 10:34:26 +07:00
|
|
|
const fullpath = `${cabinetName}/${drawerName}/${folderName}`;
|
2023-11-17 09:03:31 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const list = await listItem(fullpath, true);
|
2023-11-17 09:03:31 +07:00
|
|
|
|
2023-11-23 10:34:26 +07:00
|
|
|
const cond = new Minio.CopyConditions();
|
2023-11-17 14:43:51 +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:51 +07:00
|
|
|
|
2023-11-21 10:01:57 +07:00
|
|
|
const destination = `${cabinetName}/${drawerName}/${replaceIllegalChars(
|
|
|
|
|
body.name,
|
2023-11-23 10:34:26 +07:00
|
|
|
)}/${current.name.slice(fullpath.length)}`;
|
|
|
|
|
const source = `/ehr/${current.name}`;
|
|
|
|
|
|
|
|
|
|
return await minioClient
|
|
|
|
|
.copyObject("ehr", destination, source, cond)
|
|
|
|
|
.then(async () => {
|
|
|
|
|
if (!current.name) return;
|
|
|
|
|
|
|
|
|
|
await minioClient.removeObject("ehr", current.name);
|
|
|
|
|
|
|
|
|
|
if (current.name.includes(".keep")) return;
|
|
|
|
|
|
|
|
|
|
const search = await esClient.search<EhrFile & { attachment: Record<string, string> }>({
|
|
|
|
|
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
|
|
|
|
query: {
|
|
|
|
|
match: {
|
|
|
|
|
pathname: current.name,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (search && search.hits.hits.length === 0) {
|
|
|
|
|
throw new Error("Data cannot be found in database.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = search.hits.hits[0];
|
|
|
|
|
|
|
|
|
|
await esClient.update({
|
|
|
|
|
index: process.env.ELASTICSEARCH_INDEX ?? "ehr-index",
|
|
|
|
|
id: data._id,
|
|
|
|
|
doc: { pathname: destination },
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
throw new Error("Failed to move.");
|
|
|
|
|
});
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return this.setStatus(HttpStatusCode.NO_CONTENT);
|
2023-11-17 14:43:51 +07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 09:34:38 +07:00
|
|
|
@Delete("/{folderName}")
|
2023-11-17 14:43:51 +07:00
|
|
|
@Tags("Folder")
|
2023-11-17 16:43:07 +07:00
|
|
|
@Security("bearerAuth")
|
2023-11-17 14:43:51 +07:00
|
|
|
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
|
|
|
|
public async deleteFolder(
|
|
|
|
|
@Path() cabinetName: string,
|
|
|
|
|
@Path() drawerName: string,
|
|
|
|
|
@Path() folderName: string,
|
|
|
|
|
) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const objects: string[] = [];
|
|
|
|
|
const stream = minioClient.listObjectsV2(
|
|
|
|
|
"ehr",
|
|
|
|
|
`${cabinetName}/${drawerName}/${folderName}`,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
stream.on("data", (v) => {
|
|
|
|
|
if (!(v && v.name)) return;
|
|
|
|
|
|
|
|
|
|
objects.push(v.name);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
stream.on("close", () => minioClient.removeObjects("ehr", objects));
|
|
|
|
|
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
|
|
|
|
|
|
|
|
|
resolve(this.setStatus(HttpStatusCode.NO_CONTENT));
|
|
|
|
|
});
|
2023-11-17 09:03:31 +07:00
|
|
|
}
|
|
|
|
|
}
|