From 11015df0a898150e7d5cb760b4a488d06b0352f3 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 10 Jan 2025 13:56:46 +0700 Subject: [PATCH 1/4] checkpoint don't deploy --- src/controllers/FileController.ts | 51 ++++++++++++++++++++----------- src/services/edm.ts | 3 +- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index e02d0d4..43d412c 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -186,28 +186,45 @@ export class FileController extends Controller { throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ เข้าถึงรายการไฟล์ได้"); } - let used: string[] = []; - let fileList = !body.replace - ? body.fileList.map(({ 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) : ""; + ? 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; - while (list.findIndex((v) => v.fileName === fileName) !== -1 || used.includes(fileName)) { - fileName = `${originalName} (${i++})`; - if (dotIndex !== -1) fileName += extension; - } + let copyFileName = fileName; - props.author = "ไม่พบข้อมูล"; + while (list.findIndex((v) => v.fileName === fileName) !== -1) { + let copy = 0; - used.push(fileName); + while ( + list.findIndex( + (v) => + v.fileName === + `${originalName} (${copy + 1})` + (dotIndex !== -1 ? extension : ""), + ) !== -1 + ) { + copy++; + } - return { fileName: fileName, ...props }; - }) + copyFileName = `${originalName} (${copy})` + extension; + + const result = await updateFile(path, fileName, Object.assign(props, { title: copyFileName}), path, copyFileName); + + if (!result) { + throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถแก้ไขไฟล์ได้"); + } + break; + } + + props.author = "ไม่พบข้อมูล"; + + return { fileName: fileName, ...props }; + }), + ) : body.fileList; const map = fileList.map(async ({ fileName, ...props }) => [ diff --git a/src/services/edm.ts b/src/services/edm.ts index 3415af2..38c66e9 100644 --- a/src/services/edm.ts +++ b/src/services/edm.ts @@ -143,7 +143,7 @@ export async function listFile(path: string[]) { return (await list("file", path)) as StorageFile[] | boolean; } -export async function updateFile(path: string[], file: string, metadata: FileProps) { +export async function updateFile(path: string[], file: string, metadata: FileProps, destPath?: string[], destFile?: string) { const res = await fetch(`${STORAGE_URL}/storage/file`, { method: "PUT", headers: { @@ -152,6 +152,7 @@ export async function updateFile(path: string[], file: string, metadata: FilePro }, body: JSON.stringify({ from: { path, file }, + to: destPath && destFile ? { path: destPath, file: destFile,} : undefined, ...metadata, upload: false, }), From d635ffbc666ae684c27ef980763dda3fd69040e3 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:29:06 +0700 Subject: [PATCH 2/4] chore: format file --- src/services/edm.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/services/edm.ts b/src/services/edm.ts index 38c66e9..a064b9e 100644 --- a/src/services/edm.ts +++ b/src/services/edm.ts @@ -143,7 +143,13 @@ export async function listFile(path: string[]) { return (await list("file", path)) as StorageFile[] | boolean; } -export async function updateFile(path: string[], file: string, metadata: FileProps, destPath?: string[], destFile?: string) { +export async function updateFile( + path: string[], + file: string, + metadata: FileProps, + destPath?: string[], + destFile?: string, +) { const res = await fetch(`${STORAGE_URL}/storage/file`, { method: "PUT", headers: { @@ -152,7 +158,7 @@ export async function updateFile(path: string[], file: string, metadata: FilePro }, body: JSON.stringify({ from: { path, file }, - to: destPath && destFile ? { path: destPath, file: destFile,} : undefined, + to: destPath && destFile ? { path: destPath, file: destFile } : undefined, ...metadata, upload: false, }), From 88fec3b8f208e8f56cd0c57194603d0f3db35ede Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:32:17 +0700 Subject: [PATCH 3/4] fix: title should not affact original file --- src/controllers/FileController.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index 43d412c..7fe848e 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -210,9 +210,15 @@ export class FileController extends Controller { copy++; } - copyFileName = `${originalName} (${copy})` + extension; + copyFileName = `${originalName} (${copy + 1})` + extension; - const result = await updateFile(path, fileName, Object.assign(props, { title: copyFileName}), path, copyFileName); + const result = await updateFile( + path, + fileName, + { ...props, title: copyFileName }, + path, + copyFileName, + ); if (!result) { throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถแก้ไขไฟล์ได้"); From 990fa7c1175f8a146f4f7016a3b36e0217d4122d Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:42:07 +0700 Subject: [PATCH 4/4] feat: update file to copy file and replace original file --- src/controllers/FileController.ts | 57 ++++++++++++++------ src/controllers/StorageEmployeeController.ts | 57 ++++++++++++++------ 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index 7fe848e..73fe7c5 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -498,28 +498,51 @@ export class SubFileController extends Controller { throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ เข้าถึงรายการไฟล์ได้"); } - let used: string[] = []; - let fileList = !body.replace - ? body.fileList.map(({ 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) : ""; + ? 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; - while (list.findIndex((v) => v.fileName === fileName) !== -1 || used.includes(fileName)) { - fileName = `${originalName} (${i++})`; - if (dotIndex !== -1) fileName += extension; - } + let copyFileName = fileName; - props.author = "ไม่พบข้อมูล"; + while (list.findIndex((v) => v.fileName === fileName) !== -1) { + let copy = 0; - used.push(fileName); + while ( + list.findIndex( + (v) => + v.fileName === + `${originalName} (${copy + 1})` + (dotIndex !== -1 ? extension : ""), + ) !== -1 + ) { + copy++; + } - return { fileName: fileName, ...props }; - }) + copyFileName = `${originalName} (${copy + 1})` + extension; + + const result = await updateFile( + path, + fileName, + { ...props, title: copyFileName }, + path, + copyFileName, + ); + + if (!result) { + throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถแก้ไขไฟล์ได้"); + } + break; + } + + props.author = "ไม่พบข้อมูล"; + + return { fileName: fileName, ...props }; + }), + ) : body.fileList; const map = fileList.map(async ({ fileName, ...props }) => [ diff --git a/src/controllers/StorageEmployeeController.ts b/src/controllers/StorageEmployeeController.ts index ec08bca..acb6d2e 100644 --- a/src/controllers/StorageEmployeeController.ts +++ b/src/controllers/StorageEmployeeController.ts @@ -179,28 +179,51 @@ export class DocumentEmployeeController extends Controller { throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ เข้าถึงรายการไฟล์ได้"); } - let used: string[] = []; - let fileList = !body.replace - ? body.fileList.map(({ 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) : ""; + ? 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; - while (list.findIndex((v) => v.fileName === fileName) !== -1 || used.includes(fileName)) { - fileName = `${originalName} (${i++})`; - if (dotIndex !== -1) fileName += extension; - } + let copyFileName = fileName; - props.author = "ไม่พบข้อมูล"; + while (list.findIndex((v) => v.fileName === fileName) !== -1) { + let copy = 0; - used.push(fileName); + while ( + list.findIndex( + (v) => + v.fileName === + `${originalName} (${copy + 1})` + (dotIndex !== -1 ? extension : ""), + ) !== -1 + ) { + copy++; + } - return { fileName: fileName, ...props }; - }) + copyFileName = `${originalName} (${copy + 1})` + extension; + + const result = await updateFile( + path, + fileName, + { ...props, title: copyFileName }, + path, + copyFileName, + ); + + if (!result) { + throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถแก้ไขไฟล์ได้"); + } + break; + } + + props.author = "ไม่พบข้อมูล"; + + return { fileName: fileName, ...props }; + }), + ) : body.fileList; const map = fileList.map(async ({ fileName, ...props }) => [