π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Trim motion-photo tests and comments
Neeraj Guptae6e55c91b73c
diff --git a/rust/crates/photos/src/motion_photo/ftyp.rs b/rust/crates/photos/src/motion_photo/ftyp.rsindex 88ddaf67a5..353660526d 100644--- a/rust/crates/photos/src/motion_photo/ftyp.rs+++ b/rust/crates/photos/src/motion_photo/ftyp.rs@@ -3,23 +3,20 @@ use std::io::{self, Read};const FTYP_BOX_MIN_SIZE: u32 = 8;-// Real ftyp boxes are usually 16-32 bytes. The larger ceiling allows unusual-// brand lists while rejecting accidental matches in image data.+// Reject accidental ftyp matches in image data.const FTYP_BOX_MAX_SIZE: u32 = 1024;-// Image-only brands are intentionally excluded.const KNOWN_VIDEO_BRANDS: &[[u8; 4]] = &[*b"isom", *b"iso2", *b"iso5", *b"iso6", *b"mp41", *b"mp42", *b"mp71", *b"M4V ", *b"M4VP",*b"avc1", *b"mmp4", *b"3gp4", *b"3gp5", *b"3gp6", *b"qt ", *b"MSNV", *b"dash", *b"f4v ",];-// Some phones embed preview and full MP4s in either order. Return the largest-// valid segment.+// Phones may embed both preview and full videos.pub(super) fn find_largest_ftyp_segment<R: Read>(mut reader: R,size: usize,) -> io::Result<Option<VideoIndex>> {- // Retain enough bytes to recognize a size/type/brand header across reads.+ // Keep partial 12-byte headers across reads.const OVERLAP: usize = 11;let mut buffer = vec![0; BUFFER_SIZE + OVERLAP];let mut carried = 0;diff --git a/rust/crates/photos/src/motion_photo/mod.rs b/rust/crates/photos/src/motion_photo/mod.rsindex ab3013a6e1..2f99e6d84e 100644--- a/rust/crates/photos/src/motion_photo/mod.rs+++ b/rust/crates/photos/src/motion_photo/mod.rs@@ -83,12 +83,11 @@ fn get_motion_video_index<R: Read + Seek>(match xmp::extract_xmp(reader.take(size as u64)) {Ok(data) => Ok(extract_video_index_from_xmp(&data, size)),Err(MotionPhotoError::Xml(_)) => Ok(None),- // Propagate limit and I/O errors so callers preserve existing motion-photo tags.+ // None clears existing motion-photo tags.Err(err) => Err(err),}}-// Extract up to 64 MiB into memory. Use file extraction for larger videos.pub fn extract_motion_video_from_path<P: AsRef<Path>>(file_path: P,index: Option<VideoIndex>,@@ -135,8 +134,7 @@ pub fn extract_motion_video_file_from_path<P: AsRef<Path>, Q: AsRef<Path>>(source.seek(SeekFrom::Start(index.start as u64))?;fs::create_dir_all(destination_directory.as_ref())?;let output = destination_directory.as_ref().join(file_name);- // The output may alias the source. Copy forward before truncating so writes- // at offset zero never overwrite bytes that have not been read yet.+ // Copy forward before truncating: output may alias the source.let mut destination = OpenOptions::new().write(true).create(true)@@ -236,18 +234,6 @@ mod tests {super::get_motion_video_index(&mut Cursor::new(bytes), bytes.len()).expect("read index")}- fn extract_motion_video(- bytes: &[u8],- index: Option<VideoIndex>,- ) -> Result<Vec<u8>, MotionPhotoError> {- let mut reader = Cursor::new(bytes);- let index = resolve_video_index(&mut reader, bytes.len(), index)?;- reader.seek(SeekFrom::Start(index.start as u64))?;- let mut result = Vec::new();- copy_video(&mut reader, &mut result, index.end - index.start)?;- Ok(result)- }-fn make_ftyp_box(brand: [u8; 4]) -> Vec<u8> {let mut buf = Vec::with_capacity(16);buf.extend_from_slice(&16u32.to_be_bytes());@@ -298,10 +284,6 @@ mod tests {let index = get_motion_video_index(&bytes).expect("video index should exist");assert_eq!(index.start, first_start);assert_eq!(index.end, second_start);- let extracted =- extract_motion_video(&bytes, Some(index)).expect("video bytes should extract");- assert_eq!(extracted.len(), second_start - first_start);-let mut bytes2 = b"jpeg-prefix".to_vec();bytes2.extend_from_slice(&make_ftyp_box(*b"mp42"));bytes2.extend_from_slice(&[0xCC; 50]);@@ -413,9 +395,13 @@ mod tests {#[test]fn extracts_video_bytes_for_valid_index() {let bytes = bytes_with_xmp_and_video(20, "GCamera:MotionPhoto=\"1\"");- let video = extract_motion_video(&bytes, None).expect("video bytes should extract");- assert_eq!(video.len(), 20);- assert!(video.iter().all(|byte| *byte == 0xAB));+ let temp = tempdir().unwrap();+ let image = temp.path().join("motion.jpg");+ fs::write(&image, bytes).unwrap();+ let video = extract_motion_video_from_path(&image, None)+ .unwrap()+ .unwrap();+ assert_eq!(video, vec![0xAB; 20]);}#[test]@@ -497,24 +483,32 @@ mod tests {let temp = tempdir().expect("temp dir");let image = temp.path().join("source.jpg");let source: Vec<u8> = (0..BUFFER_SIZE * 3 + 19).map(|i| (i % 251) as u8).collect();- let index = VideoIndex {- start: 13,- end: source.len() - 7,- };+ fs::write(&image, &source).unwrap();+ fs::hard_link(&image, temp.path().join("hard.mp4")).unwrap();+ #[cfg(unix)]+ std::os::unix::fs::symlink(&image, temp.path().join("symbolic.mp4")).unwrap();- for file_name in ["clip.mp4", "source.jpg"] {- fs::write(&image, &source).expect("write source");- fs::write(temp.path().join(file_name), &source).expect("write existing output");- let output = extract_motion_video_file_from_path(- &image,- temp.path(),- file_name,- Some(index.clone()),- )- .expect("extract video")- .expect("video output");-- assert_eq!(fs::read(output).unwrap(), source[index.start..index.end]);+ for file_name in [+ "clip.mp4",+ "source.jpg",+ "hard.mp4",+ #[cfg(unix)]+ "symbolic.mp4",+ ] {+ for start in [0, 13] {+ fs::write(&image, &source).unwrap();+ fs::write(temp.path().join(file_name), &source).unwrap();+ let end = source.len() - 7;+ let output = extract_motion_video_file_from_path(+ &image,+ temp.path(),+ file_name,+ Some(VideoIndex { start, end }),+ )+ .unwrap()+ .unwrap();+ assert_eq!(fs::read(output).unwrap(), source[start..end]);+ }}}@@ -569,48 +563,15 @@ mod tests {);}- #[test]- fn file_extraction_preserves_source_aliases() {- let temp = tempdir().unwrap();- let image = temp.path().join("source.jpg");- let source: Vec<u8> = (0..BUFFER_SIZE * 2 + 9).map(|i| (i % 251) as u8).collect();- fs::write(&image, &source).unwrap();- fs::hard_link(&image, temp.path().join("hard.mp4")).unwrap();- #[cfg(unix)]- std::os::unix::fs::symlink(&image, temp.path().join("symbolic.mp4")).unwrap();-- for output in ["hard.mp4", "symbolic.mp4"] {- if !temp.path().join(output).exists() {- continue;- }- for start in [0, 11] {- fs::write(&image, &source).unwrap();- let end = source.len() - 5;- extract_motion_video_file_from_path(- &image,- temp.path(),- output,- Some(VideoIndex { start, end }),- )- .unwrap();- assert_eq!(fs::read(&image).unwrap(), source[start..end]);- assert_eq!(- fs::read(temp.path().join(output)).unwrap(),- source[start..end]- );- }- }- }-#[test]fn file_extraction_streams_videos_above_the_byte_api_limit() {let temp = tempdir().unwrap();let image = temp.path().join("source.jpg");let length = MAX_VIDEO_BYTES_SIZE + 1;- let mut file = File::create(&image).unwrap();- file.set_len(length as u64 + 1).unwrap();- file.seek(SeekFrom::Start(length as u64)).unwrap();- file.write_all(b"Z").unwrap();+ File::create(&image)+ .unwrap()+ .set_len(length as u64 + 1)+ .unwrap();let index = VideoIndex {start: 1,end: length + 1,@@ -623,12 +584,7 @@ mod tests {extract_motion_video_file_from_path(&image, temp.path(), "clip.mp4", Some(index)).unwrap().unwrap();- let mut result = File::open(output).unwrap();- assert_eq!(result.metadata().unwrap().len(), length as u64);- result.seek(SeekFrom::End(-1)).unwrap();- let mut last = [0];- result.read_exact(&mut last).unwrap();- assert_eq!(last, *b"Z");+ assert_eq!(fs::metadata(output).unwrap().len(), length as u64);}#[test]diff --git a/rust/crates/photos/src/motion_photo/xmp.rs b/rust/crates/photos/src/motion_photo/xmp.rsindex 0abb5be9e3..c597eec33f 100644--- a/rust/crates/photos/src/motion_photo/xmp.rs+++ b/rust/crates/photos/src/motion_photo/xmp.rs@@ -5,7 +5,6 @@ use std::io::{BufRead, BufReader, Read};const XMP_MARKER_BEGIN: &[u8] = b"<x:xmpmeta";const XMP_MARKER_END: &[u8] = b"</x:xmpmeta>";-// Match the input budget of the EXIF reader; the photo itself can be larger.pub(super) const MAX_XMP_SIZE: usize = 8 * 1024 * 1024;pub(super) fn extract_xmp<R: Read>(source: R) -> Result<HashMap<String, String>, MotionPhotoError> {@@ -110,52 +109,34 @@ mod tests {}#[test]- fn recognizes_both_markers_across_chunks() {- let begin = b"<x:xmpmeta>";- let attributes = br#"<rdf:Description GPano:ProjectionType="equirectangular"/>"#;+ fn reads_first_packet_across_chunks() {+ let begin = br#"<x:xmpmeta Item:Length="7">"#;for marker in [XMP_MARKER_BEGIN, XMP_MARKER_END] {for split in 1..marker.len() {let prefix = if marker == XMP_MARKER_BEGIN {BUFFER_SIZE - split} else {- BUFFER_SIZE - split - begin.len() - attributes.len()+ BUFFER_SIZE - split - begin.len()};let source = [vec![0; prefix],begin.to_vec(),- attributes.to_vec(),XMP_MARKER_END.to_vec(),+ br#"<x:xmpmeta Item:Length="8"></x:xmpmeta>"#.to_vec(),].concat();let result = extract_xmp(source.as_slice()).unwrap();- assert_eq!(- result.get("GPano:ProjectionType").unwrap(),- "equirectangular"- );+ assert_eq!(result.get("Item:Length").unwrap(), "7");}}}- #[test]- fn preserves_first_packet_attribute_and_utf8_semantics() {- let mut source = vec![0; BUFFER_SIZE * 2];- source.extend_from_slice(br#"<x:xmpmeta><rdf:Description xmlns:rdf="ignored" xml:lang="ignored" GPano:ProjectionType="old" note="A & B"/><rdf:Description GPano:ProjectionType="cylindrical" invalid=""#);- source.push(0xff);- source.extend_from_slice(br#""/></x:xmpmeta><x:xmpmeta><rdf:Description GPano:ProjectionType="ignored"/></x:xmpmeta>"#);- let data = extract_xmp(source.as_slice()).unwrap();- assert_eq!(data.len(), 3);- assert_eq!(data.get("GPano:ProjectionType").unwrap(), "cylindrical");- assert_eq!(data.get("note").unwrap(), "A & B");- assert_eq!(data.get("invalid").unwrap(), "\u{fffd}");- }-#[test]fn bounds_metadata_without_limiting_photo_size() {let begin = b"<x:xmpmeta>";let mut xml = begin.to_vec();xml.resize(MAX_XMP_SIZE - XMP_MARKER_END.len(), b' ');xml.extend_from_slice(XMP_MARKER_END);- // Trailing image data does not count toward the metadata budget.xml.extend_from_slice(&[0; 32]);assert!(extract_xmp(xml.as_slice()).unwrap().is_empty());xml.insert(begin.len(), b' ');diff --git a/rust/crates/photos/tests/motion_photos_assets_test.rs b/rust/crates/photos/tests/motion_photos_assets_test.rsindex c304a9ada7..9d19402713 100644--- a/rust/crates/photos/tests/motion_photos_assets_test.rs+++ b/rust/crates/photos/tests/motion_photos_assets_test.rs@@ -4,7 +4,6 @@ use ente_photos::{};use std::path::{Path, PathBuf};-// External fixtures live beside the repository unless overridden.fn fixture_dir() -> Option<PathBuf> {if let Some(path) = std::env::var_os("ENTE_TEST_FIXTURES_DIR") {return Some(PathBuf::from(path).join("media/motion-photos/v1/files"));