Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] Retry ML decryption three times

laurenspriem3e7283eae11c
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart b/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart
index e0f3f0b9e9..aca75df5b1 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_file_retrieval.dart
@@ -3,22 +3,24 @@ import "dart:io";
import "package:photos/module/download/decrypt.dart";
import "package:photos/services/machine_learning/ml_exceptions.dart";
-typedef MlFileLoader = Future<File?> Function();
+typedef MlFileDownloadAndLoader = Future<File?> Function();
-Future<File?> loadFileForMlWithDecryptionRetry(MlFileLoader load) async {
- late final DownloadDecryptionError firstFailure;
- try {
- return await load();
- } on DownloadDecryptionError catch (error) {
- firstFailure = error;
- }
-
- try {
- return await load();
- } on DownloadDecryptionError catch (error) {
- if (error.encryptedFileSha1 == firstFailure.encryptedFileSha1) {
- throw RepeatedFileDecryptionError();
+Future<File?> downloadAndLoadFileForMlWithDecryptionRetry(
+ MlFileDownloadAndLoader downloadAndLoad,
+) async {
+ const retryCount = 3;
+ DownloadDecryptionError? firstFailure;
+ for (var attempt = 0; ; attempt++) {
+ try {
+ return await downloadAndLoad();
+ } on DownloadDecryptionError catch (error) {
+ firstFailure ??= error;
+ if (error.encryptedFileSha1 != firstFailure.encryptedFileSha1) {
+ rethrow;
+ }
+ if (attempt == retryCount) {
+ throw RepeatedFileDecryptionError();
+ }
}
- rethrow;
}
}
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index 0465beaf63..58b8ce53d5 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -740,7 +740,7 @@ Future<String> getImagePathForML(EnteFile enteFile) async {
modificationTime: enteFile.modificationTime,
);
}
- file = await loadFileForMlWithDecryptionRetry(
+ file = await downloadAndLoadFileForMlWithDecryptionRetry(
() => getFile(enteFile, isOrigin: true, throwOnDecryptionFailure: true),
);
} on RepeatedFileDecryptionError {
@@ -1040,7 +1040,7 @@ bool isExpectedMlSkipError(Object error) {
String formatExpectedMlSkipReasonForLogs(Object error) {
if (error is RepeatedFileDecryptionError) {
- return "origin decryption failed twice for identical ciphertext";
+ return "origin decryption failed after three retries with identical ciphertext";
}
final normalized = _normalizedErrorMessage(error);
if (normalized.contains("invalidimageformatexception")) {