diff --git a/src/web/api/v1/contexts/torrent/handlers.rs b/src/web/api/v1/contexts/torrent/handlers.rs index 868ae871..a3e40327 100644 --- a/src/web/api/v1/contexts/torrent/handlers.rs +++ b/src/web/api/v1/contexts/torrent/handlers.rs @@ -217,6 +217,15 @@ pub async fn delete_torrent_handler( } } +#[derive(Debug, Deserialize)] +pub struct UuidParam(pub String); + +impl UuidParam { + fn value(&self) -> String { + self.0.to_lowercase() + } +} + /// Returns a random torrent as a byte stream `application/x-bittorrent`. /// /// This is useful for testing purposes. @@ -225,8 +234,12 @@ pub async fn delete_torrent_handler( /// /// Returns `ServiceError::BadRequest` if the torrent info-hash is invalid. #[allow(clippy::unused_async)] -pub async fn create_random_torrent_handler(State(_app_data): State>) -> Response { - let torrent = generate_random_torrent(); +pub async fn create_random_torrent_handler(State(_app_data): State>, Path(uuid): Path) -> Response { + let Ok(uuid) = Uuid::parse_str(&uuid.value()) else { + return ServiceError::BadRequest.into_response(); + }; + + let torrent = generate_random_torrent(uuid); let Ok(bytes) = parse_torrent::encode_torrent(&torrent) else { return ServiceError::InternalServerError.into_response(); @@ -236,9 +249,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State> } /// It generates a random single-file torrent for testing purposes. -fn generate_random_torrent() -> Torrent { - let id = Uuid::new_v4(); - +fn generate_random_torrent(id: Uuid) -> Torrent { let file_contents = format!("{id}\n"); let torrent_info = DbTorrentInfo { diff --git a/src/web/api/v1/contexts/torrent/routes.rs b/src/web/api/v1/contexts/torrent/routes.rs index f2df6bbe..1c529599 100644 --- a/src/web/api/v1/contexts/torrent/routes.rs +++ b/src/web/api/v1/contexts/torrent/routes.rs @@ -21,13 +21,18 @@ pub fn router_for_single_resources(app_data: Arc) -> Router { Router::new() .route("/upload", post(upload_torrent_handler).with_state(app_data.clone())) - .route("/download/:info_hash", get(download_torrent_handler).with_state(app_data)) + .route( + "/download/:info_hash", + get(download_torrent_handler).with_state(app_data.clone()), + ) + .route( + "/meta-info/random/:uuid", + get(create_random_torrent_handler).with_state(app_data), + ) .nest("/:info_hash", torrent_info_routes) } /// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for multiple resources. pub fn router_for_multiple_resources(app_data: Arc) -> Router { - Router::new() - .route("/", get(get_torrents_handler).with_state(app_data.clone())) - .route("/random", get(create_random_torrent_handler).with_state(app_data)) + Router::new().route("/", get(get_torrents_handler).with_state(app_data)) }