Skip to content

Commit 31c0b2c

Browse files
committed
Normalize relative paths in RecordingMeta
1 parent dc2afb3 commit 31c0b2c

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

crates/project/src/meta.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl RecordingMeta {
136136
let meta_path = project_path.join("recording-meta.json");
137137
let mut meta: Self = serde_json::from_str(&std::fs::read_to_string(&meta_path)?)?;
138138
meta.project_path = project_path.to_path_buf();
139+
meta.normalize_paths();
139140

140141
Ok(meta)
141142
}
@@ -184,6 +185,72 @@ impl RecordingMeta {
184185
_ => None,
185186
}
186187
}
188+
189+
fn normalize_paths(&mut self) {
190+
let normalize_video = |meta: &mut VideoMeta| normalize_relative_path(&mut meta.path);
191+
let normalize_audio = |meta: &mut AudioMeta| normalize_relative_path(&mut meta.path);
192+
let normalize_cursor = |path: &mut Option<RelativePathBuf>| {
193+
if let Some(path) = path {
194+
normalize_relative_path(path);
195+
}
196+
};
197+
198+
match &mut self.inner {
199+
RecordingMetaInner::Studio(meta) => match meta.as_mut() {
200+
StudioRecordingMeta::SingleSegment { segment } => {
201+
normalize_video(&mut segment.display);
202+
if let Some(camera) = &mut segment.camera {
203+
normalize_video(camera);
204+
}
205+
if let Some(audio) = &mut segment.audio {
206+
normalize_audio(audio);
207+
}
208+
normalize_cursor(&mut segment.cursor);
209+
}
210+
StudioRecordingMeta::MultipleSegments { inner } => {
211+
for segment in &mut inner.segments {
212+
normalize_video(&mut segment.display);
213+
if let Some(camera) = &mut segment.camera {
214+
normalize_video(camera);
215+
}
216+
if let Some(mic) = &mut segment.mic {
217+
normalize_audio(mic);
218+
}
219+
if let Some(system_audio) = &mut segment.system_audio {
220+
normalize_audio(system_audio);
221+
}
222+
normalize_cursor(&mut segment.cursor);
223+
}
224+
225+
if let Cursors::Correct(cursors) = &mut inner.cursors {
226+
for cursor in cursors.values_mut() {
227+
normalize_relative_path(&mut cursor.image_path);
228+
}
229+
}
230+
}
231+
},
232+
RecordingMetaInner::Instant(_) => {}
233+
}
234+
}
235+
}
236+
237+
fn normalize_relative_path(path: &mut RelativePathBuf) {
238+
let original = path.as_str();
239+
let normalized = original.replace('\\', "/");
240+
241+
if normalized.starts_with("content/")
242+
|| normalized.starts_with("screenshots/")
243+
|| normalized.starts_with("output/")
244+
{
245+
return;
246+
}
247+
248+
for root in ["content/", "screenshots/", "output/"] {
249+
if let Some(index) = normalized.find(root) {
250+
*path = RelativePathBuf::from(&normalized[index..]);
251+
return;
252+
}
253+
}
187254
}
188255

189256
#[derive(Debug, Clone, Serialize, Deserialize, Type)]

0 commit comments

Comments
 (0)