JSON torrent detail api

This commit is contained in:
Igor Katson 2021-07-08 23:49:25 +01:00
parent 3beac77e5d
commit e7c310a1df
2 changed files with 59 additions and 42 deletions

View file

@ -3,53 +3,12 @@ use std::sync::Arc;
use parking_lot::RwLock;
use serde::Serialize;
use std::io::Write;
use std::time::{Duration, Instant};
use warp::Filter;
use crate::torrent_manager::TorrentManagerHandle;
use crate::torrent_state::StatsSnapshot;
// enum Response<T, B> {
// NotFound(usize),
// OkJson(T),
// Ok(B),
// }
// impl<T, B> warp::Reply for Response<T, B>
// where T: Serialize + Send,
// B: Into<warp::hyper::Body> + Send
// {
// fn into_response(self) -> warp::reply::Response
// {
// match self {
// Response::NotFound(idx) => {
// let mut response = warp::reply::Response::new(warp::hyper::Body::from(format!(
// "torrent {} not found",
// idx
// )));
// *response.status_mut() = warp::http::StatusCode::NOT_FOUND;
// response
// }
// Response::OkJson(body) => {
// match serde_json::to_vec_pretty(&body) {
// Ok(body) => {
// let mut response = warp::reply::Response::new(warp::hyper::Body::from(body));
// response.headers_mut().insert("content-type", warp::http::HeaderValue::from_static("application/json"));
// response
// }
// Err(e) => {
// todo!()
// }
// }
// },
// Response::Ok(body) => warp::reply::Response::new(body.into()),
// }
// }
// }
struct Inner {
startup_time: Instant,
torrent_managers: RwLock<Vec<TorrentManagerHandle>>,
@ -96,6 +55,18 @@ struct TorrentListResponse {
torrents: Vec<TorrentListResponseItem>,
}
#[derive(Serialize)]
struct TorrentDetailsResponseFile {
name: Option<String>,
length: u64,
}
#[derive(Serialize)]
struct TorrentDetailsResponse {
info_hash: String,
files: Vec<TorrentDetailsResponseFile>,
}
#[derive(Serialize)]
struct StatsResponse {
snapshot: StatsSnapshot,
@ -125,6 +96,21 @@ impl Inner {
}
}
fn api_torrent_details(&self, idx: usize) -> Option<TorrentDetailsResponse> {
let handle = self.mgr_handle(idx)?;
let info_hash = hex::encode(handle.torrent_state().info_hash());
let files = handle
.torrent_state()
.info()
.iter_filenames_and_lengths()
.map(|(filename_it, length)| {
let name = filename_it.to_string().ok();
TorrentDetailsResponseFile { name, length }
})
.collect();
Some(TorrentDetailsResponse { info_hash, files })
}
fn api_stats(&self, idx: usize) -> Option<StatsResponse> {
let mgr = self.mgr_handle(idx)?;
let snapshot = mgr.torrent_state().stats_snapshot();
@ -204,6 +190,11 @@ impl HttpApi {
move || json_response(inner.api_torrent_list())
});
let torrent_details = warp::path!(usize).map({
let inner = inner.clone();
move |idx| json_or_404(idx, inner.api_torrent_details(idx))
});
let dump_haves = warp::path!(usize / "haves").map({
let inner = inner.clone();
move |idx| json_or_404(idx, inner.api_dump_haves(idx))
@ -214,9 +205,15 @@ impl HttpApi {
move |idx| json_or_404(idx, inner.api_stats(idx))
});
let router = list.or(dump_haves).or(dump_stats);
let router = list.or(torrent_details).or(dump_haves).or(dump_stats);
warp::serve(router).run(addr).await;
Ok(())
}
}
impl Default for HttpApi {
fn default() -> Self {
Self::new()
}
}

View file

@ -85,6 +85,26 @@ where
}
impl<'a, ByteBuf> FileIteratorName<'a, ByteBuf> {
pub fn to_string(&self) -> anyhow::Result<String>
where
ByteBuf: AsRef<[u8]>,
{
let mut it = self.iter_components();
let mut buf = it
.next()
.and_then(|v| v)
.map(|v| std::str::from_utf8(v.as_ref()))
.ok_or_else(|| anyhow::anyhow!("empty filename"))??
.to_string();
for bit in it {
buf.push('/');
let bit = bit
.map(|v| std::str::from_utf8(v.as_ref()))
.ok_or_else(|| anyhow::anyhow!("empty filename"))??;
buf.push_str(bit);
}
Ok(buf)
}
pub fn to_pathbuf(&self) -> anyhow::Result<PathBuf>
where
ByteBuf: AsRef<[u8]>,