feat: handle replace

This commit is contained in:
Methapon2001 2025-01-15 13:31:42 +07:00
parent a6295586f8
commit 6306e0266d
2 changed files with 23 additions and 15 deletions

View file

@ -192,7 +192,7 @@ export class FileController extends Controller {
const ret = await Promise.all(
body.fileList.map(async ({ fileName }) => [
fileName,
await s3UploadFile(...path, fileName),
await s3UploadFile(!!body.replace, ...path, fileName),
]),
);
return Object.fromEntries(ret);
@ -519,7 +519,13 @@ export class SubFileController extends Controller {
const path = [name, group, id, subId];
if (name !== "ระบบประเมิน") {
return await s3ListFile(name, group, id, subId);
const ret = await Promise.all(
body.fileList.map(async ({ fileName }) => [
fileName,
await s3UploadFile(!!body.replace, ...path, fileName),
]),
);
return Object.fromEntries(ret);
}
if (!(await createFolder(path.slice(0, -1), subId, true))) {

View file

@ -65,25 +65,27 @@ export async function deleteFolder(path: string) {
});
}
export async function s3UploadFile(...pathname: string[]) {
export async function s3UploadFile(replace: boolean, ...pathname: string[]) {
if (!pathname.length) return;
const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception);
if (!replace) {
const list = await s3ListFile(...pathname.slice(0, -1)).catch(exception);
const fileName = pathname.at(-1) as string;
const dot = fileName.lastIndexOf(".");
const name = dot !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dot) : fileName;
const ext = dot !== -1 && !fileName.startsWith(".") ? fileName.slice(dot) : "";
const fileName = pathname.at(-1) as string;
const dot = fileName.lastIndexOf(".");
const name = dot !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dot) : fileName;
const ext = dot !== -1 && !fileName.startsWith(".") ? fileName.slice(dot) : "";
let copy = 0;
let copyFileName = fileName;
let copy = 0;
let copyFileName = fileName;
while (list.findIndex((v) => v.fileName === copyFileName) !== -1) {
copyFileName = `${name} (${++copy})${ext}`.trim();
}
while (list.findIndex((v) => v.fileName === copyFileName) !== -1) {
copyFileName = `${name} (${++copy})${ext}`.trim();
}
if (copy > 0) {
await s3UpdateFile(pathname, pathname.slice(0, -1).concat(copyFileName));
if (copy > 0) {
await s3UpdateFile(pathname, pathname.slice(0, -1).concat(copyFileName));
}
}
const data = {