app: keep a renamed clip's game after the rename
The card that leads back to a clip's game found the recording by the id
the export writes into the file's name, and a title change takes that
name away. Saving a title now writes a small link file beside the clip,
`{name}.json`, naming the recording, and carries an existing one along,
so the card survives the rename, leaving the clip view, and a restart.
The clips folder reads the link back, which also keeps the champion on
a grid card. Deleting a clip deletes its link file too.
This commit is contained in:
1 parent
64b172649b
commit
c452fab735
4 files changed
+132
-18
No files matched your search
@@ -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<PathBuf>,
|
||||
/// 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<String>,
|
||||
}
|
||||
|
||||
/// Where the clips' miniatures live, beside the clips.
|
||||
@@ -146,6 +149,7 @@ pub fn list_clips() -> Vec<Clip> {
|
||||
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<Clip> {
|
||||
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<String> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user