Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Delete iOS origin exports after ML processing

ashil-pilot7fea744d80dd
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 0d49b2bfbd..8625d35517 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -1,6 +1,6 @@
import "dart:async";
import "dart:convert" show jsonEncode;
-import "dart:io" show Platform;
+import "dart:io" show File, PathNotFoundException, Platform;
import "dart:math" show min;
import "dart:typed_data" show Uint8List;
@@ -749,12 +749,13 @@ class MLService {
bool actuallyRanML = false;
final mlDataDB = _dbForMode(instruction.mode);
+ MLImageSource? source;
try {
- final String filePath = await getImagePathForML(instruction.file);
+ source = await getImagePathForML(instruction.file);
final MLResult? result = await MLIndexingIsolate.instance.analyzeImage(
instruction,
- filePath,
+ source.imagePath,
);
// Check if there's no result simply because MLController paused indexing
if (result == null) {
@@ -962,6 +963,20 @@ class MLService {
await mlDataDB.deletePetDataForFiles([instruction.fileKey]);
}
return false;
+ } finally {
+ if (source != null && source.shouldDeleteAfterMLProcessing) {
+ try {
+ await File(source.imagePath).delete();
+ } on PathNotFoundException {
+ // Already deleted.
+ } catch (e, s) {
+ _logger.warning(
+ "Failed to delete origin file exported for ML at ${source.imagePath}",
+ e,
+ s,
+ );
+ }
+ }
}
}
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index 829f7a2c87..16c3289b20 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -696,8 +696,17 @@ Future<int> _getIndexableFileCount({required MLMode mode}) async {
return getIndexableFileCount();
}
-Future<String> getImagePathForML(EnteFile enteFile) async {
+typedef MLImageSource = ({
+ String imagePath,
+ bool shouldDeleteAfterMLProcessing,
+});
+
+/// Returns the path to use for ML processing of [enteFile], and whether the
+/// caller must delete that file afterwards (true only for iOS photo_manager
+/// origin exports).
+Future<MLImageSource> getImagePathForML(EnteFile enteFile) async {
String? imagePath;
+ bool shouldDeleteAfterMLProcessing = false;
final stopwatch = Stopwatch()..start();
File? file;
@@ -725,6 +734,11 @@ Future<String> getImagePathForML(EnteFile enteFile) async {
trackOriginFetchForUploadOrML.put(enteFile.localID!, true);
}
file = await getFile(enteFile, isOrigin: true);
+ shouldDeleteAfterMLProcessing =
+ Platform.isIOS &&
+ file != null &&
+ !enteFile.isRemoteFile &&
+ !enteFile.isSharedMediaToAppSandbox;
} catch (e, s) {
_logger.severe("Could not get file for $enteFile", e, s);
}
@@ -742,7 +756,10 @@ Future<String> getImagePathForML(EnteFile enteFile) async {
throw CouldNotRetrieveAnyFileData();
}
- return imagePath;
+ return (
+ imagePath: imagePath,
+ shouldDeleteAfterMLProcessing: shouldDeleteAfterMLProcessing,
+ );
}
bool _shouldRunIndexing(