fix(receiver): Prevent duplicate downloads on directory save

When saving files to a user-selected directory, the browser would trigger an additional, redundant download.

This was caused by a race condition where the UI checked the file's save method (saveType) before the flag was updated upon completion. The decision to save to disk was made too late in the process.

This commit resolves the issue by setting the saveType at the beginning of the file request. This ensures the UI has the correct information when the transfer finishes, preventing the extra download.
This commit is contained in:
david_bai
2025-07-15 23:13:59 +08:00
parent 5b9f925bf9
commit 8ea2016218
+11 -16
View File
@@ -141,6 +141,15 @@ class FileReceiver {
return;
}
const shouldSaveToDisk =
!!this.saveDirectory || fileInfo.size >= this.largeFileThreshold;
// Set saveType at the beginning of the request to prevent race conditions in the UI
this.saveType[fileInfo.fileId] = shouldSaveToDisk;
if (this.currentFolderName) {
this.saveType[this.currentFolderName] = shouldSaveToDisk;
}
const receptionPromise = new Promise<void>((resolve, reject) => {
this.activeFileReception = {
meta: fileInfo,
@@ -152,11 +161,6 @@ class FileReceiver {
};
});
const shouldSaveToDisk =
!!this.saveDirectory ||
fileInfo.size >= this.largeFileThreshold ||
!!this.currentFolderName;
if (shouldSaveToDisk) {
await this.createDiskWriteStream(fileInfo);
}
@@ -435,12 +439,7 @@ class FileReceiver {
folderName: this.currentFolderName,
}) as CustomFile;
this.saveType[reception.meta.fileId] = true;
if (this.currentFolderName) {
this.saveType[this.currentFolderName] = true;
// For files in a folder saved to disk, we don't call onFileReceived
// as the user experience is typically to "open folder" after download.
} else {
if (!this.currentFolderName) {
await this.onFileReceived?.(customFile);
}
}
@@ -463,11 +462,7 @@ class FileReceiver {
folderName: this.currentFolderName,
}) as CustomFile;
this.saveType[reception.meta.fileId] = false;
if (this.currentFolderName) {
this.saveType[this.currentFolderName] = false;
}
// saveType is now set in requestFile.
await this.onFileReceived?.(customFile);
}
// endregion