From 9e745ee81cb89dc530dbae4900ad9f3904e5c96f Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Mon, 9 Sep 2024 14:02:17 +0700 Subject: [PATCH] feat: branch image multi --- src/controllers/01-branch-controller.ts | 60 ++++++++++++++++++++++++- src/utils/minio.ts | 1 + 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/controllers/01-branch-controller.ts b/src/controllers/01-branch-controller.ts index 3edc99e..d32973d 100644 --- a/src/controllers/01-branch-controller.ts +++ b/src/controllers/01-branch-controller.ts @@ -20,7 +20,7 @@ import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; import minio from "../services/minio"; import { isSystem } from "../utils/keycloak"; -import { deleteFile, fileLocation } from "../utils/minio"; +import { deleteFile, fileLocation, listFile } from "../utils/minio"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -837,4 +837,62 @@ export class BranchController extends Controller { await permissionCheck(req.user, branchId); await deleteFile(fileLocation.branch.bank(branchId, bankId)); } + + @Get("{branchId}/image") + @Security("keycloak") + async listImage(@Path() branchId: string) { + const record = await prisma.branch.findFirst({ + where: { id: branchId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound"); + } + + return await listFile(fileLocation.user.profile(branchId)); + } + + @Get("{branchId}/image/{name}") + async getImageByName( + @Request() req: RequestWithUser, + @Path() branchId: string, + @Path() name: string, + ) { + return req.res?.redirect( + await minio.presignedGetObject( + MINIO_BUCKET, + fileLocation.branch.img(branchId, name), + 12 * 60 * 60, + ), + ); + } + + @Put("{branchId}/image/{name}") + @Security("keycloak") + async putImageByName( + @Request() req: RequestWithUser, + @Path() branchId: string, + @Path() name: string, + ) { + await permissionCheck(req.user, branchId); + return req.res?.redirect( + await minio.presignedPutObject( + MINIO_BUCKET, + fileLocation.branch.img(branchId, name), + 12 * 60 * 60, + ), + ); + } + + @Delete("{branchId}/image/{name}") + async deleteImageByName( + @Request() req: RequestWithUser, + @Path() branchId: string, + @Path() name: string, + ) { + await permissionCheck(req.user, branchId); + await minio.removeObject(MINIO_BUCKET, fileLocation.branch.img(branchId, name), { + forceDelete: true, + }); + } } diff --git a/src/utils/minio.ts b/src/utils/minio.ts index ed29ae0..a62a6b0 100644 --- a/src/utils/minio.ts +++ b/src/utils/minio.ts @@ -44,6 +44,7 @@ export const fileLocation = { image: (branchId: string) => `branch/branch-img-${branchId}`, map: (branchId: string) => `branch/map-img-${branchId}`, bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`, + img: (branchId: string, name?: string) => `user/img-${branchId}/${name || ""}`, }, user: { profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,