Merge branch 'develop' into dev
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m1s

This commit is contained in:
Adisak 2026-04-08 15:14:05 +07:00
commit 5727c79cc2
2 changed files with 30 additions and 5 deletions

View file

@ -22,7 +22,7 @@ import {
listFile, listFile,
updateFile, updateFile,
} from "../services/edm"; } 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}") @Route("api/v1/salary/file/{name}/{group}")
@Security("bearerAuth") @Security("bearerAuth")
@ -257,7 +257,7 @@ export class FileController extends Controller {
const map = fileList.map(async ({ fileName, ...props }) => [ const map = fileList.map(async ({ fileName, ...props }) => [
fileName, fileName,
await createFile(path, fileName, props), await createFile(path, truncateFileName(fileName), props),
]); ]);
const result = await Promise.all(map).catch((e) => const result = await Promise.all(map).catch((e) =>
@ -607,7 +607,7 @@ export class SubFileController extends Controller {
const map = fileList.map(async ({ fileName, ...props }) => [ const map = fileList.map(async ({ fileName, ...props }) => [
fileName, fileName,
await createFile(path, fileName, props), await createFile(path, truncateFileName(fileName), props),
]); ]);
const result = await Promise.all(map).catch((e) => const result = await Promise.all(map).catch((e) =>

View file

@ -16,6 +16,26 @@ const minio = new Client({
export default minio; 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 { function exception(e: any): never {
console.error(e); console.error(e);
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์"); throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
@ -74,6 +94,11 @@ export async function s3DeleteFolder(path: string) {
export async function s3UploadFile(replace: boolean, ...pathname: string[]) { export async function s3UploadFile(replace: boolean, ...pathname: string[]) {
if (!pathname.length) return; if (!pathname.length) return;
// ตัดชื่อไฟล์ถ้ายาวเกิน
const originalFileName = pathname.at(-1) as string;
const truncatedFileName = truncateFileName(originalFileName);
pathname = [...pathname.slice(0, -1), truncatedFileName];
if (!replace) { if (!replace) {
const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception); const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception);