keep temp clean and previews alive across a capture

This commit is contained in:
2026-09-21 01:04:22 +03:00
parent 03b2fec744
commit 1ace31fc6e
3 changed files with 75 additions and 11 deletions
+52 -8
View File
@@ -317,13 +317,21 @@ fn audio_segment_path(app_dir: &Path, idx: usize) -> PathBuf {
temp_dir(app_dir).join(format!("audio-{idx:03}.pcm"))
}
fn clear_temp_segments(app_dir: &Path) {
/// Remove temporary recording artifacts (segments, previews, thumbs).
///
/// `keep_previews` is set when a capture starts mid-session: the frontend can
/// still be holding a live `convertFileSrc` URL for preview.png or
/// raw-preview.png from the previous clip, and deleting them under it made the
/// asset protocol log "file does not exist" and briefly broke the preview.
/// Startup and exit sweep everything.
fn clear_temp_segments(app_dir: &Path, keep_previews: bool) {
let temp = temp_dir(app_dir);
if let Ok(entries) = std::fs::read_dir(&temp) {
for entry in entries.flatten() {
if let Some(name) = entry.file_name().to_str() {
if (name.starts_with("segment-") && name.ends_with(".mp4"))
|| (name.starts_with("audio-") && name.ends_with(".pcm"))
|| name.ends_with(".pcm.padtmp")
|| (name.starts_with("preview-palette-") && name.ends_with(".png"))
{
drop(std::fs::remove_file(entry.path()));
@@ -338,8 +346,10 @@ fn clear_temp_segments(app_dir: &Path) {
if thumbs.is_dir() {
drop(std::fs::remove_dir_all(&thumbs));
}
drop(std::fs::remove_file(temp.join("preview.png")));
drop(std::fs::remove_file(temp.join("raw-preview.png")));
if !keep_previews {
drop(std::fs::remove_file(temp.join("preview.png")));
drop(std::fs::remove_file(temp.join("raw-preview.png")));
}
}
/// Pad or trim a raw f32le PCM file so its duration matches `target_secs`.
@@ -499,7 +509,7 @@ fn start_capture(
let cfg = lock(&state.config).clone();
let temp = temp_dir(&state.app_dir);
std::fs::create_dir_all(&temp).map_err(|e| format!("creating temp dir: {e:#}"))?;
clear_temp_segments(&state.app_dir);
clear_temp_segments(&state.app_dir, true);
let crop = request.as_ref().and_then(|r| r.crop);
let fps = request.as_ref().and_then(|r| r.fps).unwrap_or(cfg.capture.default_fps);
@@ -1029,9 +1039,43 @@ mod tests {
}
}
/// The pcm head-padding step writes a sibling `*.pcm.padtmp` next to the
/// concatenated audio. It used to fall outside every sweep pattern, so a
/// crash mid-pad left a file the size of the whole audio track in temp/.
#[test]
fn estimate_balanced_matches_gif_params() {
// The frontend uses estimate_gif_params for the on-screen dims label.
fn sweep_removes_padtmp_but_spares_previews_mid_session() {
let dir = std::env::temp_dir().join(format!("bytesize-sweep-test-{}", std::process::id()));
let temp = dir.join("temp");
drop(std::fs::remove_dir_all(&dir));
std::fs::create_dir_all(&temp).unwrap();
for name in [
"audio-concat.pcm.padtmp",
"segment-001.mp4",
"audio-001.pcm",
"preview.png",
"raw-preview.png",
] {
std::fs::write(temp.join(name), b"x").unwrap();
}
// Mid-session: drop the recording artifacts, keep the live previews.
clear_temp_segments(&dir, true);
assert!(!temp.join("audio-concat.pcm.padtmp").exists(), "padtmp should be swept");
assert!(!temp.join("segment-001.mp4").exists());
assert!(!temp.join("audio-001.pcm").exists());
assert!(temp.join("preview.png").exists(), "preview must survive a new capture");
assert!(temp.join("raw-preview.png").exists());
// Exit/startup: everything goes.
clear_temp_segments(&dir, false);
assert!(!temp.join("preview.png").exists());
assert!(!temp.join("raw-preview.png").exists());
drop(std::fs::remove_dir_all(&dir));
}
#[test]
fn estimate_balanced_matches_gif_params() { // The frontend uses estimate_gif_params for the on-screen dims label.
// The backend uses gif_params for the actual encode. The two must agree.
let req = make_request(8 * 1024 * 1024, 30, GifBudgetMode::Balanced);
let est = estimate_gif_params(req);
@@ -1118,7 +1162,7 @@ fn cleanup_temp_on_exit(app: &AppHandle) {
Some(s) => s,
None => return,
};
clear_temp_segments(&st.app_dir);
clear_temp_segments(&st.app_dir, false);
}
fn start_overlay_clickthrough_poll(
@@ -1905,7 +1949,7 @@ pub fn run() {
// Sweep temp/ before anything else so a crash on the previous run does not
// leave orphaned segments, previews, or thumbs behind - the portability
// spec promises no trace, and stale temp is trace.
clear_temp_segments(&app_dir);
clear_temp_segments(&app_dir, false);
let log_dir = app_dir.join("logs");
drop(std::fs::create_dir_all(&log_dir));