chore: change dir (prepare to merge)

This commit is contained in:
Methapon2001 2023-11-22 16:16:27 +07:00
parent f3078c47ea
commit 7c55806956
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
31 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,121 @@
import {
Body,
Controller,
Delete,
Get,
Path,
Post,
Put,
Request,
Route,
Security,
SuccessResponse,
Tags,
} from "tsoa";
import * as Minio from "minio";
import minioClient from "../storage";
import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { listFolder, pathExist, replaceIllegalChars } from "../utils/minio";
@Route("/cabinet/{cabinetName}/drawer")
export class DrawerController extends Controller {
@Get("/")
@Tags("Drawer")
@SuccessResponse(HttpStatusCode.OK)
public listDrawer(@Path() cabinetName: string) {
return listFolder(`${cabinetName}/`);
}
@Post("/")
@Tags("Drawer")
@Security("bearerAuth")
@SuccessResponse(HttpStatusCode.CREATED)
public async createDrawer(
@Request() request: { user: { preferred_username: string } },
@Path() cabinetName: string,
@Body() body: { name: string },
) {
if (!(await pathExist(`${cabinetName}/`))) {
throw new HttpError(HttpStatusCode.PRECONDITION_FAILED, "Cabinet cannot be found.");
}
const uploaded = await minioClient
.putObject("ehr", `${cabinetName}/${replaceIllegalChars(body.name)}/.keep`, "", 0, {
createdAt: new Date().toISOString(),
createdBy: request.user.preferred_username,
})
.catch((e) => console.error(e));
if (!uploaded) {
throw new Error("Object storage error occured.");
}
return this.setStatus(HttpStatusCode.CREATED);
}
@Put("/{drawerName}")
@Tags("Drawer")
@Security("bearerAuth")
@SuccessResponse(HttpStatusCode.NO_CONTENT)
public async editDrawer(
@Path() cabinetName: string,
@Path() drawerName: string,
@Body() body: { name: string },
): Promise<void> {
const fullpath = `${cabinetName}/${drawerName}/`;
if (!(await pathExist(fullpath))) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "Resource cannot be found.");
}
return new Promise((resolve, reject) => {
const stream = minioClient.listObjectsV2("ehr", fullpath, true);
stream.on("data", (v) => {
if (!(v && v.name)) return;
const destination = `${cabinetName}/${replaceIllegalChars(body.name)}/${v.name.slice(
fullpath.length,
)}`;
const source = `/ehr/${v.name}`;
const cond = new Minio.CopyConditions();
minioClient.copyObject("ehr", destination, source, cond, (e) => {
if (e) {
return reject(new Error("Failed to move."));
}
return minioClient.removeObject("ehr", v.name);
});
});
stream.on("end", () => {
resolve(this.setStatus(HttpStatusCode.NO_CONTENT));
});
stream.on("error", () => reject(new Error("Object storage error occured.")));
});
}
@Delete("/{drawerName}")
@Tags("Drawer")
@Security("bearerAuth")
@SuccessResponse(HttpStatusCode.NO_CONTENT)
public async deleteDrawer(@Path() cabinetName: string, @Path() drawerName: string) {
return new Promise((resolve, reject) => {
const objects: string[] = [];
const stream = minioClient.listObjectsV2("ehr", `${cabinetName}/${drawerName}/`, 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(true);
});
}
}