Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move to using image package as decode fallback to prevent crashes

laurenspriem61411950534d
diff --git a/mobile/apps/photos/lib/utils/image_ml_util.dart b/mobile/apps/photos/lib/utils/image_ml_util.dart
index a1800de249..72a14704d2 100644
--- a/mobile/apps/photos/lib/utils/image_ml_util.dart
+++ b/mobile/apps/photos/lib/utils/image_ml_util.dart
@@ -7,6 +7,7 @@ import "dart:ui";
import "package:exif_reader/exif_reader.dart";
import 'package:flutter/painting.dart' as paint show decodeImageFromList;
import "package:flutter_image_compress/flutter_image_compress.dart";
+import "package:image/image.dart" as img;
import "package:logging/logging.dart";
import 'package:ml_linalg/linalg.dart';
import "package:photos/models/ml/face/box.dart";
@@ -136,6 +137,27 @@ Future<DecodedImage> decodeImageFromPath(
);
}
+Future<Uint8List?> createSafeJpegDecodeFallbackBytes({
+ required String imagePath,
+}) async {
+ final imageData = await File(imagePath).readAsBytes();
+ return createSafeJpegDecodeFallbackBytesFromData(imageData);
+}
+
+Uint8List? createSafeJpegDecodeFallbackBytesFromData(Uint8List imageData) {
+ try {
+ final decoded = img.decodeImage(imageData);
+ if (decoded == null) {
+ return null;
+ }
+ final jpeg = img.encodeJpg(decoded, quality: 95);
+ return Uint8List.fromList(jpeg);
+ } catch (e, s) {
+ _logger.warning("Safe JPEG conversion failed", e, s);
+ return null;
+ }
+}
+
/// Decodes [Uint8List] image data to an ui.[Image] object.
Future<Image> decodeImageFromData(Uint8List imageData) async {
// Decoding using flutter paint. This is the fastest and easiest method.
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index 4dc2d8c176..6d519d8a2f 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -3,7 +3,6 @@ import "dart:math" as math show sqrt, min, max;
import "package:ente_pure_utils/ente_pure_utils.dart";
import "package:flutter/services.dart" show PlatformException;
-import "package:flutter_image_compress/flutter_image_compress.dart";
import "package:logging/logging.dart";
import "package:photos/db/files_db.dart";
import "package:photos/db/ml/db.dart";
@@ -747,16 +746,6 @@ Future<MLResult> analyzeImageRust(Map args) async {
rethrow;
}
- if (_shouldSkipJpegDecodeFallback(fileFormat)) {
- _logger.warning(
- "Rust decode failed for fileID $enteFileID (format: $fileFormat); skipping JPEG fallback for known-unsupported RAW input",
- );
- throw _asInvalidImageFormatExceptionForRustDecodeFailure(
- enteFileID: enteFileID,
- fileFormat: fileFormat,
- primaryError: e,
- );
- }
_logger.warning(
"Rust decode failed for fileID $enteFileID (format: $fileFormat), retrying with JPEG fallback",
);
@@ -973,10 +962,6 @@ bool _isRustDecodeIssue(Object error) {
return message.contains("decode error");
}
-bool _shouldSkipJpegDecodeFallback(String fileFormat) {
- return isRawImageExtension(fileFormat);
-}
-
bool _shouldStoreEmptyResultForRustDecodeFailure({
required Object primaryError,
Object? fallbackError,
@@ -1091,17 +1076,8 @@ class _DecodeFallbackFile {
Future<_DecodeFallbackFile?> _createJpegDecodeFallbackFile({
required String imagePath,
}) async {
- final format = getExtension(imagePath);
- if (_shouldSkipJpegDecodeFallback(format)) {
- throw Exception(
- "InvalidImageFormatException: JPEG decode fallback skipped for unsupported RAW image format $format",
- );
- }
- final convertedData = await FlutterImageCompress.compressWithFile(
- imagePath,
- format: CompressFormat.jpeg,
- minWidth: 20000,
- minHeight: 20000,
+ final convertedData = await createSafeJpegDecodeFallbackBytes(
+ imagePath: imagePath,
);
if (convertedData == null || convertedData.isEmpty) {
return null;