瀏覽代碼

Merge pull request #776 from ente-io/bug_fixes

Trash/Restore: Bug fixes
Neeraj Gupta 2 年之前
父節點
當前提交
ac8eb5fdad
共有 3 個文件被更改,包括 19 次插入2 次删除
  1. 7 0
      lib/services/collections_service.dart
  2. 10 0
      lib/services/trash_sync_service.dart
  3. 2 2
      lib/utils/file_util.dart

+ 7 - 0
lib/services/collections_service.dart

@@ -884,6 +884,8 @@ class CollectionsService {
     final params = <String, dynamic>{};
     params["collectionID"] = toCollectionID;
     final toCollectionKey = getCollectionKey(toCollectionID);
+    final Set<String> existingLocalIDS =
+        await FilesDB.instance.getExistingLocalFileIDs();
     final batchedFiles = files.chunks(batchSize);
     for (final batch in batchedFiles) {
       params["files"] = [];
@@ -892,6 +894,11 @@ class CollectionsService {
         file.generatedID =
             null; // So that a new entry is created in the FilesDB
         file.collectionID = toCollectionID;
+        // During restore, if trash file local ID is not present in currently
+        // imported files, treat the file as deleted from device
+        if (file.localID != null && !existingLocalIDS.contains(file.localID)) {
+          file.localID = null;
+        }
         final encryptedKeyData = CryptoUtil.encryptSync(key, toCollectionKey);
         file.encryptedKey = Sodium.bin2base64(encryptedKeyData.encryptedData!);
         file.keyDecryptionNonce = Sodium.bin2base64(encryptedKeyData.nonce!);

+ 10 - 0
lib/services/trash_sync_service.dart

@@ -5,6 +5,7 @@ import 'package:logging/logging.dart';
 import 'package:photos/core/constants.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/core/network.dart';
+import 'package:photos/db/files_db.dart';
 import 'package:photos/db/trash_db.dart';
 import 'package:photos/events/collection_updated_event.dart';
 import 'package:photos/events/force_reload_trash_page_event.dart';
@@ -40,9 +41,18 @@ class TrashSyncService {
     bool isLocalTrashUpdated = false;
     _logger.fine('sync trash sinceTime : $lastSyncTime');
     final diff = await _diffFetcher.getTrashFilesDiff(lastSyncTime);
+    Set<String>? localFileIDs;
     if (diff.trashedFiles.isNotEmpty) {
       isLocalTrashUpdated = true;
+      localFileIDs ??= await FilesDB.instance.getExistingLocalFileIDs();
       _logger.fine("inserting ${diff.trashedFiles.length} items in trash");
+      // During sync, if trash file local ID is not present in currently
+      // imported files, treat the file as deleted from device
+      for (var trash in diff.trashedFiles) {
+        if (trash.localID != null && !localFileIDs.contains(trash.localID)) {
+          trash.localID = null;
+        }
+      }
       await _trashDB.insertMultiple(diff.trashedFiles);
     }
     if (diff.deletedUploadIDs.isNotEmpty) {

+ 2 - 2
lib/utils/file_util.dart

@@ -51,8 +51,8 @@ Future<io.File?> getFile(
       );
       // do not cache origin file for IOS as they are immediately deleted
       // after usage
-      if (!(isOrigin && Platform.isIOS && diskFile != null)) {
-        FileLruCache.put(key, diskFile!);
+      if (!(isOrigin && Platform.isIOS) && diskFile != null) {
+        FileLruCache.put(key, diskFile);
       }
       return diskFile;
     }