Skip to content

Commit

Permalink
feat: add a custom header with infohash to the download endpoints
Browse files Browse the repository at this point in the history
Endpoint where you can download a torrent file have now a custom header
containing the torrent infohash:

The name of the header is: "x-torrust-torrent-infohash"

It is added becuase in the frontend we need the infohash in a cypress
test and this way we do not need to parse the downloaded torrent which
is faster and simpler.
  • Loading branch information
josecelano committed Aug 2, 2023
1 parent e2a0ed4 commit 414c6c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/web/api/v1/contexts/torrent/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub async fn download_torrent_handler(
return ServiceError::InternalServerError.into_response();
};

torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name))
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name), &torrent.info_hash())
}

/// It returns a list of torrents matching the search criteria.
Expand Down Expand Up @@ -244,7 +244,7 @@ pub async fn create_random_torrent_handler(State(_app_data): State<Arc<AppData>>
return ServiceError::InternalServerError.into_response();
};

torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name))
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name), &torrent.info_hash())
}

/// Extracts the [`TorrentRequest`] from the multipart form payload.
Expand Down
40 changes: 29 additions & 11 deletions src/web/api/v1/contexts/torrent/responses.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::response::{IntoResponse, Response};
use axum::Json;
use hyper::{header, StatusCode};
use hyper::{header, HeaderMap, StatusCode};
use serde::{Deserialize, Serialize};

use crate::models::torrent::TorrentId;
Expand All @@ -23,15 +23,33 @@ pub fn new_torrent_response(torrent_id: TorrentId, info_hash: &str) -> Json<OkRe
})
}

/// Builds the binary response for a torrent file.
///
/// # Panics
///
/// Panics if the filename is not a valid header value for the `content-disposition`
/// header.
#[must_use]
pub fn torrent_file_response(bytes: Vec<u8>, filename: &str) -> Response {
(
StatusCode::OK,
[
(header::CONTENT_TYPE, "application/x-bittorrent"),
(header::CONTENT_DISPOSITION, &format!("attachment; filename={filename}")),
],
bytes,
)
.into_response()
pub fn torrent_file_response(bytes: Vec<u8>, filename: &str, info_hash: &str) -> Response {
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
"application/x-bittorrent"
.parse()
.expect("HTTP content type header should be valid"),
);
headers.insert(
header::CONTENT_DISPOSITION,
format!("attachment; filename={filename}")
.parse()
.expect("Torrent filename should be a valid header value for the content disposition header"),
);
headers.insert(
"x-torrust-torrent-infohash",
info_hash
.parse()
.expect("Torrent infohash should be a valid header value for the content disposition header"),
);

(StatusCode::OK, headers, bytes).into_response()
}

0 comments on commit 414c6c1

Please sign in to comment.