From 91f7cf2a9be004f682ad063f2245928994821421 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 27 Aug 2024 09:07:14 +0700 Subject: [PATCH] feat: add new endpoint for get or download file --- src/controllers/customer-branch-controller.ts | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/controllers/customer-branch-controller.ts b/src/controllers/customer-branch-controller.ts index f27d3cc..258d330 100644 --- a/src/controllers/customer-branch-controller.ts +++ b/src/controllers/customer-branch-controller.ts @@ -556,6 +556,33 @@ export class CustomerAttachmentController extends Controller { return list.map((v) => v.split("/").at(-1) as string); } + @Get("{filename}") + async getAttachment( + @Request() req: RequestWithUser, + @Path() branchId: string, + @Path() filename: string, + ) { + const record = await prisma.customerBranch.findFirst({ + where: { id: branchId }, + }); + + if (!record) { + throw new HttpError( + HttpStatus.NOT_FOUND, + "Customer branch cannot be found.", + "customerBranchNotFound", + ); + } + + return req.res?.redirect( + await minio.presignedGetObject( + MINIO_BUCKET, + `${attachmentLocation(record.customerId, branchId)}/${filename}`, + 12 * 60 * 60, + ), + ); + } + @Put("{filename}") async addAttachment( @Request() req: RequestWithUser,