Skip to content

armgit / ente

Public Imported from GitHub
0

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

Disable unbounded Dart image decode fallback

laurenspriem35df6c466999
diff --git a/mobile/apps/photos/lib/utils/image_decode_util.dart b/mobile/apps/photos/lib/utils/image_decode_util.dart
deleted file mode 100644
index e4c5c15c05..0000000000
--- a/mobile/apps/photos/lib/utils/image_decode_util.dart
+++ /dev/null
@@ -1,32 +0,0 @@
-import "dart:io" show File;
-import "dart:typed_data" show Uint8List;
-
-import "package:image/image.dart" as image;
-import "package:logging/logging.dart";
-
-final _logger = Logger("ImageDecodeUtil");
-
-Future<Uint8List?> createSafeJpegDecodeFallbackBytes({
- required String imagePath,
-}) async {
- final imageData = await File(imagePath).readAsBytes();
- return _createSafeJpegDecodeFallbackBytesFromData(imageData);
-}
-
-Uint8List? _createSafeJpegDecodeFallbackBytesFromData(Uint8List imageData) {
- try {
- final decoded = image.decodeImage(imageData);
- if (decoded == null) {
- return null;
- }
- return Uint8List.fromList(image.encodeJpg(decoded, quality: 95));
- } catch (e) {
- final firstLine = e.toString().split("\n").first.trim();
- _logger.warning(
- firstLine.isEmpty
- ? "Safe JPEG conversion failed"
- : "Safe JPEG conversion failed: $firstLine",
- );
- return null;
- }
-}
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index 179eb2d0de..5f6e2e8975 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -1,4 +1,4 @@
-import "dart:io" show Directory, File, Platform;
+import "dart:io" show File, Platform;
import "dart:math" as math show min, max;
import "package:dio/dio.dart";
@@ -33,7 +33,6 @@ import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/search_service.dart";
import "package:photos/services/sync/local_sync_service.dart";
import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;
-import "package:photos/utils/image_decode_util.dart";
import "package:photos/utils/network_util.dart";
final _logger = Logger("MlUtil");
@@ -878,46 +877,13 @@ Future<MLResult> analyzeImageRust(Map args) async {
rethrow;
}
- _logger.warning(
- "Rust decode failed for fileID $enteFileID (format: $fileFormat), retrying with JPEG fallback",
+ // A Rust decode error may be a resource-limit rejection. Retrying it
+ // through an unbounded Dart decoder can exhaust the mobile process.
+ throw _asInvalidImageFormatExceptionForRustDecodeFailure(
+ enteFileID: enteFileID,
+ fileFormat: fileFormat,
+ primaryError: e,
);
- final _DecodeFallbackFile? fallback;
- try {
- fallback = await _createJpegDecodeFallbackFile(imagePath: imagePath);
- } catch (fallbackError, fallbackStack) {
- _logger.severe(
- "JPEG fallback conversion threw for fileID $enteFileID (format: $fileFormat)",
- fallbackError,
- fallbackStack,
- );
- rethrow;
- }
- if (fallback == null) {
- _logger.warning(
- "JPEG fallback conversion returned null/empty bytes for fileID $enteFileID (format: $fileFormat); storing empty result instead",
- );
- throw _asInvalidImageFormatExceptionForRustDecodeFailure(
- enteFileID: enteFileID,
- fileFormat: fileFormat,
- primaryError: e,
- );
- }
-
- try {
- rustResult = await runRustAnalyzeForPath(fallback.file.path);
- _logger.info(
- "Rust decode fallback succeeded for fileID $enteFileID (original format: $fileFormat)",
- );
- } catch (retryError, retryStack) {
- _logger.severe(
- "Rust decode fallback retry failed for fileID $enteFileID (original format: $fileFormat)",
- retryError,
- retryStack,
- );
- rethrow;
- } finally {
- await _cleanupDecodeFallback(fallback);
- }
}
final result = MLResult.fromEnteFileID(enteFileID);
@@ -1081,50 +1047,10 @@ Exception _asInvalidImageFormatExceptionForRustDecodeFailure({
required int enteFileID,
required String fileFormat,
required Object primaryError,
- Object? fallbackError,
}) {
final details = <String>[
"InvalidImageFormatException: Rust decode failed for fileID $enteFileID (format: $fileFormat)",
"primary_error: $primaryError",
- if (fallbackError != null) "fallback_error: $fallbackError",
];
return Exception(details.join("; "));
}
-
-class _DecodeFallbackFile {
- final File file;
- final Directory directory;
-
- const _DecodeFallbackFile({required this.file, required this.directory});
-}
-
-Future<_DecodeFallbackFile?> _createJpegDecodeFallbackFile({
- required String imagePath,
-}) async {
- final convertedData = await createSafeJpegDecodeFallbackBytes(
- imagePath: imagePath,
- );
- if (convertedData == null || convertedData.isEmpty) {
- return null;
- }
-
- final tempDirectory = await Directory.systemTemp.createTemp(
- "ente_ml_decode_fallback_",
- );
- final fallbackFile = File("${tempDirectory.path}/ml_decode_retry.jpg");
- await fallbackFile.writeAsBytes(convertedData, flush: true);
- return _DecodeFallbackFile(file: fallbackFile, directory: tempDirectory);
-}
-
-Future<void> _cleanupDecodeFallback(_DecodeFallbackFile fallback) async {
- try {
- if (await fallback.file.exists()) {
- await fallback.file.delete();
- }
- if (await fallback.directory.exists()) {
- await fallback.directory.delete(recursive: true);
- }
- } catch (e, s) {
- _logger.warning("Could not cleanup decode fallback file", e, s);
- }
-}