Merge pull request #1344 from killerprojecte/master

feat: rename duplicate file when user deny overwrite operation
This commit is contained in:
Unitwk 2024-08-21 11:18:53 +08:00 committed by GitHub
commit 5a54c19806
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 3 deletions

View File

@ -74,12 +74,30 @@ router.post("/upload/:key", async (ctx) => {
throw new Error("Access denied: Files must a array!");
}
const originFileName = uploadedFile.originalFilename || "";
const fileSaveRelativePath = path.normalize(path.join(uploadDir, originFileName));
if (!FileManager.checkFileName(path.basename(originFileName)))
throw new Error("Access denied: Malformed file name");
const fileManager = new FileManager(cwd);
const ext = path.extname(originFileName);
const basename = path.basename(originFileName, ext);
let tempFileSaveName = basename + ext;
let counter = 1;
while (fs.existsSync(fileManager.toAbsolutePath(path.normalize(path.join(uploadDir, tempFileSaveName)))) && ctx.query.overwrite === "false") {
if (counter == 1) {
tempFileSaveName = `${basename}-copy${ext}`
} else {
tempFileSaveName = `${basename}-copy-${counter}${ext}`
}
counter++;
}
let fileSaveRelativePath = path.normalize(path.join(uploadDir, tempFileSaveName));
if (!fileManager.checkPath(fileSaveRelativePath))
throw new Error("Access denied: Invalid destination");
const fileSaveAbsolutePath = fileManager.toAbsolutePath(fileSaveRelativePath);
await fs.move(uploadedFile.filepath, fileSaveAbsolutePath, {

View File

@ -366,6 +366,7 @@ export const useFileManager = (instanceId?: string, daemonId?: string) => {
throw new Error(t("TXT_CODE_e8ce38c2"));
}
let shouldOverwrite = true;
if (dataSource.value?.find((dataType) => dataType.name === file.name)) {
const confirmPromise = new Promise<boolean>((onComplete) => {
Modal.confirm({
@ -381,7 +382,10 @@ export const useFileManager = (instanceId?: string, daemonId?: string) => {
}
});
});
if (!(await confirmPromise)) return reportErrorMsg(t("TXT_CODE_8b14426e"));
if (!(await confirmPromise)) {
shouldOverwrite = false;
//return reportErrorMsg(t("TXT_CODE_8b14426e"));
}
}
const uploadFormData = new FormData();
@ -396,6 +400,9 @@ export const useFileManager = (instanceId?: string, daemonId?: string) => {
onUploadProgress: (progressEvent: any) => {
const p = Math.round((progressEvent.loaded * 100) / progressEvent.total);
if (p >= 1) percentComplete.value = p;
},
params: {
overwrite: shouldOverwrite
}
});
await getFileList();

View File

@ -167,7 +167,10 @@ export const uploadAddress = useDefineApi<
export const uploadFile = useDefineApi<
{
data: FormData;
data: FormData,
params: {
overwrite: boolean;
};
},
any
>({