Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

feat(magnet): implement native pause/resume for torrents

InstaZDLL7c3cd1de91f3
diff --git a/src-tauri/omniget-core/src/models/media.rs b/src-tauri/omniget-core/src/models/media.rs
index a92772da..4c4a4502 100644
--- a/src-tauri/omniget-core/src/models/media.rs
+++ b/src-tauri/omniget-core/src/models/media.rs
@@ -49,6 +49,7 @@ pub struct DownloadOptions {
pub concurrent_fragments: u32,
pub ytdlp_path: Option<PathBuf>,
pub torrent_listen_port: Option<u16>,
+ pub torrent_id_slot: Option<std::sync::Arc<tokio::sync::Mutex<Option<usize>>>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
diff --git a/src-tauri/src/commands/downloads.rs b/src-tauri/src/commands/downloads.rs
index ec227988..7668f014 100644
--- a/src-tauri/src/commands/downloads.rs
+++ b/src-tauri/src/commands/downloads.rs
@@ -269,14 +269,22 @@ pub async fn pause_download(
state: tauri::State<'_, AppState>,
download_id: u64,
) -> Result<String, String> {
- let state_to_emit = {
+ let (state_to_emit, torrent_id) = {
let mut q = state.download_queue.lock().await;
if q.pause(download_id) {
- Some(q.get_state())
+ let tid = q.items.iter().find(|i| i.id == download_id).and_then(|i| i.torrent_id);
+ (Some(q.get_state()), tid)
} else {
- None
+ (None, None)
}
};
+ if let Some(tid) = torrent_id {
+ if let Some(session) = state.torrent_session.lock().await.as_ref() {
+ if let Some(handle) = session.get(librqbit::api::TorrentIdOrHash::Id(tid)) {
+ let _ = session.pause(&handle).await;
+ }
+ }
+ }
if let Some(s) = state_to_emit {
emit_queue_state_from_state(&app, s);
queue::try_start_next(app, state.download_queue.clone()).await;
@@ -292,14 +300,22 @@ pub async fn resume_download(
state: tauri::State<'_, AppState>,
download_id: u64,
) -> Result<String, String> {
- let state_to_emit = {
+ let (state_to_emit, torrent_id) = {
let mut q = state.download_queue.lock().await;
if q.resume(download_id) {
- Some(q.get_state())
+ let tid = q.items.iter().find(|i| i.id == download_id).and_then(|i| i.torrent_id);
+ (Some(q.get_state()), tid)
} else {
- None
+ (None, None)
}
};
+ if let Some(tid) = torrent_id {
+ if let Some(session) = state.torrent_session.lock().await.as_ref() {
+ if let Some(handle) = session.get(librqbit::api::TorrentIdOrHash::Id(tid)) {
+ let _ = session.unpause(&handle).await;
+ }
+ }
+ }
if let Some(s) = state_to_emit {
emit_queue_state_from_state(&app, s);
queue::try_start_next(app, state.download_queue.clone()).await;
diff --git a/src-tauri/src/core/queue.rs b/src-tauri/src/core/queue.rs
index 13f57d04..bf247ee0 100644
--- a/src-tauri/src/core/queue.rs
+++ b/src-tauri/src/core/queue.rs
@@ -260,6 +260,7 @@ impl DownloadQueue {
speed: f64,
downloaded: u64,
total: Option<u64>,
+ torrent_id: Option<usize>,
) {
if let Some(item) = self.items.iter_mut().find(|i| i.id == id) {
item.percent = percent;
@@ -268,13 +269,18 @@ impl DownloadQueue {
if let Some(t) = total {
item.total_bytes = Some(t);
}
+ if torrent_id.is_some() && item.torrent_id.is_none() {
+ item.torrent_id = torrent_id;
+ }
}
}
pub fn pause(&mut self, id: u64) -> bool {
if let Some(item) = self.items.iter_mut().find(|i| i.id == id) {
if item.status == QueueStatus::Active {
- item.cancel_token.cancel();
+ if item.platform != "magnet" {
+ item.cancel_token.cancel();
+ }
item.status = QueueStatus::Paused;
item.speed_bytes_per_sec = 0.0;
return true;
@@ -286,8 +292,12 @@ impl DownloadQueue {
pub fn resume(&mut self, id: u64) -> bool {
if let Some(item) = self.items.iter_mut().find(|i| i.id == id) {
if item.status == QueueStatus::Paused {
- item.status = QueueStatus::Queued;
- item.cancel_token = CancellationToken::new();
+ if item.platform == "magnet" {
+ item.status = QueueStatus::Active;
+ } else {
+ item.status = QueueStatus::Queued;
+ item.cancel_token = CancellationToken::new();
+ }
return true;
}
}
@@ -580,6 +590,7 @@ async fn spawn_download_inner(
if settings.download.organize_by_platform {
final_output_dir = final_output_dir.join(&platform_name);
}
+ let torrent_id_slot = Arc::new(tokio::sync::Mutex::new(None));
let opts = crate::models::media::DownloadOptions {
quality: quality.or_else(|| Some(settings.download.video_quality.clone())),
output_dir: final_output_dir,
@@ -592,6 +603,7 @@ async fn spawn_download_inner(
concurrent_fragments: settings.advanced.concurrent_fragments,
ytdlp_path,
torrent_listen_port: Some(settings.advanced.torrent_listen_port),
+ torrent_id_slot: Some(torrent_id_slot.clone()),
};
let total_bytes = info.file_size_bytes;
@@ -601,6 +613,7 @@ async fn spawn_download_inner(
let app_progress = app.clone();
let queue_progress = queue.clone();
+ let torrent_id_slot_progress = torrent_id_slot.clone();
let progress_forwarder = tokio::spawn(async move {
let mut last_bytes: u64 = 0;
let mut last_time = std::time::Instant::now();
@@ -642,7 +655,8 @@ async fn spawn_download_inner(
{
let mut q = queue_progress.lock().await;
- q.update_progress(item_id, clamped, current_speed, downloaded_bytes, total_bytes);
+ let tid = { *torrent_id_slot_progress.lock().await };
+ q.update_progress(item_id, clamped, current_speed, downloaded_bytes, total_bytes, tid);
}
let _ = app_progress.emit("queue-item-progress", &QueueItemProgress {
diff --git a/src-tauri/src/platforms/magnet/mod.rs b/src-tauri/src/platforms/magnet/mod.rs
index 1463a335..a21be2e6 100644
--- a/src-tauri/src/platforms/magnet/mod.rs
+++ b/src-tauri/src/platforms/magnet/mod.rs
@@ -118,6 +118,10 @@ impl PlatformDownloader for MagnetDownloader {
tracing::info!("[magnet] torrent added (id={}), waiting for download...", torrent_id);
+ if let Some(slot) = &opts.torrent_id_slot {
+ *slot.lock().await = Some(torrent_id);
+ }
+
let completion = managed_torrent.wait_until_completed();
tokio::pin!(completion);