This commit is contained in:
Manav Rathi 2024-04-19 14:11:09 +05:30
parent 2c46be6ded
commit f59ecdb8d8
No known key found for this signature in database
2 changed files with 29 additions and 32 deletions

View file

@ -38,8 +38,6 @@ class FolderWatcher {
* If the file system directory corresponding to the (root) folder path of a
* folder watch is deleted on disk, we note down that in this queue so that
* we can ignore any file system events that come for it next.
*
* TODO: is this really needed? the mappings are pre-checked first.
*/
private deletedFolderPaths: string[] = [];
/** `true` if we are using the uploader. */
@ -89,12 +87,12 @@ class FolderWatcher {
this.syncWithDisk();
}
/** `true` if we are currently using the uploader */
/** Return `true` if we are currently using the uploader. */
isUploadRunning() {
return this.uploadRunning;
}
/** `true` if syncing has been temporarily paused */
/** Return `true` if syncing has been temporarily paused. */
isSyncPaused() {
return this.isPaused;
}
@ -501,31 +499,30 @@ class FolderWatcher {
this.eventQueue = this.eventQueue.filter(
(event) => !event.filePath.startsWith(deletedFolderPath),
);
return true;
}
private async moveToTrash(syncedFiles: FolderWatch["syncedFiles"]) {
const files = await getLocalFiles();
const toTrashFilesMap = new Map<number, FolderWatchSyncedFile>();
for (const file of syncedFiles) {
toTrashFilesMap.set(file.uploadedFileID, file);
}
const filesToTrash = files.filter((file) => {
if (toTrashFilesMap.has(file.id)) {
const fileToTrash = toTrashFilesMap.get(file.id);
if (fileToTrash.collectionID === file.collectionID) {
return true;
}
}
});
const groupFilesByCollectionId =
groupFilesBasedOnCollectionID(filesToTrash);
const syncedFileForID = new Map<number, FolderWatchSyncedFile>();
for (const file of syncedFiles)
syncedFileForID.set(file.uploadedFileID, file);
for (const [
collectionID,
filesToTrash,
] of groupFilesByCollectionId.entries()) {
await removeFromCollection(collectionID, filesToTrash);
const files = await getLocalFiles();
const filesToTrash = files.filter((file) => {
const correspondingSyncedFile = syncedFileForID.get(file.id);
if (
correspondingSyncedFile &&
correspondingSyncedFile.collectionID == file.collectionID
) {
return true;
}
return false;
});
const filesByCollectionID = groupFilesBasedOnCollectionID(filesToTrash);
for (const [id, files] of filesByCollectionID.entries()) {
await removeFromCollection(id, files);
}
this.requestSyncWithRemote();

View file

@ -132,16 +132,16 @@ export async function downloadFile(file: EnteFile) {
}
}
export function groupFilesBasedOnCollectionID(files: EnteFile[]) {
const collectionWiseFiles = new Map<number, EnteFile[]>();
/** Segment the given {@link files} into lists indexed by their collection ID */
export const groupFilesBasedOnCollectionID = (files: EnteFile[]) => {
const result = new Map<number, EnteFile[]>();
for (const file of files) {
if (!collectionWiseFiles.has(file.collectionID)) {
collectionWiseFiles.set(file.collectionID, []);
}
collectionWiseFiles.get(file.collectionID).push(file);
const id = file.collectionID;
if (!result.has(id)) result.set(id, []);
result.get(id).push(file);
}
return collectionWiseFiles;
}
return result;
};
function getSelectedFileIds(selectedFiles: SelectedState) {
const filesIDs: number[] = [];