chore: change dir (prepare to merge)
This commit is contained in:
parent
f3078c47ea
commit
7c55806956
31 changed files with 0 additions and 0 deletions
108
Services/server/src/controllers/cabinetController.ts
Normal file
108
Services/server/src/controllers/cabinetController.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Path,
|
||||
Post,
|
||||
Put,
|
||||
Route,
|
||||
Security,
|
||||
SuccessResponse,
|
||||
Tags,
|
||||
Request,
|
||||
} from "tsoa";
|
||||
import * as Minio from "minio";
|
||||
import minioClient from "../storage";
|
||||
|
||||
import { EhrFolder } from "../interfaces/ehr-fs";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { listFolder, replaceIllegalChars } from "../utils/minio";
|
||||
|
||||
@Route("cabinet")
|
||||
export class CabinetController extends Controller {
|
||||
@Get("/")
|
||||
@Tags("Cabinet")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
public listCabinet(): Promise<EhrFolder[]> {
|
||||
return listFolder();
|
||||
}
|
||||
|
||||
@Post("/")
|
||||
@Tags("Cabinet")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.CREATED)
|
||||
public async createCabinet(
|
||||
@Request() request: { user: { preferred_username: string } },
|
||||
@Body() body: { name: string },
|
||||
) {
|
||||
const uploaded = await minioClient
|
||||
.putObject("ehr", `${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("/{cabinetName}")
|
||||
@Tags("Cabinet")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT, "Success")
|
||||
public async editCabinet(
|
||||
@Path() cabinetName: string,
|
||||
@Body() body: { name: string },
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const stream = minioClient.listObjectsV2("ehr", `${cabinetName}/`, true);
|
||||
|
||||
stream.on("data", (v) => {
|
||||
if (!(v && v.name)) return;
|
||||
|
||||
const destination = `${replaceIllegalChars(body.name)}/${v.name.slice(
|
||||
cabinetName.length + 1,
|
||||
)}`;
|
||||
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", () => {
|
||||
this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
resolve();
|
||||
});
|
||||
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
||||
});
|
||||
}
|
||||
|
||||
@Delete("/{cabinetName}")
|
||||
@Tags("Cabinet")
|
||||
@Security("bearerAuth")
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT)
|
||||
public async deleteCabinet(@Path() cabinetName: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const objects: string[] = [];
|
||||
const stream = minioClient.listObjectsV2("ehr", `${cabinetName}/`, 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));
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue