From 30ac106894d9238815a82ea08908f3f80e50e98a Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 11 Jul 2025 10:50:05 +0700 Subject: [PATCH] =?UTF-8?q?api=20duplicate=20=E0=B8=A3=E0=B8=B9=E0=B8=9B?= =?UTF-8?q?=E0=B8=A0=E0=B8=B2=E0=B8=9E=E0=B9=82=E0=B8=9B=E0=B8=A3=E0=B9=84?= =?UTF-8?q?=E0=B8=9F=E0=B8=A5=E0=B9=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/AvatarFileController.ts | 46 +++++++++++++++++++++++++ src/services/minio.ts | 14 ++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/controllers/AvatarFileController.ts diff --git a/src/controllers/AvatarFileController.ts b/src/controllers/AvatarFileController.ts new file mode 100644 index 0000000..9137e45 --- /dev/null +++ b/src/controllers/AvatarFileController.ts @@ -0,0 +1,46 @@ +import { + Controller, + Route, + Security, + Tags, + Body, + Path, + Post, + SuccessResponse, + Response, +} from "tsoa"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatusCode from "../interfaces/http-status"; +import { duplicateAvatarFile } from "../services/minio"; + +@Route("api/v1/salary/file/avatar") +@Tags("Avatar") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class AvatarFileController extends Controller { + + /** + * API duplicate avatar file + * + * @summary API duplicate avatar file + * + * @param {string} refId objectRefId ไฟล์รูปภาพเดิมก่อนจะทำซ้ำเพื่อสร้างใหม่เป็นรูปภาพในทะเบียนประวัติ + */ + @Post("{refId}") + @SuccessResponse(200, "Success") + public async duplicateAvatarFile( + @Path() refId: string, + @Body() + body: { + prefix: string, + fileName: string, + } + ) { + await duplicateAvatarFile(refId, body.prefix, body.fileName); + return new HttpSuccess(); + } +} diff --git a/src/services/minio.ts b/src/services/minio.ts index d98ce53..48bd91f 100644 --- a/src/services/minio.ts +++ b/src/services/minio.ts @@ -145,4 +145,18 @@ export async function s3DownloadFile(...pathname: string[]) { }; } +export async function duplicateAvatarFile(refId: string, prefix: string, fileName: string) { + try { + await minio.statObject(MINIO_BUCKET, refId); + const avatar = `${prefix}/${fileName}`; + await minio.copyObject( + MINIO_BUCKET, + avatar, + `/${MINIO_BUCKET}/${refId}` + ); + } catch (error) { + exception(error); + } +} + export const fileLocation = (...path: string[]) => path.join("/");