From de1318deb6a1f73ae8921d7bf6ecb317c7616a4f Mon Sep 17 00:00:00 2001 From: NimBold Date: Sun, 21 Jun 2026 13:48:42 +0330 Subject: [PATCH] fix(download): wait for media process to stop and clean up partial files on remove --- src-tauri/src/lib.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a047542..24305ab 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2440,13 +2440,20 @@ async fn remove_download( log::info!("aria2 remove [{}]: gid {} stopped and forgotten", id, gid); } else { drop(retry_add_guard); - if let Ok(download_id) = Uuid::parse_str(&id) { + let (tx, rx) = tokio::sync::oneshot::channel(); + if matches!(active_kind, Some(crate::queue::TaskKind::Media)) { + state.download_coordinator.pause_media_with_ack(id.clone(), tx).await?; + } else if let Ok(download_id) = Uuid::parse_str(&id) { state .download_coordinator .send(download::DownloadCmd::Cancel(download_id)) .await?; + let _ = tx.send(()); + } else { + let _ = tx.send(()); } - state.download_coordinator.pause_media(id.clone()).await?; + let _ = rx.await; + if !matches!(active_kind, Some(crate::queue::TaskKind::Media)) { state.queue_manager.release_permit(&id).await; } @@ -2472,6 +2479,25 @@ async fn remove_download( if p_aria2.exists() { let _ = tokio::fs::remove_file(p_aria2).await; } + + if let Some(parent) = p.parent() { + if let Some(stem) = p.file_stem().and_then(|s| s.to_str()) { + let stem_with_dot = format!("{}.", stem); + if let Ok(mut entries) = tokio::fs::read_dir(parent).await { + while let Ok(Some(entry)) = entries.next_entry().await { + if let Ok(file_name) = entry.file_name().into_string() { + if file_name.starts_with(&stem_with_dot) && + (file_name.ends_with(".part") || file_name.ends_with(".ytdl") || file_name.ends_with(".aria2")) { + let path_to_remove = entry.path(); + if is_safe_path(&path_to_remove, &app_handle) { + let _ = tokio::fs::remove_file(path_to_remove).await; + } + } + } + } + } + } + } } } }