Skip to content

armgit / ente

Public Imported from GitHub
0

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

fix(photos): pick last embedded mp4 header for mp extraction

Neeraj Gupta1e3f6c737ae1
diff --git a/rust/photos/src/lib.rs b/rust/photos/src/lib.rs
index 1e7135c0bb..06b9587222 100644
--- a/rust/photos/src/lib.rs
+++ b/rust/photos/src/lib.rs
@@ -54,7 +54,7 @@ pub fn get_motion_video_index_from_path<P: AsRef<Path>>(
}
fn get_motion_video_index(bytes: &[u8]) -> Option<VideoIndex> {
- if let Some(start) = find_subslice(bytes, &MP4_HEADER_PATTERN) {
+ if let Some(start) = find_last_subslice(bytes, &MP4_HEADER_PATTERN) {
return Some(VideoIndex {
start,
end: bytes.len(),
@@ -219,6 +219,15 @@ fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
.position(|window| window == needle)
}
+fn find_last_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
+ if needle.is_empty() {
+ return Some(haystack.len());
+ }
+ haystack
+ .windows(needle.len())
+ .rposition(|window| window == needle)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -252,6 +261,20 @@ mod tests {
assert_eq!(index.end, bytes.len());
}
+ #[test]
+ fn prefers_last_mp4_header_when_multiple_exist() {
+ let mut bytes = b"jpeg-prefix".to_vec();
+ bytes.extend_from_slice(&MP4_HEADER_PATTERN);
+ bytes.extend_from_slice(b"autoplay-segment");
+ let second_start = bytes.len();
+ bytes.extend_from_slice(&MP4_HEADER_PATTERN);
+ bytes.extend_from_slice(b"motion-video-segment");
+
+ let index = get_motion_video_index(&bytes).expect("video index should exist");
+ assert_eq!(index.start, second_start);
+ assert_eq!(index.end, bytes.len());
+ }
+
#[test]
fn finds_xmp_offset_for_jpeg_like_file() {
let bytes = bytes_with_xmp_and_video(24, "GCamera:MotionPhoto=\"1\"");