From 2bcd7506a4145ebc4e85a7fd5f48131ab743d54d Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 8 Apr 2026 15:13:29 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=95=E0=B8=B1=E0=B8=94=E0=B8=8A=E0=B8=B7?= =?UTF-8?q?=E0=B9=88=E0=B8=AD=E0=B9=84=E0=B8=9F=E0=B8=A5=E0=B9=8C=E0=B8=A2?= =?UTF-8?q?=E0=B8=B2=E0=B8=A7=E0=B9=80=E0=B8=81=E0=B8=B4=E0=B8=99=20#2397?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/FileController.ts | 6 +++--- src/services/minio.ts | 29 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index 0a1dd32..3359dcc 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -22,7 +22,7 @@ import { listFile, updateFile, } from "../services/edm"; -import { s3DeleteFile, s3DownloadFile, s3ListFile, s3UploadFile } from "../services/minio"; +import { s3DeleteFile, s3DownloadFile, s3ListFile, s3UploadFile, truncateFileName } from "../services/minio"; @Route("api/v1/salary/file/{name}/{group}") @Security("bearerAuth") @@ -257,7 +257,7 @@ export class FileController extends Controller { const map = fileList.map(async ({ fileName, ...props }) => [ fileName, - await createFile(path, fileName, props), + await createFile(path, truncateFileName(fileName), props), ]); const result = await Promise.all(map).catch((e) => @@ -607,7 +607,7 @@ export class SubFileController extends Controller { const map = fileList.map(async ({ fileName, ...props }) => [ fileName, - await createFile(path, fileName, props), + await createFile(path, truncateFileName(fileName), props), ]); const result = await Promise.all(map).catch((e) => diff --git a/src/services/minio.ts b/src/services/minio.ts index 48bd91f..940d5ca 100644 --- a/src/services/minio.ts +++ b/src/services/minio.ts @@ -16,6 +16,26 @@ const minio = new Client({ export default minio; +/** + * ตัดชื่อไฟล์ถ้ายาวเกิน limit + * @param fileName - ชื่อไฟล์เดิม + * @param maxLength - ความยาวสูงสุดของชื่อไฟล์ (default: 50 ตัวอักษร ไม่รวมนามสกุล) + * @returns ชื่อไฟล์ที่ตัดแล้ว + */ +export function truncateFileName(fileName: string, maxLength: number = 50): string { + const dot = fileName.lastIndexOf("."); + const name = dot !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dot) : fileName; + const ext = dot !== -1 && !fileName.startsWith(".") ? fileName.slice(dot) : ""; + + if (name.length <= maxLength) { + return fileName; + } + + // ตัดชื่อและเติม "..." ตามด้วย 5 ตัวสุดท้ายของชื่อเดิม + const truncated = name.slice(0, maxLength - 8) + "..." + name.slice(-5); + return truncated + ext; +} + function exception(e: any): never { console.error(e); throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์"); @@ -74,6 +94,11 @@ export async function s3DeleteFolder(path: string) { export async function s3UploadFile(replace: boolean, ...pathname: string[]) { if (!pathname.length) return; + // ตัดชื่อไฟล์ถ้ายาวเกิน + const originalFileName = pathname.at(-1) as string; + const truncatedFileName = truncateFileName(originalFileName); + pathname = [...pathname.slice(0, -1), truncatedFileName]; + if (!replace) { const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception); @@ -146,7 +171,7 @@ export async function s3DownloadFile(...pathname: string[]) { } export async function duplicateAvatarFile(refId: string, prefix: string, fileName: string) { - try { + try { await minio.statObject(MINIO_BUCKET, refId); const avatar = `${prefix}/${fileName}`; await minio.copyObject( @@ -159,4 +184,4 @@ export async function duplicateAvatarFile(refId: string, prefix: string, fileNam } } -export const fileLocation = (...path: string[]) => path.join("/"); +export const fileLocation = (...path: string[]) => path.join("/"); \ No newline at end of file