Pārlūkot izejas kodu

persist deleted/trashed files with localId in ignoredFiles db

Neeraj Gupta 3 gadi atpakaļ
vecāks
revīzija
3b3f787f8c

+ 0 - 3
lib/db/ignore_files_db.dart

@@ -11,9 +11,6 @@ import 'package:sqflite/sqflite.dart';
 // when they delete a file from web/another device, we should not automatically
 // upload the files.
 class IgnoreFilesDB {
-  static const kIgnoreReasonTrash = "trash";
-  static const kIgnoreReasonInvalidFile = "invalidFile";
-
   static final _databaseName = "ente.ignore_files.db";
   static final _databaseVersion = 1;
   static final Logger _logger = Logger("IgnoreFilesDB");

+ 17 - 0
lib/models/ignored_file.dart

@@ -1,3 +1,7 @@
+import 'package:photos/models/trash_file.dart';
+
+const kIgnoreReasonTrash = "trash";
+const kIgnoreReasonInvalidFile = "invalidFile";
 class IgnoredFile {
   final String localID;
   final String deviceFolder;
@@ -5,4 +9,17 @@ class IgnoredFile {
   String reason;
 
   IgnoredFile(this.localID, this.deviceFolder, this.title, this.reason);
+
+  factory IgnoredFile.fromTrashItem(TrashFile trashFile) {
+    if (trashFile == null) return null;
+    if (trashFile.localID == null ||
+        trashFile.title == null ||
+        trashFile.localID.isEmpty ||
+        trashFile.title.isEmpty) {
+      return null;
+    }
+
+    return IgnoredFile(trashFile.localID, trashFile.deviceFolder,
+        trashFile.title, kIgnoreReasonTrash);
+  }
 }

+ 6 - 2
lib/services/remote_sync_service.dart

@@ -61,8 +61,12 @@ class RemoteSyncService {
     if (!_hasSyncedArchive()) {
       await _markArchiveAsSynced();
     }
-
-    await TrashSyncService.instance.syncTrash();
+    // sync trash but consume error during initial launch.
+    // this is to ensure that we don't pause upload due to any error during
+    // the trash sync. Impact: We may end up re-uploading a file which was
+    // recently trashed.
+    await TrashSyncService.instance.syncTrash()
+    .onError((e, s) => _logger.severe('trash sync failed', e, s));
     bool hasUploadedFiles = await _uploadDiff();
     if (hasUploadedFiles) {
       sync(silently: true);

+ 26 - 1
lib/services/trash_sync_service.dart

@@ -3,7 +3,9 @@ import 'package:logging/logging.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/network.dart';
 import 'package:photos/db/files_db.dart';
+import 'package:photos/db/ignore_files_db.dart';
 import 'package:photos/db/trash_db.dart';
+import 'package:photos/models/ignored_file.dart';
 import 'package:photos/models/trash_file.dart';
 import 'package:photos/models/trash_item_request.dart';
 import 'package:photos/utils/trash_diff_fetcher.dart';
@@ -15,7 +17,7 @@ class TrashSyncService {
   final _filesDB = FilesDB.instance;
   final _trashDB = TrashDB.instance;
   static const kDiffLimit = 2500;
-  static const kLastTrashSyncTime = "last_trash_sync_time";
+  static const kLastTrashSyncTime = "last_trash_sync_timex";
   SharedPreferences _prefs;
 
   TrashSyncService._privateConstructor();
@@ -46,6 +48,9 @@ class TrashSyncService {
       await _trashDB
           .delete(diff.restoredFiles.map((e) => e.uploadedFileID).toList());
     }
+
+    await _updateIgnoredFiles(diff);
+
     if (diff.lastSyncedTimeStamp != 0) {
       await setSyncTime(diff.lastSyncedTimeStamp);
     }
@@ -54,6 +59,26 @@ class TrashSyncService {
     }
   }
 
+  Future<void> _updateIgnoredFiles(Diff diff) async {
+    final ignoredFiles = <IgnoredFile>[];
+    for (TrashFile t in diff.trashedFiles) {
+      final file = IgnoredFile.fromTrashItem(t);
+      if (file != null) {
+        ignoredFiles.add(file);
+      }
+    }
+    for (TrashFile t in diff.deletedFiles) {
+      final file = IgnoredFile.fromTrashItem(t);
+      if (file != null) {
+        ignoredFiles.add(file);
+      }
+    }
+    if (ignoredFiles.isNotEmpty) {
+      _logger.fine('updating ${ignoredFiles.length} ignored files ');
+      await IgnoreFilesDB.instance.insertMultiple(ignoredFiles);
+    }
+  }
+
   Future<void> setSyncTime(int time) async {
     if (time == null) {
       return _prefs.remove(kLastTrashSyncTime);