Bladeren bron

Merge pull request #457 from ente-io/match_bug_fix

Fix: Set localID for all files with same uploadedFileID
Manav 2 jaren geleden
bovenliggende
commit
a3109c1dfa
3 gewijzigde bestanden met toevoegingen van 42 en 6 verwijderingen
  1. 10 0
      lib/db/files_db.dart
  2. 31 5
      lib/utils/file_uploader.dart
  3. 1 1
      lib/utils/file_uploader_util.dart

+ 10 - 0
lib/db/files_db.dart

@@ -894,6 +894,16 @@ class FilesDB {
     );
   }
 
+  Future<int> updateLocalIDForUploaded(int uploadedID, String localID) async {
+    final db = await instance.database;
+    return await db.update(
+      table,
+      {columnLocalID: localID},
+      where: '$columnUploadedFileID = ? AND $columnLocalID IS NULL',
+      whereArgs: [uploadedID],
+    );
+  }
+
   Future<int> delete(int uploadedFileID) async {
     final db = await instance.database;
     return db.delete(

+ 31 - 5
lib/utils/file_uploader.dart

@@ -508,9 +508,8 @@ class FileUploader {
     if (existingUploadedFiles?.isEmpty ?? true) {
       // continueUploading this file
       return Tuple2(false, fileToUpload);
-    } else {
-      debugPrint("Found some matches");
     }
+
     // case a
     final File sameLocalSameCollection = existingUploadedFiles.firstWhere(
       (e) =>
@@ -518,12 +517,19 @@ class FileUploader {
       orElse: () => null,
     );
     if (sameLocalSameCollection != null) {
-      debugPrint(
+      _logger.fine(
         "sameLocalSameCollection: \n toUpload  ${fileToUpload.tag()} "
         "\n existing: ${sameLocalSameCollection.tag()}",
       );
       // should delete the fileToUploadEntry
       await FilesDB.instance.deleteByGeneratedID(fileToUpload.generatedID);
+
+      Bus.instance.fire(
+        LocalPhotosUpdatedEvent(
+          [fileToUpload],
+          type: EventType.deletedFromEverywhere, //
+        ),
+      );
       return Tuple2(true, sameLocalSameCollection);
     }
 
@@ -536,13 +542,23 @@ class FileUploader {
     if (fileMissingLocalButSameCollection != null) {
       // update the local id of the existing file and delete the fileToUpload
       // entry
-      debugPrint(
+      _logger.fine(
         "fileMissingLocalButSameCollection: \n toUpload  ${fileToUpload.tag()} "
         "\n existing: ${fileMissingLocalButSameCollection.tag()}",
       );
       fileMissingLocalButSameCollection.localID = fileToUpload.localID;
-      await FilesDB.instance.insert(fileMissingLocalButSameCollection);
+      // set localID for the given uploadedID across collections
+      await FilesDB.instance.updateLocalIDForUploaded(
+        fileMissingLocalButSameCollection.uploadedFileID,
+        fileToUpload.localID,
+      );
       await FilesDB.instance.deleteByGeneratedID(fileToUpload.generatedID);
+      Bus.instance.fire(
+        LocalPhotosUpdatedEvent(
+          [fileToUpload],
+          type: EventType.deletedFromEverywhere, //
+        ),
+      );
       return Tuple2(true, fileMissingLocalButSameCollection);
     }
 
@@ -565,6 +581,16 @@ class FileUploader {
       );
       return Tuple2(true, linkedFile);
     }
+    final Set<String> matchLocalIDs = existingUploadedFiles
+        .where(
+          (e) => e.localID != null,
+        )
+        .map((e) => e.localID)
+        .toSet();
+    _logger.fine(
+      "Found hashMatch but probably with diff localIDs "
+      "$matchLocalIDs",
+    );
     // case e
     return Tuple2(false, fileToUpload);
   }

+ 1 - 1
lib/utils/file_uploader_util.dart

@@ -103,7 +103,7 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
       _logger.severe(errMsg);
       throw InvalidFileUploadState(errMsg);
     }
-    String livePhotoVideoHash =
+    final String livePhotoVideoHash =
         Sodium.bin2base64(await CryptoUtil.getHash(videoUrl));
     // imgHash:vidHash
     fileHash = '$fileHash$kLivePhotoHashSeparator$livePhotoVideoHash';