feat: update file to copy file and replace original file
This commit is contained in:
parent
88fec3b8f2
commit
990fa7c117
2 changed files with 80 additions and 34 deletions
|
|
@ -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 }) => [
|
||||
|
|
|
|||
|
|
@ -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 }) => [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue