diff --git a/app/src/app.rs b/app/src/app.rs index 13aaa90..b5621f7 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -264,6 +264,7 @@ impl App { name, path: PathBuf::new(), thumb: None, + recording_id: None, exported: Some( std::time::SystemTime::now() - chrono::Duration::hours(hours_ago) diff --git a/app/src/export.rs b/app/src/export.rs index 6b4b47a..486db1d 100644 --- a/app/src/export.rs +++ b/app/src/export.rs @@ -30,6 +30,9 @@ pub struct Clip { pub bytes: u64, /// The miniature cut from the video, when one is on disk and as new as the clip. pub thumb: Option, + /// The recording the clip was cut from, from the file a rename leaves beside it. A clip + /// still named as it was exported carries the id in its name instead. + pub recording_id: Option, } /// Where the clips' miniatures live, beside the clips. @@ -146,6 +149,7 @@ pub fn list_clips() -> Vec { exported: meta.modified().ok(), bytes: meta.len(), thumb: None, + recording_id: recording_link(&path), }; clip.thumb = thumb_of(&clip); Some(clip) @@ -155,6 +159,28 @@ pub fn list_clips() -> Vec { clips } +/// The file beside a clip that names the recording it was cut from. +/// +/// The export writes the recording's id into the clip's name, and the name is the user's to +/// change. A clip renamed in the app carries this file instead, so a screen that leads back to +/// the game still finds it. +pub fn link_path(clip: &Path) -> PathBuf { + clip.with_extension("json") +} + +/// Name the recording beside `clip`. True when the file landed. +pub fn link_recording(clip: &Path, recording_id: &str) -> bool { + let text = serde_json::json!({ "recording_id": recording_id }).to_string(); + std::fs::write(link_path(clip), text).is_ok() +} + +/// The recording `clip`'s link file names, when one sits beside it. +pub fn recording_link(clip: &Path) -> Option { + let text = std::fs::read_to_string(link_path(clip)).ok()?; + let value: serde_json::Value = serde_json::from_str(&text).ok()?; + value.get("recording_id")?.as_str().map(str::to_string) +} + /// The clips folder beside the recordings. pub fn clips_dir() -> PathBuf { let dir = daemon::recordings_dir().join("clips"); @@ -437,3 +463,22 @@ fn sanitize(name: &str) -> String { }) .collect() } + +#[cfg(test)] +mod tests { + use super::{link_path, link_recording, recording_link}; + use std::path::Path; + + #[test] + fn a_link_file_names_the_recording_beside_its_clip() { + let dir = std::env::temp_dir().join(format!("leaguerecorder-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&dir).unwrap(); + let clip = dir.join("My Ace.mp4"); + std::fs::write(&clip, b"").unwrap(); + assert_eq!(link_path(&clip), Path::new(&dir).join("My Ace.json")); + assert_eq!(recording_link(&clip), None); + assert!(link_recording(&clip, "demo-1")); + assert_eq!(recording_link(&clip).as_deref(), Some("demo-1")); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/app/src/screens/clip.rs b/app/src/screens/clip.rs index a1c2964..f0487b0 100644 --- a/app/src/screens/clip.rs +++ b/app/src/screens/clip.rs @@ -3,6 +3,7 @@ //! The grid opens it on a card, and an export opens it when the clip lands. The title is //! the file's name: saving renames the clip, and the grid follows. +use std::path::Path; use std::time::Duration; use rune::tok::{radius, space, type_}; @@ -65,7 +66,7 @@ pub fn open(app: &mut App, name: &str) { app.clip = Some(ClipView { name: clip.name.clone(), title: TextEdit::new(clip.name.clone()), - game: recording_of(app, &clip.name), + game: recording_of(app, &clip), player: Some(player), error: None, muted: false, @@ -78,17 +79,30 @@ pub fn open(app: &mut App, name: &str) { /// The recording a clip was cut from. /// -/// The export puts the recording's id in the file's name, so the id is in the clip's name, and -/// the id names the timeline. The row comes from the loaded history when it is there, and from -/// the timeline itself when the history has not paged that far, so an old clip still leads back -/// to its game. -fn recording_of(app: &App, clip_name: &str) -> Option { +/// A clip the app renamed carries a file naming its recording; one still named as the export +/// left it carries the id in the name. Either way the row comes from the loaded history when it +/// is there, and from the timeline itself when the history has not paged that far, so an old +/// clip still leads back to its game. +fn recording_of(app: &App, clip: &Clip) -> Option { + let id = match &clip.recording_id { + Some(id) => id.clone(), + None => recording_id_in_export_name(app, &clip.name)?, + }; + if let Some(game) = app.games.iter().find(|game| game.recording_id == id) { + return Some(game.clone()); + } + history::load(&id).map(|game| game.summary()) +} + +/// The recording a clip's export name carries. The export writes the id into the name, so the +/// id is a substring of it; a clip renamed away from that name carries a link file instead. +fn recording_id_in_export_name(app: &App, clip_name: &str) -> Option { if let Some(game) = app .games .iter() .find(|game| clip_name.contains(&game.recording_id)) { - return Some(game.clone()); + return Some(game.recording_id.clone()); } let paths = if app.games_paths.is_empty() { history::timeline_paths() @@ -98,12 +112,11 @@ fn recording_of(app: &App, clip_name: &str) -> Option { let ids = paths .iter() .filter_map(|path| path.file_stem().and_then(|stem| stem.to_str())); - let id = recording_id(clip_name, ids)?; - history::load(id).map(|game| game.summary()) + recording_id(clip_name, ids).map(str::to_string) } -/// The recording a clip's file name carries. The export writes the id into the name, so the id -/// is a substring of it; a clip renamed away from its export name carries none. +/// The recording a name carries, from ids the caller has. A substring check: the export appends +/// to the id, and no two ids share a prefix. fn recording_id<'a>(clip_name: &str, ids: impl Iterator) -> Option<&'a str> { ids.into_iter().find(|id| clip_name.contains(id)) } @@ -193,10 +206,19 @@ pub fn save_title(app: &mut App) { }; let (from_thumb, to_thumb) = (export::thumb_path(&clip), export::thumb_path(&renamed)); let _ = std::fs::rename(from_thumb, &to_thumb); + // The link to the recording follows the file: a new name no longer names it. + let recording = app + .clip + .as_ref() + .and_then(|view| view.game.as_ref()) + .map(|game| game.recording_id.clone()) + .or_else(|| clip.recording_id.clone()); + keep_link(&clip.path, &to, recording.as_deref()); let entry = &mut app.clips[index]; entry.name = wanted.clone(); entry.path = to; entry.thumb = to_thumb.exists().then_some(to_thumb); + entry.recording_id = recording; let view = app.clip.as_mut().expect("checked above"); view.name = wanted; view.title = TextEdit::new(view.name.clone()); @@ -204,6 +226,16 @@ pub fn save_title(app: &mut App) { app.spawn_thumbs(); } +/// Keep a clip's link to its recording through a move. One already beside the old file moves +/// with it; a clip that has none gets one from `recording`, while the view still knows it. +fn keep_link(from: &Path, to: &Path, recording: Option<&str>) { + if std::fs::rename(export::link_path(from), export::link_path(to)).is_err() + && let Some(id) = recording + { + export::link_recording(to, id); + } +} + /// The file name for a typed title. Characters a Windows file cannot carry become dashes, /// the edges lose their stray spaces and dots, and the whole stays short enough to show. fn title_name(title: &str) -> String { @@ -752,7 +784,35 @@ fn seek(app: &mut App, seconds: f64) { #[cfg(test)] mod tests { - use super::{recording_id, title_name}; + use super::{keep_link, recording_id, title_name}; + use crate::export::{link_path, link_recording, recording_link}; + + #[test] + fn a_renamed_clip_keeps_the_link_to_its_recording() { + let dir = std::env::temp_dir().join(format!("leaguerecorder-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&dir).unwrap(); + let from = dir.join("demo-1-clip.mp4"); + let to = dir.join("My Ace.mp4"); + std::fs::write(&from, b"").unwrap(); + std::fs::write(&to, b"").unwrap(); + + // A link from an earlier rename moves with the file. + link_recording(&from, "demo-1"); + keep_link(&from, &to, Some("ignored")); + assert_eq!(recording_link(&to).as_deref(), Some("demo-1")); + + // A clip exported before links existed gets one from the game the view knows. + std::fs::remove_file(link_path(&to)).unwrap(); + keep_link(&from, &to, Some("demo-2")); + assert_eq!(recording_link(&to).as_deref(), Some("demo-2")); + + // With no game known there is nothing to write. + std::fs::remove_file(link_path(&to)).unwrap(); + keep_link(&from, &to, None); + assert_eq!(recording_link(&to), None); + + let _ = std::fs::remove_dir_all(&dir); + } #[test] fn a_clip_name_carries_the_recording_it_was_cut_from() { diff --git a/app/src/screens/clips.rs b/app/src/screens/clips.rs index 5bd8b33..3eceb6f 100644 --- a/app/src/screens/clips.rs +++ b/app/src/screens/clips.rs @@ -127,13 +127,18 @@ struct Card { impl Card { /// The card for one clip. The game the clip was cut from, when its recording is in the - /// loaded history, names the card; otherwise the file does. Export names carry the - /// recording's id somewhere in them, so a match is a contains check away. + /// loaded history, names the card; otherwise the file does. The recording comes from the + /// clip's link file, or from the export name, which carries the id. fn of(app: &App, clip: &Clip, index: usize) -> Card { - let game = app - .games - .iter() - .find(|game| clip.name.contains(&game.recording_id)); + let game = clip + .recording_id + .as_deref() + .and_then(|id| app.games.iter().find(|game| game.recording_id == id)) + .or_else(|| { + app.games + .iter() + .find(|game| clip.name.contains(&game.recording_id)) + }); let when = format::age(clip.exported); match game { Some(game) => Card { @@ -353,6 +358,9 @@ fn delete_confirmed(app: &mut App) { }; let clip = app.clips.remove(index); let _ = std::fs::remove_file(crate::export::thumb_path(&clip)); + if !clip.path.as_os_str().is_empty() { + let _ = std::fs::remove_file(crate::export::link_path(&clip.path)); + } match std::fs::remove_file(&clip.path) { Ok(()) => app.notify("Clip deleted"), Err(error) => {