feat: handle same name upload

This commit is contained in:
Methapon2001 2025-01-15 13:28:33 +07:00
parent a1b88c070b
commit a6295586f8

View file

@ -68,6 +68,24 @@ export async function deleteFolder(path: string) {
export async function s3UploadFile(...pathname: string[]) {
if (!pathname.length) return;
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) : "";
let copy = 0;
let copyFileName = fileName;
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));
}
const data = {
path: pathname.slice(0, -1).join("/"),
pathname: pathname.join("/"),
@ -81,10 +99,9 @@ export async function s3UploadFile(...pathname: string[]) {
export async function s3UpdateFile(from: string[], to: string[], upload?: boolean) {
await minio
.copyObject(MINIO_BUCKET, from.join("/"), [MINIO_BUCKET].concat(to).join("/"))
.then(() => {
minio.removeObject(MINIO_BUCKET, from.join("/"));
});
.copyObject(MINIO_BUCKET, to.join("/"), [MINIO_BUCKET].concat(from).join("/"))
.catch(exception);
await minio.removeObject(MINIO_BUCKET, from.join("/")).catch(exception);
const data = {
path: to.slice(0, -1).join("/"),