Skip to content

armgit / ente

Public Imported from GitHub
0

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

Reject partial and sentinel capture dates for local photos

Neeraj Gupta1cd32470bbb6
diff --git a/rust/crates/exif/src/photo.rs b/rust/crates/exif/src/photo.rs
index c6b435a9fd..6110fdd3b3 100644
--- a/rust/crates/exif/src/photo.rs
+++ b/rust/crates/exif/src/photo.rs
@@ -8,6 +8,7 @@ pub struct CaptureDateTime {
pub date_time: String,
pub offset_time: Option<String>,
pub timestamp_micros: Option<i64>,
+ pub precision: DateTimePrecision,
pub source: DateTimeSource,
}
pub fn capture_date_time(metadata: &Metadata) -> Option<CaptureDateTime> {
@@ -64,6 +65,7 @@ pub fn capture_date_time_with(
)
}),
timestamp_micros: absolute.unix_micros(),
+ precision: value.precision,
source,
};
accept(&mut result).then_some(result)
diff --git a/rust/crates/exif/tests/integration/capture.rs b/rust/crates/exif/tests/integration/capture.rs
index f614aac401..c96da3e576 100644
--- a/rust/crates/exif/tests/integration/capture.rs
+++ b/rust/crates/exif/tests/integration/capture.rs
@@ -239,15 +239,32 @@ fn host_policy_can_resolve_local_time_and_reject_candidates_before_fallback() {
#[test]
fn reduced_xmp_dates_use_period_start_without_inventing_an_embedded_offset() {
- for (value, expected, timestamp) in [
- ("1971", "1971-01-01T00:00:00", Some(31_536_000_000_000)),
- ("1970-02", "1970-02-01T00:00:00", Some(2_678_400_000_000)),
- ("1970-01-02", "1970-01-02T00:00:00", Some(86_400_000_000)),
- ("1970-01-02T00:15", "1970-01-02T00:15:00", None),
+ use DateTimePrecision::{Day, Minute, Month, Year};
+ for (value, expected, timestamp, precision) in [
+ (
+ "1971",
+ "1971-01-01T00:00:00",
+ Some(31_536_000_000_000),
+ Year,
+ ),
+ (
+ "1970-02",
+ "1970-02-01T00:00:00",
+ Some(2_678_400_000_000),
+ Month,
+ ),
+ (
+ "1970-01-02",
+ "1970-01-02T00:00:00",
+ Some(86_400_000_000),
+ Day,
+ ),
+ ("1970-01-02T00:15", "1970-01-02T00:15:00", None, Minute),
(
"1970-01-02T00:15+00:30",
"1970-01-02T00:15:00",
Some(85_500_000_000),
+ Minute,
),
] {
assert_eq!(DateTime::parse(value), None);
@@ -260,6 +277,7 @@ fn reduced_xmp_dates_use_period_start_without_inventing_an_embedded_offset() {
.unwrap();
assert_eq!(capture.date_time, expected);
assert_eq!(capture.timestamp_micros, timestamp);
+ assert_eq!(capture.precision, precision);
if value.len() <= 10 {
assert_eq!(capture.offset_time, None);
}
@@ -308,6 +326,14 @@ fn generated_iptc_dates_pair_only_their_own_time_dataset() {
let metadata = read(&iptc_tiff(&fields), mode);
let capture = metadata.capture_date_time().unwrap();
assert_eq!(capture.source, DateTimeSource::Iptc(55, 60));
+ assert_eq!(
+ capture.precision,
+ if time.is_some() {
+ DateTimePrecision::Second
+ } else {
+ DateTimePrecision::Day
+ }
+ );
assert_eq!(capture.offset_time.as_deref(), offset);
assert_eq!(capture.timestamp_micros, timestamp);
}
diff --git a/rust/crates/photos/src/metadata.rs b/rust/crates/photos/src/metadata.rs
index 6a54e345ac..60f0db01f3 100644
--- a/rust/crates/photos/src/metadata.rs
+++ b/rust/crates/photos/src/metadata.rs
@@ -1,6 +1,6 @@
use std::{fs::File, path::Path};
-use ente_exif::{CaptureDateTime, Dimensions, Error, Limits, Location, Mode};
+use ente_exif::{CaptureDateTime, DateTimePrecision, Dimensions, Error, Limits, Location, Mode};
#[derive(Debug)]
pub struct PhotoMetadata {
@@ -13,7 +13,11 @@ pub fn read_photo_metadata(path: impl AsRef<Path>) -> Result<PhotoMetadata, Erro
let metadata = ente_exif::read(&mut File::open(path)?, Mode::Summary, Limits::default())?;
Ok(PhotoMetadata {
dimensions: metadata.display_dimensions(),
- capture_date_time: metadata.capture_date_time(),
+ capture_date_time: metadata.capture_date_time_with(|date| {
+ date.precision == DateTimePrecision::Second
+ && date.timestamp_micros != Some(0)
+ && date.date_time != "4501-01-01T00:00:00"
+ }),
location: metadata.exif_location().or_else(|| metadata.xmp_location()),
})
}
diff --git a/rust/crates/photos/tests/metadata_test.rs b/rust/crates/photos/tests/metadata_test.rs
index a657aa92b5..629343491d 100644
--- a/rust/crates/photos/tests/metadata_test.rs
+++ b/rust/crates/photos/tests/metadata_test.rs
@@ -1,25 +1,13 @@
use std::io::Write;
-use ente_exif::{Dimensions, Location};
-use ente_photos::metadata::read_photo_metadata;
+use ente_exif::{DateTimeSource, Dimensions, Location, namespace};
+use ente_photos::metadata::{PhotoMetadata, read_photo_metadata};
#[test]
fn reads_display_dimensions_capture_time_and_xmp_location() {
let properties = r#"tiff:Orientation="6" exif:DateTimeOriginal="2024-01-01T12:00:00.123456-00:30"
exif:GPSLatitude="40,30N" exif:GPSLongitude="74,15W""#;
- let xmp = format!(
- "http://ns.adobe.com/xap/1.0/\0<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description xmlns:exif=\"http://ns.adobe.com/exif/1.0/\" xmlns:tiff=\"http://ns.adobe.com/tiff/1.0/\" {properties}/></rdf:RDF>"
- );
- let mut file = tempfile::NamedTempFile::new().unwrap();
- file.write_all(&[0xff, 0xd8, 0xff, 0xe1]).unwrap();
- file.write_all(&u16::try_from(xmp.len() + 2).unwrap().to_be_bytes())
- .unwrap();
- file.write_all(xmp.as_bytes()).unwrap();
- file.write_all(&[
- 0xff, 0xc0, 0, 11, 8, 0, 48, 0, 64, 1, 1, 0x11, 0, 0xff, 0xd9,
- ])
- .unwrap();
- let metadata = read_photo_metadata(file.path()).unwrap();
+ let metadata = read(properties);
assert_eq!(
metadata.dimensions,
Some(Dimensions {
@@ -39,3 +27,64 @@ fn reads_display_dimensions_capture_time_and_xmp_location() {
})
);
}
+
+#[test]
+fn skips_partial_and_sentinel_capture_dates_and_continues_to_fallbacks() {
+ for (original, rejected) in [
+ ("2004", true),
+ ("2004-06", true),
+ ("2004-06-15", true),
+ ("2004-06-15T12:30Z", true),
+ ("1970-01-01T00:00:00Z", true),
+ ("1970-01-01T05:30:00+05:30", true),
+ ("4501-01-01T00:00:00.000Z", true),
+ ("4501-01-01T00:00:00+05:30", true),
+ ("1970-01-01T00:00:00+05:30", false),
+ ("1969-12-31T23:59:59Z", false),
+ ("2024-01-01T12:00:00", false),
+ ("2024-01-01T00:00:00Z", false),
+ ("4501-01-02T00:00:00Z", false),
+ ] {
+ let metadata = read(&format!(
+ r#"exif:DateTimeOriginal="{original}" xmp:CreateDate="2024-01-02T12:00:00Z""#
+ ));
+ let expected = if rejected {
+ DateTimeSource::Xmp(namespace::XMP, "CreateDate")
+ } else {
+ DateTimeSource::Xmp(namespace::EXIF, "DateTimeOriginal")
+ };
+ assert_eq!(
+ metadata.capture_date_time.unwrap().source,
+ expected,
+ "{original}"
+ );
+ }
+ assert!(
+ read(r#"exif:DateTimeOriginal="2004" xmp:CreateDate="2004-06-15""#)
+ .capture_date_time
+ .is_none()
+ );
+ assert!(
+ read(
+ r#"exif:DateTimeOriginal="4501-01-01T00:00:00Z" xmp:CreateDate="1970-01-01T00:00:00Z""#
+ )
+ .capture_date_time
+ .is_none()
+ );
+}
+
+fn read(properties: &str) -> PhotoMetadata {
+ let xmp = format!(
+ "http://ns.adobe.com/xap/1.0/\0<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description xmlns:exif=\"http://ns.adobe.com/exif/1.0/\" xmlns:tiff=\"http://ns.adobe.com/tiff/1.0/\" xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\" {properties}/></rdf:RDF>"
+ );
+ let mut file = tempfile::NamedTempFile::new().unwrap();
+ file.write_all(&[0xff, 0xd8, 0xff, 0xe1]).unwrap();
+ file.write_all(&u16::try_from(xmp.len() + 2).unwrap().to_be_bytes())
+ .unwrap();
+ file.write_all(xmp.as_bytes()).unwrap();
+ file.write_all(&[
+ 0xff, 0xc0, 0, 11, 8, 0, 48, 0, 64, 1, 1, 0x11, 0, 0xff, 0xd9,
+ ])
+ .unwrap();
+ read_photo_metadata(file.path()).unwrap()
+}