diff --git a/lib/db/files_db.dart b/lib/db/files_db.dart index 698965e0d..68325a668 100644 --- a/lib/db/files_db.dart +++ b/lib/db/files_db.dart @@ -894,6 +894,16 @@ class FilesDB { ); } + Future 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 delete(int uploadedFileID) async { final db = await instance.database; return db.delete( diff --git a/lib/utils/file_uploader.dart b/lib/utils/file_uploader.dart index c6142b8f9..d89753785 100644 --- a/lib/utils/file_uploader.dart +++ b/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 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); } diff --git a/lib/utils/file_uploader_util.dart b/lib/utils/file_uploader_util.dart index 60bc10c69..fab5f0fc5 100644 --- a/lib/utils/file_uploader_util.dart +++ b/lib/utils/file_uploader_util.dart @@ -103,7 +103,7 @@ Future _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';