Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Ignore fileUpdate event if asset was recently requested

Neeraj Gupta6febb5447831
diff --git a/mobile/lib/services/sync/local_sync_service.dart b/mobile/lib/services/sync/local_sync_service.dart
index aa630c2d73..3c8fe3d607 100644
--- a/mobile/lib/services/sync/local_sync_service.dart
+++ b/mobile/lib/services/sync/local_sync_service.dart
@@ -4,6 +4,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:photo_manager/photo_manager.dart';
+import "package:photos/core/cache/lru_map.dart";
import 'package:photos/core/configuration.dart';
import "package:photos/core/errors.dart";
import 'package:photos/core/event_bus.dart';
@@ -29,6 +30,11 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:synchronized/synchronized.dart';
import 'package:tuple/tuple.dart';
+// This map is used to track if a iOS origin file is being fetched for uploading
+// or ML processing. In such cases, we want to ignore these files if they come in response
+// from the local sync service. When a file is download
+final LRUMap<String, bool> trackOriginFetchForUploadOrML = LRUMap(200);
+
class LocalSyncService {
final _logger = Logger("LocalSyncService");
final _db = FilesDB.instance;
@@ -297,13 +303,39 @@ class LocalSyncService {
conflictAlgorithm: SqliteAsyncConflictAlgorithm.ignore,
);
_logger.info('Inserted ${files.length} out of ${allFiles.length} files');
- if (allFiles.isNotEmpty) {
+ _checkAndFireLocalAssetUpdateEvent(allFiles, files);
+ }
+ await _prefs.setInt(kDbUpdationTimeKey, toTime);
+ }
+
+ void _checkAndFireLocalAssetUpdateEvent(
+ List<EnteFile> allFiles,
+ List<EnteFile> files,
+ ) {
+ if (allFiles.isNotEmpty) {
+ // If there's no new file, verify that the all files don't just contain
+ // the files that are recently fetched for upload or ML processing
+ if (files.isEmpty) {
+ allFiles.removeWhere(
+ (file) =>
+ trackOriginFetchForUploadOrML.get(file.localID ?? '') ?? false,
+ );
+ if (allFiles.isNotEmpty) {
+ _logger.info(
+ "Firing LocalPhotosUpdatedEvent with ${allFiles.length} files",
+ );
+ Bus.instance.fire(
+ LocalPhotosUpdatedEvent(allFiles, source: "loadedPhoto"),
+ );
+ } else {
+ _logger.info("No new files to update after filtering");
+ }
+ } else {
Bus.instance.fire(
LocalPhotosUpdatedEvent(allFiles, source: "loadedPhoto"),
);
}
}
- await _prefs.setInt(kDbUpdationTimeKey, toTime);
}
Future<void> _trackUpdatedFiles(
@@ -318,11 +350,20 @@ class LocalSyncService {
)
.map((e) => e.localID!)
.toList();
+
if (updatedLocalIDs.isNotEmpty) {
- await FileUpdationDB.instance.insertMultiple(
- updatedLocalIDs,
- FileUpdationDB.modificationTimeUpdated,
+ final int updateCount = updatedLocalIDs.length;
+ updatedLocalIDs
+ .removeWhere((x) => trackOriginFetchForUploadOrML.get(x) ?? false);
+ _logger.info(
+ "track ${updatedLocalIDs.length}/ $updateCount files due to modification change",
);
+ if (updatedLocalIDs.isEmpty) {
+ await FileUpdationDB.instance.insertMultiple(
+ updatedLocalIDs,
+ FileUpdationDB.modificationTimeUpdated,
+ );
+ }
}
}
diff --git a/mobile/lib/utils/file_uploader_util.dart b/mobile/lib/utils/file_uploader_util.dart
index b49120ce7b..d59cce75b5 100644
--- a/mobile/lib/utils/file_uploader_util.dart
+++ b/mobile/lib/utils/file_uploader_util.dart
@@ -24,6 +24,7 @@ import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
import "package:photos/models/location/location.dart";
import "package:photos/models/metadata/file_magic.dart";
+import "package:photos/services/sync/local_sync_service.dart";
import "package:photos/utils/exif_util.dart";
import 'package:photos/utils/file_util.dart';
import "package:uuid/uuid.dart";
@@ -109,6 +110,9 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(
throw InvalidFileError("", InvalidReason.assetDeleted);
}
_assertFileType(asset, file);
+ if (Platform.isIOS) {
+ trackOriginFetchForUploadOrML.put(file.localID!, true);
+ }
sourceFile = await asset.originFile
.timeout(const Duration(seconds: 15))
.catchError((e) async {
diff --git a/mobile/lib/utils/ml_util.dart b/mobile/lib/utils/ml_util.dart
index f8e704e171..7a62aab885 100644
--- a/mobile/lib/utils/ml_util.dart
+++ b/mobile/lib/utils/ml_util.dart
@@ -1,4 +1,4 @@
-import "dart:io" show File;
+import "dart:io" show File, Platform;
import "dart:math" as math show sqrt, min, max;
import "package:flutter/services.dart" show PlatformException;
@@ -21,6 +21,7 @@ import "package:photos/services/machine_learning/ml_exceptions.dart";
import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";
import "package:photos/services/search_service.dart";
+import "package:photos/services/sync/local_sync_service.dart";
import "package:photos/utils/file_util.dart";
import "package:photos/utils/image_ml_util.dart";
import "package:photos/utils/network_util.dart";
@@ -348,6 +349,9 @@ Future<String> getImagePathForML(EnteFile enteFile) async {
);
}
try {
+ if (Platform.isIOS && enteFile.localID != null) {
+ trackOriginFetchForUploadOrML.put(enteFile.localID!, true);
+ }
file = await getFile(enteFile, isOrigin: true);
} catch (e, s) {
_logger.severe(