Skip to content

Commit

Permalink
feat!: change random torrent generator endpoint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josecelano committed Jul 31, 2023
1 parent 30bf79e commit b269ecb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Arc<AppData>>) -> Response {
let torrent = generate_random_torrent();
pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>, Path(uuid): Path<UuidParam>) -> 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();
Expand All @@ -236,9 +249,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>
}

/// 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 {
Expand Down
13 changes: 9 additions & 4 deletions src/web/api/v1/contexts/torrent/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ pub fn router_for_single_resources(app_data: Arc<AppData>) -> 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<AppData>) -> 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))
}

0 comments on commit b269ecb

Please sign in to comment.