π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob] Refactor local import (#5280)
Neeraj3b568bf9142c
diff --git a/mobile/lib/db/device_files_db.dart b/mobile/lib/db/device_files_db.dartindex fbd1649e26..b464039f2c 100644--- a/mobile/lib/db/device_files_db.dart+++ b/mobile/lib/db/device_files_db.dart@@ -8,7 +8,7 @@ import 'package:photos/models/device_collection.dart';import 'package:photos/models/file/file.dart';import 'package:photos/models/file_load_result.dart';import 'package:photos/models/upload_strategy.dart';-import 'package:photos/services/local/local_sync_util.dart';+import "package:photos/services/sync/import/model.dart";import 'package:sqflite/sqlite_api.dart';import 'package:tuple/tuple.dart';diff --git a/mobile/lib/services/sync/import/diff.dart b/mobile/lib/services/sync/import/diff.dartnew file mode 100644index 0000000000..f5e48f2c04--- /dev/null+++ b/mobile/lib/services/sync/import/diff.dart@@ -0,0 +1,120 @@+import "package:computer/computer.dart";+import "package:logging/logging.dart";+import "package:photo_manager/photo_manager.dart";+import "package:photos/models/file/file.dart";+import "package:photos/services/sync/import/model.dart";++class LocalDiffResult {+ // unique localPath Assets.+ final List<LocalPathAsset>? localPathAssets;++ // set of File object created from localPathAssets+ List<EnteFile>? uniqueLocalFiles;++ // newPathToLocalIDs represents new entries which needs to be synced to+ // the local db+ final Map<String, Set<String>>? newPathToLocalIDs;++ final Map<String, Set<String>>? deletePathToLocalIDs;++ LocalDiffResult({+ this.uniqueLocalFiles,+ this.localPathAssets,+ this.newPathToLocalIDs,+ this.deletePathToLocalIDs,+ });+}++Future<LocalDiffResult> getDiffFromExistingImport(+ List<LocalPathAsset> assets,+ // current set of assets available on device+ Set<String> existingIDs, // localIDs of files already imported in app+ Map<String, Set<String>> pathToLocalIDs,+) async {+ final Map<String, dynamic> args = <String, dynamic>{};+ args['assets'] = assets;+ args['existingIDs'] = existingIDs;+ args['pathToLocalIDs'] = pathToLocalIDs;+ final LocalDiffResult diffResult = await Computer.shared().compute(+ _getLocalAssetsDiff,+ param: args,+ taskName: "getLocalAssetsDiff",+ );+ if (diffResult.localPathAssets != null) {+ diffResult.uniqueLocalFiles =+ await _convertLocalAssetsToUniqueFiles(diffResult.localPathAssets!);+ }+ return diffResult;+}++Future<List<EnteFile>> _convertLocalAssetsToUniqueFiles(+ List<LocalPathAsset> assets,+) async {+ final Set<String> alreadySeenLocalIDs = <String>{};+ final List<EnteFile> files = [];+ for (LocalPathAsset localPathAsset in assets) {+ final String localPathName = localPathAsset.pathName;+ for (final String localID in localPathAsset.localIDs) {+ if (!alreadySeenLocalIDs.contains(localID)) {+ final assetEntity = await AssetEntity.fromId(localID);+ if (assetEntity == null) {+ Logger("_convertLocalAssetsToUniqueFiles")+ .warning('Failed to fetch asset with id $localID');+ continue;+ }+ files.add(+ await EnteFile.fromAsset(localPathName, assetEntity),+ );+ alreadySeenLocalIDs.add(localID);+ }+ }+ }+ return files;+}++// _getLocalAssetsDiff compares local db with the file system and compute+// the files which needs to be added or removed from device collection.+LocalDiffResult _getLocalAssetsDiff(Map<String, dynamic> args) {+ final List<LocalPathAsset> onDeviceLocalPathAsset = args['assets'];+ final Set<String> existingIDs = args['existingIDs'];+ final Map<String, Set<String>> pathToLocalIDs = args['pathToLocalIDs'];+ final Map<String, Set<String>> newPathToLocalIDs = <String, Set<String>>{};+ final Map<String, Set<String>> removedPathToLocalIDs =+ <String, Set<String>>{};+ final List<LocalPathAsset> unsyncedAssets = [];++ for (final localPathAsset in onDeviceLocalPathAsset) {+ final String pathID = localPathAsset.pathID;+ // Start identifying pathID to localID mapping changes which needs to be+ // synced+ final Set<String> candidateLocalIDsForRemoval =+ pathToLocalIDs[pathID] ?? <String>{};+ final Set<String> missingLocalIDsInPath = <String>{};+ for (final String localID in localPathAsset.localIDs) {+ if (candidateLocalIDsForRemoval.contains(localID)) {+ // remove the localID after checking. Any pending existing ID indicates+ // the the local file was removed from the path.+ candidateLocalIDsForRemoval.remove(localID);+ } else {+ missingLocalIDsInPath.add(localID);+ }+ }+ if (candidateLocalIDsForRemoval.isNotEmpty) {+ removedPathToLocalIDs[pathID] = candidateLocalIDsForRemoval;+ }+ if (missingLocalIDsInPath.isNotEmpty) {+ newPathToLocalIDs[pathID] = missingLocalIDsInPath;+ }+ // End++ localPathAsset.localIDs.removeAll(existingIDs);+ if (localPathAsset.localIDs.isNotEmpty) {+ unsyncedAssets.add(localPathAsset);+ }+ }+ return LocalDiffResult(+ localPathAssets: unsyncedAssets,+ newPathToLocalIDs: newPathToLocalIDs,+ deletePathToLocalIDs: removedPathToLocalIDs,+ );+}diff --git a/mobile/lib/services/local/local_sync_util.dart b/mobile/lib/services/sync/import/local_assets.dartsimilarity index 64%rename from mobile/lib/services/local/local_sync_util.dartrename to mobile/lib/services/sync/import/local_assets.dartindex 2be4707a22..4bb42911f1 100644--- a/mobile/lib/services/local/local_sync_util.dart+++ b/mobile/lib/services/sync/import/local_assets.dart@@ -7,6 +7,7 @@ import 'package:photo_manager/photo_manager.dart';import 'package:photos/core/event_bus.dart';import 'package:photos/events/local_import_progress.dart';import 'package:photos/models/file/file.dart';+import "package:photos/services/sync/import/model.dart";import 'package:tuple/tuple.dart';final _logger = Logger("FileSyncUtil");@@ -127,99 +128,6 @@ Future<List<LocalPathAsset>> getAllLocalAssets() async {return localPathAssets;}-Future<LocalDiffResult> getDiffWithLocal(- List<LocalPathAsset> assets,- // current set of assets available on device- Set<String> existingIDs, // localIDs of files already imported in app- Map<String, Set<String>> pathToLocalIDs,-) async {- final Map<String, dynamic> args = <String, dynamic>{};- args['assets'] = assets;- args['existingIDs'] = existingIDs;- args['pathToLocalIDs'] = pathToLocalIDs;- final LocalDiffResult diffResult = await Computer.shared().compute(- _getLocalAssetsDiff,- param: args,- taskName: "getLocalAssetsDiff",- );- if (diffResult.localPathAssets != null) {- diffResult.uniqueLocalFiles =- await _convertLocalAssetsToUniqueFiles(diffResult.localPathAssets!);- }- return diffResult;-}--// _getLocalAssetsDiff compares local db with the file system and compute-// the files which needs to be added or removed from device collection.-LocalDiffResult _getLocalAssetsDiff(Map<String, dynamic> args) {- final List<LocalPathAsset> onDeviceLocalPathAsset = args['assets'];- final Set<String> existingIDs = args['existingIDs'];- final Map<String, Set<String>> pathToLocalIDs = args['pathToLocalIDs'];- final Map<String, Set<String>> newPathToLocalIDs = <String, Set<String>>{};- final Map<String, Set<String>> removedPathToLocalIDs =- <String, Set<String>>{};- final List<LocalPathAsset> unsyncedAssets = [];-- for (final localPathAsset in onDeviceLocalPathAsset) {- final String pathID = localPathAsset.pathID;- // Start identifying pathID to localID mapping changes which needs to be- // synced- final Set<String> candidateLocalIDsForRemoval =- pathToLocalIDs[pathID] ?? <String>{};- final Set<String> missingLocalIDsInPath = <String>{};- for (final String localID in localPathAsset.localIDs) {- if (candidateLocalIDsForRemoval.contains(localID)) {- // remove the localID after checking. Any pending existing ID indicates- // the the local file was removed from the path.- candidateLocalIDsForRemoval.remove(localID);- } else {- missingLocalIDsInPath.add(localID);- }- }- if (candidateLocalIDsForRemoval.isNotEmpty) {- removedPathToLocalIDs[pathID] = candidateLocalIDsForRemoval;- }- if (missingLocalIDsInPath.isNotEmpty) {- newPathToLocalIDs[pathID] = missingLocalIDsInPath;- }- // End-- localPathAsset.localIDs.removeAll(existingIDs);- if (localPathAsset.localIDs.isNotEmpty) {- unsyncedAssets.add(localPathAsset);- }- }- return LocalDiffResult(- localPathAssets: unsyncedAssets,- newPathToLocalIDs: newPathToLocalIDs,- deletePathToLocalIDs: removedPathToLocalIDs,- );-}--Future<List<EnteFile>> _convertLocalAssetsToUniqueFiles(- List<LocalPathAsset> assets,-) async {- final Set<String> alreadySeenLocalIDs = <String>{};- final List<EnteFile> files = [];- for (LocalPathAsset localPathAsset in assets) {- final String localPathName = localPathAsset.pathName;- for (final String localID in localPathAsset.localIDs) {- if (!alreadySeenLocalIDs.contains(localID)) {- final assetEntity = await AssetEntity.fromId(localID);- if (assetEntity == null) {- _logger.warning('Failed to fetch asset with id $localID');- continue;- }- files.add(- await EnteFile.fromAsset(localPathName, assetEntity),- );- alreadySeenLocalIDs.add(localID);- }- }- }- return files;-}-/// returns a list of AssetPathEntity with relevant filter operations./// [needTitle] impacts the performance for fetching the actual [AssetEntity]/// in iOS. Same is true for [containsModifiedPath]@@ -314,36 +222,3 @@ Future<Tuple2<Set<String>, List<EnteFile>>> _getLocalIDsAndFilesFromAssets(}return Tuple2(localIDs, files);}--class LocalPathAsset {- final Set<String> localIDs;- final String pathID;- final String pathName;-- LocalPathAsset({- required this.localIDs,- required this.pathName,- required this.pathID,- });-}--class LocalDiffResult {- // unique localPath Assets.- final List<LocalPathAsset>? localPathAssets;-- // set of File object created from localPathAssets- List<EnteFile>? uniqueLocalFiles;-- // newPathToLocalIDs represents new entries which needs to be synced to- // the local db- final Map<String, Set<String>>? newPathToLocalIDs;-- final Map<String, Set<String>>? deletePathToLocalIDs;-- LocalDiffResult({- this.uniqueLocalFiles,- this.localPathAssets,- this.newPathToLocalIDs,- this.deletePathToLocalIDs,- });-}diff --git a/mobile/lib/services/sync/import/model.dart b/mobile/lib/services/sync/import/model.dartnew file mode 100644index 0000000000..68491cc39f--- /dev/null+++ b/mobile/lib/services/sync/import/model.dart@@ -0,0 +1,11 @@+class LocalPathAsset {+ final Set<String> localIDs;+ final String pathID;+ final String pathName;++ LocalPathAsset({+ required this.localIDs,+ required this.pathName,+ required this.pathID,+ });+}diff --git a/mobile/lib/services/sync/local_sync_service.dart b/mobile/lib/services/sync/local_sync_service.dartindex a7f81f27c5..d101b99338 100644--- a/mobile/lib/services/sync/local_sync_service.dart+++ b/mobile/lib/services/sync/local_sync_service.dart@@ -21,7 +21,9 @@ import "package:photos/models/ignored_file.dart";import "package:photos/service_locator.dart";import 'package:photos/services/app_lifecycle_service.dart';import "package:photos/services/ignored_files_service.dart";-import 'package:photos/services/local/local_sync_util.dart';+import "package:photos/services/sync/import/diff.dart";+import "package:photos/services/sync/import/local_assets.dart";+import "package:photos/services/sync/import/model.dart";import "package:photos/utils/standalone/debouncer.dart";import 'package:shared_preferences/shared_preferences.dart';import 'package:synchronized/synchronized.dart';@@ -172,7 +174,7 @@ class LocalSyncService {final Map<String, Set<String>> pathToLocalIDs =await _db.getDevicePathIDToLocalIDMap();- final localDiffResult = await getDiffWithLocal(+ final localDiffResult = await getDiffFromExistingImport(localAssets,existingLocalFileIDs,pathToLocalIDs,