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("/");