From 7fa0e2f2426a1373242c153119da992ad9ff2d0f Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 4 Apr 2025 15:05:18 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=A3=E0=B8=B0=E0=B8=9A=E0=B8=9A=E0=B8=9B?= =?UTF-8?q?=E0=B8=A3=E0=B8=B0=E0=B9=80=E0=B8=A1=E0=B8=B4=E0=B8=99=20?= =?UTF-8?q?=E0=B8=81=E0=B8=A3=E0=B8=93=E0=B8=B5=E0=B8=AD=E0=B8=B1=E0=B8=9B?= =?UTF-8?q?=E0=B9=82=E0=B8=AB=E0=B8=A5=E0=B8=94=E0=B9=80=E0=B8=AD=E0=B8=81?= =?UTF-8?q?=E0=B8=AA=E0=B8=B2=E0=B8=A3=E0=B8=97=E0=B8=B1=E0=B8=9A=E0=B8=AD?= =?UTF-8?q?=E0=B8=B1=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B4=E0=B8=A1=20=20(?= =?UTF-8?q?=E0=B8=97=E0=B8=94=E0=B8=AA=E0=B8=AD=E0=B8=9A)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/DocumentController.ts | 44 +++++++++++++++++++++------ src/services/storage.ts | 31 +++++++++++++++++++ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/controllers/DocumentController.ts b/src/controllers/DocumentController.ts index 78a3913..b20ba08 100644 --- a/src/controllers/DocumentController.ts +++ b/src/controllers/DocumentController.ts @@ -20,6 +20,7 @@ import { downloadFile, listFile, updateFile, + updateFile2 } from "../services/storage"; import { AppDataSource } from "../database/data-source"; import { Evaluation } from "../entities/Evaluation"; @@ -221,7 +222,7 @@ export class DocumentController extends Controller { throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถสร้างแฟ้มได้"); } - let used: string[] = []; + // let used: string[] = []; const evaluation = AppDataSource.getRepository(Evaluation); @@ -230,20 +231,45 @@ export class DocumentController extends Controller { }); let fileList = !body.replace - ? body.fileList.map(({ fileName, ...props }) => { + ? await Promise.all(body.fileList.map(async({ fileName, ...props }) => { const dotIndex = fileName.lastIndexOf("."); const originalName = dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dotIndex) : fileName; const extension = dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(dotIndex) : ""; - let i = 1; + // let i = 1; + let copyFileName = fileName; while ( - list.findIndex((v) => v.fileName === fileName) !== -1 || - used.includes(fileName) + list.findIndex((v) => v.fileName === fileName) !== -1 /*|| + used.includes(fileName)*/ ) { - fileName = `${originalName} (${i++})`; - if (dotIndex !== -1) fileName += extension; + // fileName = `${originalName} (${i++})`; + // if (dotIndex !== -1) fileName += extension; + let copy = 0; + while ( + list.findIndex( + (v) => + v.fileName === + `${originalName} (${copy + 1})` + (dotIndex !== -1 ? extension : ""), + ) !== -1 + ) { + copy++; + } + copyFileName = `${originalName} (${copy + 1})` + extension; + + const result = await updateFile2( + ["ระบบประเมิน", volume, id], + fileName, + { ...props, title: copyFileName }, + ["ระบบประเมิน", volume, id], + copyFileName, + ); + + if (!result) { + throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถแก้ไขไฟล์ได้"); + } + break; } if (author) { props.author = `${author.prefix}${author.fullName}`; @@ -251,9 +277,9 @@ export class DocumentController extends Controller { props.author = "ไม่พบข้อมูล"; } - used.push(fileName); + // used.push(fileName); return { fileName: fileName, ...props }; - }) + })) : body.fileList; const map = fileList.map(async ({ fileName, ...props }) => [ diff --git a/src/services/storage.ts b/src/services/storage.ts index 60cbf17..e618475 100644 --- a/src/services/storage.ts +++ b/src/services/storage.ts @@ -167,6 +167,37 @@ export async function updateFile(path: string[], file: string, metadata: FilePro return Boolean(res); } +export async function updateFile2( + path: string[], + file: string, + metadata: FileProps, + destPath?: string[], + destFile?: string, +) { + const res = await fetch(`${STORAGE_URL}/storage/file`, { + method: "PUT", + headers: { + Authorization: `Bearer ${await getToken()}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + from: { path, file }, + to: destPath && destFile ? { path: destPath, file: destFile } : undefined, + ...metadata, + upload: false, + }), + }).catch((e) => console.error(e)); + + if (!res || !res.ok) { + if (res && res.status === HttpStatus.NOT_FOUND) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบไฟล์ในระบบ"); + } + return Boolean(console.error(res ? await res.json() : res)); + } + + return Boolean(res); +} + export async function deleteFolder(path: string[], name: string) { const res = await fetch(`${STORAGE_URL}/storage/folder`, { method: "DELETE",