Skip to content

Commit b269ecb

Browse files
committed
feat!: change random torrent generator endpoint
The new enpopint is: `http://0.0.0.0:3001/v1/torrent/meta-info/random/:uuid` The segments have changed to differenciate the indexed torrent from the torrent file (meta-indo). An indexed torrent is a torrent file (meta-info file) with some extra classification metadata: title, description, category and tags. There is also a new PATH param `:uuid` which is an UUID to identify the generated torrent file. The UUID is used for: - The torrent file name - The sample contents for a text file ffrom which we generate the torrent file.
1 parent 30bf79e commit b269ecb

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/web/api/v1/contexts/torrent/handlers.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ pub async fn delete_torrent_handler(
217217
}
218218
}
219219

220+
#[derive(Debug, Deserialize)]
221+
pub struct UuidParam(pub String);
222+
223+
impl UuidParam {
224+
fn value(&self) -> String {
225+
self.0.to_lowercase()
226+
}
227+
}
228+
220229
/// Returns a random torrent as a byte stream `application/x-bittorrent`.
221230
///
222231
/// This is useful for testing purposes.
@@ -225,8 +234,12 @@ pub async fn delete_torrent_handler(
225234
///
226235
/// Returns `ServiceError::BadRequest` if the torrent info-hash is invalid.
227236
#[allow(clippy::unused_async)]
228-
pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>) -> Response {
229-
let torrent = generate_random_torrent();
237+
pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>, Path(uuid): Path<UuidParam>) -> Response {
238+
let Ok(uuid) = Uuid::parse_str(&uuid.value()) else {
239+
return ServiceError::BadRequest.into_response();
240+
};
241+
242+
let torrent = generate_random_torrent(uuid);
230243

231244
let Ok(bytes) = parse_torrent::encode_torrent(&torrent) else {
232245
return ServiceError::InternalServerError.into_response();
@@ -236,9 +249,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>
236249
}
237250

238251
/// It generates a random single-file torrent for testing purposes.
239-
fn generate_random_torrent() -> Torrent {
240-
let id = Uuid::new_v4();
241-
252+
fn generate_random_torrent(id: Uuid) -> Torrent {
242253
let file_contents = format!("{id}\n");
243254

244255
let torrent_info = DbTorrentInfo {

src/web/api/v1/contexts/torrent/routes.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ pub fn router_for_single_resources(app_data: Arc<AppData>) -> Router {
2121

2222
Router::new()
2323
.route("/upload", post(upload_torrent_handler).with_state(app_data.clone()))
24-
.route("/download/:info_hash", get(download_torrent_handler).with_state(app_data))
24+
.route(
25+
"/download/:info_hash",
26+
get(download_torrent_handler).with_state(app_data.clone()),
27+
)
28+
.route(
29+
"/meta-info/random/:uuid",
30+
get(create_random_torrent_handler).with_state(app_data),
31+
)
2532
.nest("/:info_hash", torrent_info_routes)
2633
}
2734

2835
/// Routes for the [`torrent`](crate::web::api::v1::contexts::torrent) API context for multiple resources.
2936
pub fn router_for_multiple_resources(app_data: Arc<AppData>) -> Router {
30-
Router::new()
31-
.route("/", get(get_torrents_handler).with_state(app_data.clone()))
32-
.route("/random", get(create_random_torrent_handler).with_state(app_data))
37+
Router::new().route("/", get(get_torrents_handler).with_state(app_data))
3338
}

0 commit comments

Comments
 (0)