Browse Source

ignore files which were trashed or deleted

Neeraj Gupta 3 years ago
parent
commit
be9c9ca13b
2 changed files with 52 additions and 0 deletions
  1. 24 0
      lib/db/ignore_files_db.dart
  2. 28 0
      lib/services/remote_sync_service.dart

+ 24 - 0
lib/db/ignore_files_db.dart

@@ -103,7 +103,31 @@ class IgnoreFilesDB {
     );
     );
   }
   }
 
 
+  // return map of localID to set of titles associated with the given localIDs
+  // Note: localIDs can easily clash across devices for Android. The set of titles
+  // is to handle that case. In iOS, for selected permissions, we may not always
+  // get correct deviceFolder.
+  Future<Map<String, Set<String>>> getIgnoredFiles() async {
+    final db = await instance.database;
+    final rows = await db.query(tableName);
+    final result = <String, Set<String>>{};
+    for (final row in rows) {
+      final ignoredFile = _getIgnoredFileFromRow(row);
+      result
+          .putIfAbsent(ignoredFile.localID, () => <String>{})
+          .add(ignoredFile.title);
+    }
+    return result;
+  }
+
+  IgnoredFile _getIgnoredFileFromRow(Map<String, dynamic> row) {
+    return IgnoredFile(row[columnLocalID], row[columnDeviceFolder],
+        row[columnTitle], row[columnReason]);
+  }
+
   Map<String, dynamic> _getRowForIgnoredFile(IgnoredFile ignoredFile) {
   Map<String, dynamic> _getRowForIgnoredFile(IgnoredFile ignoredFile) {
+    assert(ignoredFile.title != null);
+    assert(ignoredFile.localID != null);
     final row = <String, dynamic>{};
     final row = <String, dynamic>{};
     row[columnLocalID] = ignoredFile.localID;
     row[columnLocalID] = ignoredFile.localID;
     row[columnTitle] = ignoredFile.title;
     row[columnTitle] = ignoredFile.title;

+ 28 - 0
lib/services/remote_sync_service.dart

@@ -7,6 +7,7 @@ import 'package:photos/core/configuration.dart';
 import 'package:photos/core/errors.dart';
 import 'package:photos/core/errors.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/db/files_db.dart';
 import 'package:photos/db/files_db.dart';
+import 'package:photos/db/ignore_files_db.dart';
 import 'package:photos/events/collection_updated_event.dart';
 import 'package:photos/events/collection_updated_event.dart';
 import 'package:photos/events/files_updated_event.dart';
 import 'package:photos/events/files_updated_event.dart';
 import 'package:photos/events/local_photos_updated_event.dart';
 import 'package:photos/events/local_photos_updated_event.dart';
@@ -125,6 +126,23 @@ class RemoteSyncService {
     }
     }
   }
   }
 
 
+  bool _shouldIgnoreFileUpload(
+      Map<String, Set<String>> ignoredFilesMap, File file) {
+    if (file.localID == null || file.localID.isEmpty) {
+      return false;
+    }
+    if (!ignoredFilesMap.containsKey(file.localID)) {
+      return false;
+    }
+    // only compare title in Android because title may be missing in IOS
+    // and iOS anyways use uuid for localIDs of file, so collision should be
+    // rate.
+    if (Platform.isAndroid) {
+      return ignoredFilesMap[file.localID].contains(file.title ?? '');
+    }
+    return true;
+  }
+
   Future<bool> _uploadDiff() async {
   Future<bool> _uploadDiff() async {
     final foldersToBackUp = Configuration.instance.getPathsToBackUp();
     final foldersToBackUp = Configuration.instance.getPathsToBackUp();
     List<File> filesToBeUploaded;
     List<File> filesToBeUploaded;
@@ -139,6 +157,16 @@ class RemoteSyncService {
       filesToBeUploaded
       filesToBeUploaded
           .removeWhere((element) => element.fileType == FileType.video);
           .removeWhere((element) => element.fileType == FileType.video);
     }
     }
+    if (filesToBeUploaded.isNotEmpty) {
+      final ignoredFilesMap = await IgnoreFilesDB.instance.getIgnoredFiles();
+      int prevCount = filesToBeUploaded.length;
+      filesToBeUploaded.removeWhere(
+          (file) => _shouldIgnoreFileUpload(ignoredFilesMap, file));
+      if (prevCount != filesToBeUploaded.length) {
+        _logger.info((prevCount - filesToBeUploaded.length).toString() +
+            " files were ignored for upload");
+      }
+    }
     _logger.info(
     _logger.info(
         filesToBeUploaded.length.toString() + " new files to be uploaded.");
         filesToBeUploaded.length.toString() + " new files to be uploaded.");