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:
vhaudiquet committed 2026-11-23 20:53:09 +01:00
1 parent 64b172649b
commit c452fab735
4 files changed
+132 -18

No files matched your search

+1
View File
@@ -264,6 +264,7 @@ impl App {
name, name,
path: PathBuf::new(), path: PathBuf::new(),
thumb: None, thumb: None,
recording_id: None,
exported: Some( exported: Some(
std::time::SystemTime::now() std::time::SystemTime::now()
- chrono::Duration::hours(hours_ago) - chrono::Duration::hours(hours_ago)
+45
View File
@@ -30,6 +30,9 @@ pub struct Clip {
pub bytes: u64, pub bytes: u64,
/// The miniature cut from the video, when one is on disk and as new as the clip. /// The miniature cut from the video, when one is on disk and as new as the clip.
pub thumb: Option<PathBuf>, 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. /// Where the clips' miniatures live, beside the clips.
@@ -146,6 +149,7 @@ pub fn list_clips() -> Vec<Clip> {
exported: meta.modified().ok(), exported: meta.modified().ok(),
bytes: meta.len(), bytes: meta.len(),
thumb: None, thumb: None,
recording_id: recording_link(&path),
}; };
clip.thumb = thumb_of(&clip); clip.thumb = thumb_of(&clip);
Some(clip) Some(clip)
@@ -155,6 +159,28 @@ pub fn list_clips() -> Vec<Clip> {
clips 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. /// The clips folder beside the recordings.
pub fn clips_dir() -> PathBuf { pub fn clips_dir() -> PathBuf {
let dir = daemon::recordings_dir().join("clips"); let dir = daemon::recordings_dir().join("clips");
@@ -437,3 +463,22 @@ fn sanitize(name: &str) -> String {
}) })
.collect() .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);
}
}
+72 -12
View File
@@ -3,6 +3,7 @@
//! The grid opens it on a card, and an export opens it when the clip lands. The title is //! 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. //! the file's name: saving renames the clip, and the grid follows.
use std::path::Path;
use std::time::Duration; use std::time::Duration;
use rune::tok::{radius, space, type_}; use rune::tok::{radius, space, type_};
@@ -65,7 +66,7 @@ pub fn open(app: &mut App, name: &str) {
app.clip = Some(ClipView { app.clip = Some(ClipView {
name: clip.name.clone(), name: clip.name.clone(),
title: TextEdit::new(clip.name.clone()), title: TextEdit::new(clip.name.clone()),
game: recording_of(app, &clip.name), game: recording_of(app, &clip),
player: Some(player), player: Some(player),
error: None, error: None,
muted: false, muted: false,
@@ -78,17 +79,30 @@ pub fn open(app: &mut App, name: &str) {
/// The recording a clip was cut from. /// 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 /// A clip the app renamed carries a file naming its recording; one still named as the export
/// the id names the timeline. The row comes from the loaded history when it is there, and from /// left it carries the id in the name. Either way the row comes from the loaded history when it
/// the timeline itself when the history has not paged that far, so an old clip still leads back /// is there, and from the timeline itself when the history has not paged that far, so an old
/// to its game. /// clip still leads back to its game.
fn recording_of(app: &App, clip_name: &str) -> Option<Summary> { fn recording_of(app: &App, clip: &Clip) -> Option<Summary> {
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<String> {
if let Some(game) = app if let Some(game) = app
.games .games
.iter() .iter()
.find(|game| clip_name.contains(&game.recording_id)) .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() { let paths = if app.games_paths.is_empty() {
history::timeline_paths() history::timeline_paths()
@@ -98,12 +112,11 @@ fn recording_of(app: &App, clip_name: &str) -> Option<Summary> {
let ids = paths let ids = paths
.iter() .iter()
.filter_map(|path| path.file_stem().and_then(|stem| stem.to_str())); .filter_map(|path| path.file_stem().and_then(|stem| stem.to_str()));
let id = recording_id(clip_name, ids)?; recording_id(clip_name, ids).map(str::to_string)
history::load(id).map(|game| game.summary())
} }
/// The recording a clip's file name carries. The export writes the id into the name, so the id /// The recording a name carries, from ids the caller has. A substring check: the export appends
/// is a substring of it; a clip renamed away from its export name carries none. /// to the id, and no two ids share a prefix.
fn recording_id<'a>(clip_name: &str, ids: impl Iterator<Item = &'a str>) -> Option<&'a str> { fn recording_id<'a>(clip_name: &str, ids: impl Iterator<Item = &'a str>) -> Option<&'a str> {
ids.into_iter().find(|id| clip_name.contains(id)) 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 (from_thumb, to_thumb) = (export::thumb_path(&clip), export::thumb_path(&renamed));
let _ = std::fs::rename(from_thumb, &to_thumb); 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]; let entry = &mut app.clips[index];
entry.name = wanted.clone(); entry.name = wanted.clone();
entry.path = to; entry.path = to;
entry.thumb = to_thumb.exists().then_some(to_thumb); entry.thumb = to_thumb.exists().then_some(to_thumb);
entry.recording_id = recording;
let view = app.clip.as_mut().expect("checked above"); let view = app.clip.as_mut().expect("checked above");
view.name = wanted; view.name = wanted;
view.title = TextEdit::new(view.name.clone()); view.title = TextEdit::new(view.name.clone());
@@ -204,6 +226,16 @@ pub fn save_title(app: &mut App) {
app.spawn_thumbs(); 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 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. /// the edges lose their stray spaces and dots, and the whole stays short enough to show.
fn title_name(title: &str) -> String { fn title_name(title: &str) -> String {
@@ -752,7 +784,35 @@ fn seek(app: &mut App, seconds: f64) {
#[cfg(test)] #[cfg(test)]
mod tests { 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] #[test]
fn a_clip_name_carries_the_recording_it_was_cut_from() { fn a_clip_name_carries_the_recording_it_was_cut_from() {
+14 -6
View File
@@ -127,13 +127,18 @@ struct Card {
impl Card { impl Card {
/// The card for one clip. The game the clip was cut from, when its recording is in the /// 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 /// loaded history, names the card; otherwise the file does. The recording comes from the
/// recording's id somewhere in them, so a match is a contains check away. /// clip's link file, or from the export name, which carries the id.
fn of(app: &App, clip: &Clip, index: usize) -> Card { fn of(app: &App, clip: &Clip, index: usize) -> Card {
let game = app let game = clip
.games .recording_id
.iter() .as_deref()
.find(|game| clip.name.contains(&game.recording_id)); .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); let when = format::age(clip.exported);
match game { match game {
Some(game) => Card { Some(game) => Card {
@@ -353,6 +358,9 @@ fn delete_confirmed(app: &mut App) {
}; };
let clip = app.clips.remove(index); let clip = app.clips.remove(index);
let _ = std::fs::remove_file(crate::export::thumb_path(&clip)); 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) { match std::fs::remove_file(&clip.path) {
Ok(()) => app.notify("Clip deleted"), Ok(()) => app.notify("Clip deleted"),
Err(error) => { Err(error) => {