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

@ -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 = {