ระบบประเมิน กรณีอัปโหลดเอกสารทับอันเดิม (ทดสอบ)
This commit is contained in:
parent
94f5724033
commit
7fa0e2f242
2 changed files with 66 additions and 9 deletions
|
|
@ -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 }) => [
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue