From 6306e0266dc831d8e0c555c6a5d597bc5339282c Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:31:42 +0700 Subject: [PATCH] feat: handle replace --- src/controllers/FileController.ts | 10 ++++++++-- src/services/minio.ts | 28 +++++++++++++++------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index dc124c4..606ea4f 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -192,7 +192,7 @@ export class FileController extends Controller { const ret = await Promise.all( body.fileList.map(async ({ fileName }) => [ fileName, - await s3UploadFile(...path, fileName), + await s3UploadFile(!!body.replace, ...path, fileName), ]), ); return Object.fromEntries(ret); @@ -519,7 +519,13 @@ export class SubFileController extends Controller { const path = [name, group, id, subId]; if (name !== "ระบบประเมิน") { - return await s3ListFile(name, group, id, subId); + const ret = await Promise.all( + body.fileList.map(async ({ fileName }) => [ + fileName, + await s3UploadFile(!!body.replace, ...path, fileName), + ]), + ); + return Object.fromEntries(ret); } if (!(await createFolder(path.slice(0, -1), subId, true))) { diff --git a/src/services/minio.ts b/src/services/minio.ts index 10a3949..921e3e0 100644 --- a/src/services/minio.ts +++ b/src/services/minio.ts @@ -65,25 +65,27 @@ export async function deleteFolder(path: string) { }); } -export async function s3UploadFile(...pathname: string[]) { +export async function s3UploadFile(replace: boolean, ...pathname: string[]) { if (!pathname.length) return; - const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception); + if (!replace) { + const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception); - const fileName = pathname.at(-1) as 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) : ""; + const fileName = pathname.at(-1) as 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) : ""; - let copy = 0; - let copyFileName = fileName; + let copy = 0; + let copyFileName = fileName; - while (list.findIndex((v) => v.fileName === copyFileName) !== -1) { - copyFileName = `${name} (${++copy})${ext}`.trim(); - } + while (list.findIndex((v) => v.fileName === copyFileName) !== -1) { + copyFileName = `${name} (${++copy})${ext}`.trim(); + } - if (copy > 0) { - await s3UpdateFile(pathname, pathname.slice(0, -1).concat(copyFileName)); + if (copy > 0) { + await s3UpdateFile(pathname, pathname.slice(0, -1).concat(copyFileName)); + } } const data = {