Skip to content

armgit / ente

Public Imported from GitHub
0

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

Evict ML-downloaded remote files from cache after indexing

ashil-pilot9c4546c25536
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index 1726a4565c..f4e4f4e496 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -5,7 +5,9 @@ import "dart:math" show min;
import "dart:typed_data" show Uint8List;
import "package:flutter/foundation.dart" show kDebugMode;
+import "package:flutter_cache_manager/flutter_cache_manager.dart";
import "package:logging/logging.dart";
+import "package:photos/core/cache/video_cache_manager.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/db/files_db.dart";
import "package:photos/db/ml/db.dart";
@@ -752,6 +754,8 @@ class MLService {
final mlDataDB = _dbForMode(instruction.mode);
String? pathToDeleteAfterMLProcessing;
+ final bool shouldEvictRemoteCacheAfterMLProcessing =
+ _shouldEvictRemoteCacheAfterMLProcessing(instruction.file);
try {
final String filePath = await getImagePathForML(instruction.file);
if (_shouldDeleteAfterMLProcessing(instruction.file)) {
@@ -980,6 +984,9 @@ class MLService {
);
}
}
+ if (shouldEvictRemoteCacheAfterMLProcessing) {
+ await _evictRemoteCacheAfterMLProcessing(instruction.file);
+ }
}
}
@@ -989,6 +996,33 @@ class MLService {
!file.isRemoteOnlyFile;
}
+ bool _shouldEvictRemoteCacheAfterMLProcessing(EnteFile file) {
+ return file.isRemoteOnlyFile && file.fileType != FileType.video;
+ }
+
+ Future<void> _evictRemoteCacheAfterMLProcessing(EnteFile file) async {
+ try {
+ await DefaultCacheManager().removeFile(file.downloadUrl);
+ } catch (e, s) {
+ _logger.warning(
+ "Failed to evict remote image file cached for ML for fileID ${file.uploadedFileID}",
+ e,
+ s,
+ );
+ }
+ if (file.fileType == FileType.livePhoto) {
+ try {
+ await VideoCacheManager.instance.removeFile(file.downloadUrl);
+ } catch (e, s) {
+ _logger.warning(
+ "Failed to evict remote live photo video cached for ML for fileID ${file.uploadedFileID}",
+ e,
+ s,
+ );
+ }
+ }
+ }
+
bool _canRunMLFunction({required String function}) {
if (kDebugMode && Platform.isIOS && !_isIndexingOrClusteringRunning) {
return true;