keep temp clean and previews alive across a capture
This commit is contained in:
@@ -287,6 +287,16 @@ fn is_ffmpeg_entry(name: &str) -> bool {
|
||||
.is_some_and(|leaf| leaf.eq_ignore_ascii_case("ffmpeg.exe"))
|
||||
}
|
||||
|
||||
/// Removes a scratch directory on drop so extraction failures don't leave an
|
||||
/// unpacked ffmpeg build sitting in temp/.
|
||||
struct DirGuard(PathBuf);
|
||||
|
||||
impl Drop for DirGuard {
|
||||
fn drop(&mut self) {
|
||||
drop(fs::remove_dir_all(&self.0));
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_ffmpeg_from_7z(archive_path: &Path, dest: &Path) -> Result<()> {
|
||||
let app_dir = dest
|
||||
.parent()
|
||||
@@ -295,13 +305,16 @@ fn extract_ffmpeg_from_7z(archive_path: &Path, dest: &Path) -> Result<()> {
|
||||
let scratch = app_dir.join("temp").join("ffmpeg-extract");
|
||||
drop(fs::remove_dir_all(&scratch));
|
||||
fs::create_dir_all(&scratch).context("create 7z scratch dir")?;
|
||||
// Removes the extracted tree on every exit path, including the extraction
|
||||
// failure below - the older unconditional remove sat before the `?`, so a
|
||||
// failed extract left the whole unpacked build behind in temp/.
|
||||
let _guard = DirGuard(scratch.clone());
|
||||
|
||||
let file = File::open(archive_path).context("opening 7z archive")?;
|
||||
let extract_result =
|
||||
sevenz_rust2::decompress_with_extract_fn(file, &scratch, |_, _, _| Ok(true))
|
||||
.map_err(|e| anyhow!("7z extraction failed: {e}"));
|
||||
let found = find_ffmpeg_in(&scratch);
|
||||
drop(fs::remove_dir_all(&scratch));
|
||||
|
||||
extract_result?;
|
||||
let Some(src) = found else {
|
||||
|
||||
@@ -874,8 +874,15 @@ pub(crate) fn render_gif_preview(
|
||||
let sample_start = (midpoint_secs - 1.0).max(0.0);
|
||||
let sample_dur = duration_secs.min(2.0);
|
||||
let palette_key = palette_cache_key(gif_colors, target_width, sample_start, sample_dur);
|
||||
let palette_path = out_path.parent().unwrap_or(input.parent().unwrap())
|
||||
.join(format!("preview-palette-{palette_key}.png"));
|
||||
// The preview and its input both live in the scratch dir, so the preview's
|
||||
// parent is the normal case. Fall back to the input's parent rather than
|
||||
// panicking on a bare filename.
|
||||
let palette_dir = out_path
|
||||
.parent()
|
||||
.filter(|p| !p.as_os_str().is_empty())
|
||||
.or_else(|| input.parent().filter(|p| !p.as_os_str().is_empty()))
|
||||
.context("preview palette has no parent directory")?;
|
||||
let palette_path = palette_dir.join(format!("preview-palette-{palette_key}.png"));
|
||||
|
||||
let need_regen = !palette_path.is_file();
|
||||
if need_regen {
|
||||
|
||||
+50
-6
@@ -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));
|
||||
}
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user