Skip to content

armgit / ente

Public Imported from GitHub
0

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

Handle EXIF metadata parse edge cases

Neeraj Gupta6e7389ef1be8
diff --git a/mobile/apps/photos/lib/utils/exif_util.dart b/mobile/apps/photos/lib/utils/exif_util.dart
index 3ca0955ccd..98d97ca92a 100644
--- a/mobile/apps/photos/lib/utils/exif_util.dart
+++ b/mobile/apps/photos/lib/utils/exif_util.dart
@@ -141,7 +141,11 @@ Future<ParsedExifDateTime?> tryParseExifDateTime(
break;
}
}
- return getDateTimeInDeviceTimezone(exifTime, exifOffsetTime);
+ try {
+ return getDateTimeInDeviceTimezone(exifTime, exifOffsetTime);
+ } on FormatException catch (e) {
+ _logger.warning("Ignoring invalid EXIF date time", e);
+ }
} catch (e, s) {
_logger.severe("failed to getCreationTimeFromEXIF", e, s);
}
@@ -341,16 +345,10 @@ GPSData gpsDataFromExif(Map<String, IfdTag> exif) {
"longRef": null,
};
if (exif["GPS GPSLatitude"] != null) {
- exifLocationData["lat"] = exif["GPS GPSLatitude"]!.values
- .toList()
- .map((e) => ((e as Ratio).numerator / e.denominator))
- .toList();
+ exifLocationData["lat"] = _gpsCoordinateParts(exif["GPS GPSLatitude"]!);
}
if (exif["GPS GPSLongitude"] != null) {
- exifLocationData["long"] = exif["GPS GPSLongitude"]!.values
- .toList()
- .map((e) => ((e as Ratio).numerator / e.denominator))
- .toList();
+ exifLocationData["long"] = _gpsCoordinateParts(exif["GPS GPSLongitude"]!);
}
if (exif["GPS GPSLatitudeRef"] != null) {
exifLocationData["latRef"] = exif["GPS GPSLatitudeRef"].toString();
@@ -365,3 +363,14 @@ GPSData gpsDataFromExif(Map<String, IfdTag> exif) {
exifLocationData["long"],
);
}
+
+List<double> _gpsCoordinateParts(IfdTag tag) {
+ return tag.values.toList().map(_gpsCoordinatePart).toList();
+}
+
+double _gpsCoordinatePart(dynamic value) {
+ if (value is Ratio) {
+ return value.numerator / value.denominator;
+ }
+ return (value as num).toDouble();
+}
diff --git a/mobile/apps/photos/test/utils/exif_util_test.dart b/mobile/apps/photos/test/utils/exif_util_test.dart
index d5ade2ffcd..89b1a4b7ef 100644
--- a/mobile/apps/photos/test/utils/exif_util_test.dart
+++ b/mobile/apps/photos/test/utils/exif_util_test.dart
@@ -1,3 +1,4 @@
+import "package:exif_reader/exif_reader.dart";
import "package:photos/utils/exif_util.dart";
import "package:test/test.dart";
@@ -213,4 +214,38 @@ void main() {
);
});
});
+
+ group("tryParseExifDateTime", () {
+ test("ignores invalid date values from file metadata", () async {
+ final parsed = await tryParseExifDateTime(null, {
+ kDateTimeOriginal: _ifdTag(
+ const IfdNone(),
+ printable: "2025:02:30 08:00:00.945",
+ ),
+ });
+
+ expect(parsed, isNull);
+ });
+ });
+
+ group("gpsDataFromExif", () {
+ test("parses integer GPS coordinate parts", () {
+ final gpsData = gpsDataFromExif({
+ "GPS GPSLatitude": _ifdTag(const IfdInts([40, 26, 46])),
+ "GPS GPSLatitudeRef": _ifdTag(const IfdInts([0]), printable: "N"),
+ "GPS GPSLongitude": _ifdTag(const IfdInts([79, 58, 56])),
+ "GPS GPSLongitudeRef": _ifdTag(const IfdInts([0]), printable: "W"),
+ });
+
+ final location = gpsData.toLocationObj();
+
+ expect(location, isNotNull);
+ expect(location!.latitude, closeTo(40.446111, 0.000001));
+ expect(location.longitude, closeTo(-79.982222, 0.000001));
+ });
+ });
+}
+
+IfdTag _ifdTag(IfdValues values, {String printable = ""}) {
+ return IfdTag(tag: 0, tagType: "", printable: printable, values: values);
}