Skip to content

Commit 414c6c1

Browse files
committed
feat: add a custom header with infohash to the download endpoints
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.
1 parent e2a0ed4 commit 414c6c1

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub async fn download_torrent_handler(
9494
return ServiceError::InternalServerError.into_response();
9595
};
9696

97-
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name))
97+
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name), &torrent.info_hash())
9898
}
9999

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

247-
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name))
247+
torrent_file_response(bytes, &format!("{}.torrent", torrent.info.name), &torrent.info_hash())
248248
}
249249

250250
/// Extracts the [`TorrentRequest`] from the multipart form payload.

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::response::{IntoResponse, Response};
22
use axum::Json;
3-
use hyper::{header, StatusCode};
3+
use hyper::{header, HeaderMap, StatusCode};
44
use serde::{Deserialize, Serialize};
55

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

26+
/// Builds the binary response for a torrent file.
27+
///
28+
/// # Panics
29+
///
30+
/// Panics if the filename is not a valid header value for the `content-disposition`
31+
/// header.
2632
#[must_use]
27-
pub fn torrent_file_response(bytes: Vec<u8>, filename: &str) -> Response {
28-
(
29-
StatusCode::OK,
30-
[
31-
(header::CONTENT_TYPE, "application/x-bittorrent"),
32-
(header::CONTENT_DISPOSITION, &format!("attachment; filename={filename}")),
33-
],
34-
bytes,
35-
)
36-
.into_response()
33+
pub fn torrent_file_response(bytes: Vec<u8>, filename: &str, info_hash: &str) -> Response {
34+
let mut headers = HeaderMap::new();
35+
headers.insert(
36+
header::CONTENT_TYPE,
37+
"application/x-bittorrent"
38+
.parse()
39+
.expect("HTTP content type header should be valid"),
40+
);
41+
headers.insert(
42+
header::CONTENT_DISPOSITION,
43+
format!("attachment; filename={filename}")
44+
.parse()
45+
.expect("Torrent filename should be a valid header value for the content disposition header"),
46+
);
47+
headers.insert(
48+
"x-torrust-torrent-infohash",
49+
info_hash
50+
.parse()
51+
.expect("Torrent infohash should be a valid header value for the content disposition header"),
52+
);
53+
54+
(StatusCode::OK, headers, bytes).into_response()
3755
}

0 commit comments

Comments
 (0)