Skip to content

armgit / ente

Public Imported from GitHub
0

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

Avoid copying motion videos before writing files

laurenspriemae301f2e2379
diff --git a/rust/crates/photos/src/motion_photo/mod.rs b/rust/crates/photos/src/motion_photo/mod.rs
index 81bc3d11d8..15724227a1 100644
--- a/rust/crates/photos/src/motion_photo/mod.rs
+++ b/rust/crates/photos/src/motion_photo/mod.rs
@@ -67,13 +67,13 @@ pub fn extract_motion_video_from_path<P: AsRef<Path>>(
index: Option<VideoIndex>,
) -> Result<Option<Vec<u8>>, MotionPhotoError> {
let bytes = fs::read(file_path)?;
- extract_motion_video(&bytes, index).map(Some)
+ extract_motion_video(&bytes, index).map(|video| Some(video.to_vec()))
}
fn extract_motion_video(
bytes: &[u8],
index: Option<VideoIndex>,
-) -> Result<Vec<u8>, MotionPhotoError> {
+) -> Result<&[u8], MotionPhotoError> {
let video_index = index.or_else(|| get_motion_video_index(bytes));
let Some(video_index) = video_index else {
return Err(MotionPhotoError::VideoNotFound);
@@ -83,7 +83,7 @@ fn extract_motion_video(
return Err(MotionPhotoError::InvalidIndex);
}
- Ok(bytes[video_index.start..video_index.end].to_vec())
+ Ok(&bytes[video_index.start..video_index.end])
}
pub fn extract_motion_video_file_from_path<P: AsRef<Path>, Q: AsRef<Path>>(
@@ -94,9 +94,8 @@ pub fn extract_motion_video_file_from_path<P: AsRef<Path>, Q: AsRef<Path>>(
) -> Result<Option<PathBuf>, MotionPhotoError> {
validate_output_file_name(file_name)?;
- let Some(video) = extract_motion_video_from_path(file_path, index)? else {
- return Ok(None);
- };
+ let bytes = fs::read(file_path)?;
+ let video = extract_motion_video(&bytes, index)?;
fs::create_dir_all(destination_directory.as_ref())?;
let output = destination_directory.as_ref().join(file_name);
fs::write(&output, video)?;
@@ -387,6 +386,55 @@ mod tests {
assert!(!output_dir.join("nested").exists());
}
+ #[test]
+ fn file_extraction_rejects_invalid_input_before_creating_output() {
+ let temp = tempdir().expect("temp dir");
+ let image = temp.path().join("source.jpg");
+ let output_dir = temp.path().join("out");
+ fs::write(&image, b"still image").expect("write source");
+
+ for index in [
+ VideoIndex { start: 0, end: 0 },
+ VideoIndex { start: 5, end: 3 },
+ VideoIndex { start: 0, end: 12 },
+ ] {
+ assert!(matches!(
+ extract_motion_video_file_from_path(&image, &output_dir, "clip.mp4", Some(index)),
+ Err(MotionPhotoError::InvalidIndex)
+ ));
+ assert!(!output_dir.exists());
+ }
+
+ assert!(matches!(
+ extract_motion_video_file_from_path(&image, &output_dir, "clip.mp4", None),
+ Err(MotionPhotoError::VideoNotFound)
+ ));
+ assert!(!output_dir.exists());
+ }
+
+ #[test]
+ fn file_extraction_preserves_selected_bytes_when_overwriting() {
+ let temp = tempdir().expect("temp dir");
+ let image = temp.path().join("source.jpg");
+ let source = b"image-prefix-video-data-trailing-preview";
+ let index = VideoIndex { start: 13, end: 23 };
+
+ 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]);
+ }
+ }
+
#[test]
fn extracts_xmp_attributes() {
let bytes = bytes_with_xmp_and_video(
diff --git a/rust/crates/photos/tests/motion_photos_assets_test.rs b/rust/crates/photos/tests/motion_photos_assets_test.rs
index 81854a23c4..c5cb106c74 100644
--- a/rust/crates/photos/tests/motion_photos_assets_test.rs
+++ b/rust/crates/photos/tests/motion_photos_assets_test.rs
@@ -1,4 +1,7 @@
-use ente_photos::{extract_motion_video_from_path, get_motion_video_index_from_path};
+use ente_photos::{
+ extract_motion_video_file_from_path, extract_motion_video_from_path,
+ get_motion_video_index_from_path,
+};
use std::path::{Path, PathBuf};
// External fixtures live beside the repository unless overridden.
@@ -112,3 +115,42 @@ fn validates_known_motion_photo_indices_when_fixtures_present() {
.expect("video present");
assert!(dual_first_video.len() > 1_000_000);
}
+
+#[test]
+fn file_extraction_matches_video_bytes_when_fixtures_present() {
+ let output_directory = tempfile::tempdir().expect("output directory");
+ for name in [
+ "motionphoto.jpg",
+ "motionphoto.heic",
+ "pixel_6_small_video.jpg",
+ "dual_mp4_video_last.jpg",
+ "dual_mp4_video_first.jpg",
+ ] {
+ let Some(path) = fixture(name) else {
+ eprintln!("Skipping: external fixture {name} not present");
+ return;
+ };
+ let source = std::fs::read(&path).expect("read fixture");
+ let index = get_motion_video_index_from_path(&path)
+ .expect("find video")
+ .expect("video present");
+ let expected = &source[index.start..index.end];
+
+ for supplied_index in [None, Some(index)] {
+ let video = extract_motion_video_from_path(&path, supplied_index.clone())
+ .expect("extract bytes")
+ .expect("video present");
+ let output = extract_motion_video_file_from_path(
+ &path,
+ output_directory.path(),
+ "clip.mp4",
+ supplied_index,
+ )
+ .expect("extract file")
+ .expect("video file present");
+
+ assert_eq!(video, expected, "{name}");
+ assert_eq!(std::fs::read(output).unwrap(), expected, "{name}");
+ }
+ }
+}