From 4e3366654dd791c1e61803a9185441ec838b42f9 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 22 Nov 2023 12:58:45 +0700 Subject: [PATCH] feat: file download --- .../server/src/controllers/fileController.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Prototype/server/src/controllers/fileController.ts b/Prototype/server/src/controllers/fileController.ts index fe9d1f9..8c5fb09 100644 --- a/Prototype/server/src/controllers/fileController.ts +++ b/Prototype/server/src/controllers/fileController.ts @@ -278,4 +278,43 @@ export class FileController extends Controller { return this.setStatus(HttpStatusCode.NO_CONTENT); } + + @Get("/{fileName}") + @Tags("File") + @SuccessResponse(HttpStatusCode.OK) + public async downloadFile( + @Path() cabinetName: string, + @Path() drawerName: string, + @Path() folderName: string, + @Path() fileName: string, + ) { + const search = await esClient.search }>({ + index: "ehr-api-client", + query: { + match: { + pathname: `${cabinetName}/${drawerName}/${folderName}/${fileName}`, + }, + }, + }); + + if (search && search.hits.hits.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "Not found"); + } + + const data = search.hits.hits[0]._source; + + if (!data) { + throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "Found data but no info."); + } + + const { attachment, ...rest } = data; + + return { + ...rest, + download: await minioClient.presignedGetObject( + "ehr", + `${cabinetName}/${drawerName}/${folderName}/${fileName}`, + ), + }; + } }