ระบบประเมิน กรณีอัปโหลดเอกสารทับอันเดิม (ทดสอบ)

This commit is contained in:
Bright 2025-04-04 15:05:18 +07:00
parent 94f5724033
commit 7fa0e2f242
2 changed files with 66 additions and 9 deletions

View file

@ -20,6 +20,7 @@ import {
downloadFile, downloadFile,
listFile, listFile,
updateFile, updateFile,
updateFile2
} from "../services/storage"; } from "../services/storage";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import { Evaluation } from "../entities/Evaluation"; import { Evaluation } from "../entities/Evaluation";
@ -221,7 +222,7 @@ export class DocumentController extends Controller {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถสร้างแฟ้มได้"); throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถสร้างแฟ้มได้");
} }
let used: string[] = []; // let used: string[] = [];
const evaluation = AppDataSource.getRepository(Evaluation); const evaluation = AppDataSource.getRepository(Evaluation);
@ -230,20 +231,45 @@ export class DocumentController extends Controller {
}); });
let fileList = !body.replace let fileList = !body.replace
? body.fileList.map(({ fileName, ...props }) => { ? await Promise.all(body.fileList.map(async({ fileName, ...props }) => {
const dotIndex = fileName.lastIndexOf("."); const dotIndex = fileName.lastIndexOf(".");
const originalName = const originalName =
dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dotIndex) : fileName; dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dotIndex) : fileName;
const extension = const extension =
dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(dotIndex) : ""; dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(dotIndex) : "";
let i = 1; // let i = 1;
let copyFileName = fileName;
while ( while (
list.findIndex((v) => v.fileName === fileName) !== -1 || list.findIndex((v) => v.fileName === fileName) !== -1 /*||
used.includes(fileName) used.includes(fileName)*/
) { ) {
fileName = `${originalName} (${i++})`; // fileName = `${originalName} (${i++})`;
if (dotIndex !== -1) fileName += extension; // 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) { if (author) {
props.author = `${author.prefix}${author.fullName}`; props.author = `${author.prefix}${author.fullName}`;
@ -251,9 +277,9 @@ export class DocumentController extends Controller {
props.author = "ไม่พบข้อมูล"; props.author = "ไม่พบข้อมูล";
} }
used.push(fileName); // used.push(fileName);
return { fileName: fileName, ...props }; return { fileName: fileName, ...props };
}) }))
: body.fileList; : body.fileList;
const map = fileList.map(async ({ fileName, ...props }) => [ const map = fileList.map(async ({ fileName, ...props }) => [

View file

@ -167,6 +167,37 @@ export async function updateFile(path: string[], file: string, metadata: FilePro
return Boolean(res); 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) { export async function deleteFolder(path: string[], name: string) {
const res = await fetch(`${STORAGE_URL}/storage/folder`, { const res = await fetch(`${STORAGE_URL}/storage/folder`, {
method: "DELETE", method: "DELETE",