From 8cd393151ee55fbbe5190167e15c13c8e757fbb4 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 1 Aug 2024 09:53:16 +0700 Subject: [PATCH] feat: image endpoint (branch endpoint) --- src/controllers/branch-controller.ts | 84 +++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index a4b5f10..fd82fac 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -18,7 +18,7 @@ import prisma from "../db"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; -import minio from "../services/minio"; +import minio, { presignedGetObjectIfExist } from "../services/minio"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -533,4 +533,86 @@ export class BranchController extends Controller { where: { id: branchId }, }); } + + @Get("{branchId}/line-image") + async getLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { + const url = await presignedGetObjectIfExist(MINIO_BUCKET, lineImageLoc(branchId), 60 * 60); + + if (!url) { + throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); + } + + return req.res?.redirect(url); + } + + @Put("{branchId}/line-image") + @Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"]) + async setLineImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { + const record = await prisma.branch.findUnique({ + include: { + user: { where: { userId: req.user.sub } }, + }, + where: { id: branchId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound"); + } + + if ( + !["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) && + !record?.user.find((v) => v.userId === req.user.sub) + ) { + throw new HttpError( + HttpStatus.FORBIDDEN, + "You do not have permission to perform this action.", + "noPermission", + ); + } + + return req.res?.redirect( + await minio.presignedPutObject(MINIO_BUCKET, lineImageLoc(record.id), 12 * 60 * 60), + ); + } + + @Get("{branchId}/branch-image") + async getBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { + const url = await presignedGetObjectIfExist(MINIO_BUCKET, branchImageLoc(branchId), 60 * 60); + + if (!url) { + throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound"); + } + + return req.res?.redirect(url); + } + + @Put("{branchId}/branch-image") + @Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"]) + async setBranchImageByBranchId(@Request() req: RequestWithUser, @Path() branchId: string) { + const record = await prisma.branch.findUnique({ + include: { + user: { where: { userId: req.user.sub } }, + }, + where: { id: branchId }, + }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "branchNotFound"); + } + + if ( + !["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) && + !record?.user.find((v) => v.userId === req.user.sub) + ) { + throw new HttpError( + HttpStatus.FORBIDDEN, + "You do not have permission to perform this action.", + "noPermission", + ); + } + + return req.res?.redirect( + await minio.presignedPutObject(MINIO_BUCKET, branchImageLoc(record.id), 12 * 60 * 60), + ); + } }