feat: update/delete drawer
Also included subfolder and file
This commit is contained in:
parent
5a120dce76
commit
ea40c17796
1 changed files with 57 additions and 2 deletions
|
|
@ -2,7 +2,6 @@ import {
|
|||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Example,
|
||||
Get,
|
||||
Path,
|
||||
Post,
|
||||
|
|
@ -10,7 +9,6 @@ import {
|
|||
Route,
|
||||
Security,
|
||||
SuccessResponse,
|
||||
Response,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
import * as Minio from "minio";
|
||||
|
|
@ -130,4 +128,61 @@ export class CabinetController extends Controller {
|
|||
this.setStatus(HttpStatusCode.CREATED);
|
||||
return;
|
||||
}
|
||||
|
||||
@Put("/{cabinetName}/drawer/{drawerName}")
|
||||
@Tags("Drawer")
|
||||
@SuccessResponse(HttpStatusCode.NO_CONTENT, "Success")
|
||||
public async editDrawer(
|
||||
@Path() cabinetName: string,
|
||||
@Path() drawerName: string,
|
||||
@Body() body: { name: string },
|
||||
): Promise<void> {
|
||||
const fullpath = `${cabinetName}/${drawerName}/`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const stream = minioClient.listObjectsV2("ehr", fullpath, true);
|
||||
|
||||
stream.on("data", (v) => {
|
||||
if (!(v && v.name)) return;
|
||||
|
||||
const destination = `${cabinetName}/${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", () => {
|
||||
this.setStatus(HttpStatusCode.NO_CONTENT);
|
||||
resolve();
|
||||
});
|
||||
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
||||
});
|
||||
}
|
||||
|
||||
@Delete("/{cabinetName}/drawer/{drawerName}")
|
||||
@Tags("Drawer")
|
||||
@SuccessResponse(HttpStatusCode.OK)
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue