From 5b5352710eaca48fc904bd0d0e6fafb351ecd743 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 12 Aug 2024 23:50:55 +0100 Subject: [PATCH] Add an HTTP endpoint to read torrent bytes --- crates/librqbit/src/http_api.rs | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 46b505c..360388a 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -26,7 +26,7 @@ use crate::torrent_state::peer::stats::snapshot::PeerStatsFilter; type ApiState = Api; use crate::api::Result; -use crate::{ApiError, ManagedTorrent}; +use crate::{ApiError, ListOnlyResponse, ManagedTorrent}; /// An HTTP server for the API. pub struct HttpApi { @@ -188,6 +188,37 @@ impl HttpApi { ) } + async fn resolve_magnet( + State(state): State, + url: String, + ) -> Result { + let added = state + .session() + .add_torrent( + AddTorrent::from_url(&url), + Some(AddTorrentOptions { + list_only: true, + ..Default::default() + }), + ) + .await?; + let content = match added { + crate::AddTorrentResponse::AlreadyManaged(_, handle) => { + handle.info().torrent_bytes.clone().0 + } + crate::AddTorrentResponse::ListOnly(ListOnlyResponse { torrent_bytes, .. }) => { + torrent_bytes.0 + } + crate::AddTorrentResponse::Added(_, _) => { + return Err(ApiError::new_from_text( + StatusCode::INTERNAL_SERVER_ERROR, + "bug: torrent was added to session, but shouldn't have been", + )) + } + }; + Ok(content) + } + async fn torrent_playlist( State(state): State, headers: HeaderMap, @@ -388,6 +419,7 @@ impl HttpApi { .route("/torrents/:id/stream/:file_id", get(torrent_stream_file)) .route("/torrents/:id/playlist", get(torrent_playlist)) .route("/torrents/playlist", get(global_playlist)) + .route("/torrents/resolve_magnet", post(resolve_magnet)) .route( "/torrents/:id/stream/:file_id/*filename", get(torrent_stream_file),