Skip to content

Commit 4bfa760

Browse files
committed
Add get_files method on QBittorrentClient
1 parent ca967f1 commit 4bfa760

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ default = [ "qbittorrent" ]
1616
qbittorrent = [ "reqwest" ]
1717

1818
[dependencies]
19-
hightorrent = { version = "0.2", path = "../hightorrent" }
19+
# hightorrent = { version = "0.2", path = "../hightorrent" }
20+
hightorrent = { git = "https://github.com/angrynode/hightorrent" }
2021
tokio = { version = "1", features = [ "fs" ] }
2122
tokio-util = { version = "0.7" }
2223
async-trait = "0.1"

src/api/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::ApiError;
2-
use hightorrent::{SingleTarget, Torrent, TorrentList, Tracker};
2+
use hightorrent::{SingleTarget, Torrent, TorrentContent, TorrentList, Tracker};
33

44
mod add;
55
pub use add::*;
@@ -26,4 +26,6 @@ pub trait Api: Send + Sync + for<'a> ApiAdd<'a> {
2626
async fn get_trackers(&self, hash: &SingleTarget) -> Result<Vec<Tracker>, ApiError>;
2727
async fn add_tracker(&self, hash: &SingleTarget, tracker: &str) -> Result<(), ApiError>;
2828
async fn remove_tracker(&self, hash: &SingleTarget, tracker: &str) -> Result<(), ApiError>;
29+
30+
async fn get_files(&self, hash: &SingleTarget) -> Result<Vec<TorrentContent>, ApiError>;
2931
}

src/qbittorrent/api.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hightorrent::{
2-
InfoHash, MultiTarget, SingleTarget, ToTorrent, Torrent, TorrentID, TorrentList, Tracker,
3-
TryIntoTracker,
2+
InfoHash, MultiTarget, SingleTarget, ToTorrent, ToTorrentContent, Torrent,
3+
TorrentContent, TorrentID, TorrentList, Tracker, TryIntoTracker,
44
};
55
use reqwest::multipart::Form;
66
use reqwest::multipart::Part;
@@ -14,7 +14,7 @@ use std::borrow::Borrow;
1414
use crate::{
1515
api::*,
1616
api_error::{ApiError as Error, *},
17-
qbittorrent::{QBittorrentTorrent, QBittorrentTracker},
17+
qbittorrent::{QBittorrentTorrent, QBittorrentTorrentContent, QBittorrentTracker},
1818
};
1919

2020
#[derive(Clone)]
@@ -296,6 +296,32 @@ impl Api for QBittorrentClient {
296296
})
297297
}
298298
}
299+
300+
async fn get_files(&self, target: &SingleTarget) -> Result<Vec<TorrentContent>, Error> {
301+
let Some(id) = self.id(target).await? else {
302+
return Err(Error::MissingTorrent {
303+
hash: target.as_str().to_string(),
304+
});
305+
};
306+
307+
let mut form = Form::new();
308+
form = form.text("hash", id.as_str().to_string());
309+
let res = self
310+
._post_multipart(self._endpoint("torrents/files"), form)
311+
.await?;
312+
313+
if res.status().is_success() {
314+
let concrete: Vec<QBittorrentTorrentContent> = self._json(res).await?;
315+
Ok(concrete
316+
.iter()
317+
.map(|t| t.to_torrent_content())
318+
.collect())
319+
} else {
320+
Err(Error::MissingTorrent {
321+
hash: target.as_str().to_string(),
322+
})
323+
}
324+
}
299325
}
300326

301327
#[async_trait]

src/qbittorrent/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ mod api;
22
pub use api::QBittorrentClient;
33

44
mod torrent;
5-
pub use torrent::{QBittorrentTorrent, QBittorrentTracker};
5+
pub use torrent::{QBittorrentTorrent, QBittorrentTorrentContent, QBittorrentTracker};

src/qbittorrent/torrent.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
use hightorrent::{InfoHash, ToTorrent, Torrent, TorrentID, Tracker, TrackerError, TryIntoTracker};
1+
use hightorrent::{
2+
InfoHash, ToTorrent, ToTorrentContent, Torrent, TorrentContent, TorrentID, Tracker,
3+
TrackerError, TryIntoTracker,
4+
};
25
use serde::{Deserialize, Deserializer, Serialize};
36

7+
use std::path::PathBuf;
8+
49
/// Deserializes from the 'info' endpoint of QBittorrent API
510
/// [See QBittorrent API docs](https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#get-torrent-list)
611
#[derive(Clone, Debug, Deserialize)]
@@ -82,3 +87,22 @@ impl PartialEq for QBittorrentTracker {
8287
self.url == other.url
8388
}
8489
}
90+
91+
#[derive(Debug, Serialize, Deserialize)]
92+
pub struct QBittorrentTorrentContent {
93+
#[serde(rename = "name")]
94+
pub path: PathBuf,
95+
pub size: u32,
96+
pub progress: f32,
97+
#[serde(default)]
98+
pub is_seed: bool,
99+
}
100+
101+
impl ToTorrentContent for QBittorrentTorrentContent {
102+
fn to_torrent_content(&self) -> TorrentContent {
103+
TorrentContent {
104+
path: self.path.clone(),
105+
size: self.size as u64,
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)