diff --git a/Services/server/src/controllers/storageController.ts b/Services/server/src/controllers/storageController.ts index 0c94d90..17ea543 100644 --- a/Services/server/src/controllers/storageController.ts +++ b/Services/server/src/controllers/storageController.ts @@ -104,6 +104,13 @@ interface DeleteFileBody { file: string; } +interface DownloadFileBody { + /** @example ["แฟ้ม 1", "แฟ้ม 2", "แฟ้ม 3"] */ + path: string[]; + /** @example "ไฟล์ 1 แก้ไข.xlsx" */ + file: string; +} + async function listFolder(path: string[]) { const list = await new Promise<{ pathname: string; name: string }[]>((resolve, reject) => { const item: { pathname: string; name: string }[] = []; @@ -601,7 +608,7 @@ export class StorageController extends Controller { @Security("bearerAuth", ["management-role", "admin"]) @SuccessResponse(HttpStatusCode.NO_CONTENT, "สำเร็จ") public async deleteFile(@Body() body: DeleteFileBody) { - const pathname = body.path.join("/") + body.file; + const pathname = body.path.join("/") + `/${body.file}`; await minioClient .removeObject(DEFAULT_BUCKET, pathname) @@ -617,4 +624,30 @@ export class StorageController extends Controller { return this.setStatus(HttpStatusCode.NO_CONTENT); } + + @Post("file/download") + @Tags("Download") + @Security("bearerAuth", ["management-role", "admin"]) + @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") + public async downloadFile(@Body() body: DownloadFileBody) { + const pathname = body.path.join("/") + `/${body.file}`; + + const search = await esClient.search }>({ + index: DEFAULT_INDEX!, + query: { + match: { pathname }, + }, + }); + + if (search && search.hits.hits.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบไฟล์"); + } + + const { attachment, ...rest } = search.hits.hits[0]._source!; + + return { + ...rest, + downloadUrl: await minioClient.presignedGetObject(DEFAULT_BUCKET, pathname), + }; + } }