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}`, + ), + }; + } }