forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge torrust#237: Endpoint to create random torrents
0f163cf refactor: invert the dependency between Torrent named constructors (Jose Celano) 4b6f25c test: add test for random torrent file generator service (Jose Celano) 40c4df0 refactor: extract hasher service (Jose Celano) b2870b9 refactor: extract torrent file service (Jose Celano) dfa260e fix: clippy warnings alter updating clippy to clippy 0.1.73 (Jose Celano) b269ecb feat!: change random torrent generator endpoint (Jose Celano) 30bf79e feat: new endpoint to generate random torrents (Jose Celano) dd1dc0c chore: add dependencies: hex, uuid (Jose Celano) Pull request description: Relates to: torrust/torrust-index-gui#185 Sometimes we need to generate a random torrent for testing purposes. We need to generate test torrents for the frontend application. With this new endpoint, you can get a random torrent: http://0.0.0.0:3001/v1/torrents/random The torrent is a single-file torrent using a UUID for its name and the original data file contents. In this repo, we use the `imdl` command to generate random torrents but in the frontend we are using cypress, and it seems the best option is [to make a request to an endpoint](https://docs.cypress.io/api/commands/request) to obtain dynamic fixtures like random (or customized in the future) torrents. torrust/torrust-index-gui#185 ### TODO - [x] Generate the random ID - [x] Calculate the correct value for the "pieces" field in the torrent file (sha1 hash of the contents). - [x] Refactor: extract service. Remove the domain code from the handler. - [ ] Refactor: add a new named the constructor for the `Torrent` instead of using the `from_db_info_files_and_announce_urls`. We do not need the `torrent_id`, for example. Other things that we could implement: - [ ] Add an env var to enable this endpoint only for development/testing environments. - [x] Add an URL parameter with the UUDI: `v1/torrents/random/:uuid`. We use the UUID for the torrent name (torrent file name: `name.torrent`). Top commit has no ACKs. Tree-SHA512: 3740a628b177303e60dac78969c786088c5c4391f7e1936fe6634031e533f38dea67a2aa7b876966456fb649f749773c43961d686c2c120ae62a2705be35c041
- Loading branch information
Showing
10 changed files
with
213 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! Hashing service | ||
use sha1::{Digest, Sha1}; | ||
|
||
// Calculate the sha1 hash of a string | ||
#[must_use] | ||
pub fn sha1(data: &str) -> String { | ||
// Create a Sha1 object | ||
let mut hasher = Sha1::new(); | ||
|
||
// Write input message | ||
hasher.update(data.as_bytes()); | ||
|
||
// Read hash digest and consume hasher | ||
let result = hasher.finalize(); | ||
|
||
// Convert the hash (a byte array) to a string of hex characters | ||
hex::encode(result) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::services::hasher::sha1; | ||
|
||
#[test] | ||
fn it_should_hash_an_string() { | ||
assert_eq!(sha1("hello world"), "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! This module contains the services related to torrent file management. | ||
use uuid::Uuid; | ||
|
||
use crate::models::torrent_file::{Torrent, TorrentFile}; | ||
use crate::services::hasher::sha1; | ||
|
||
/// It contains the information required to create a new torrent file. | ||
/// | ||
/// It's not the full in-memory representation of a torrent file. The full | ||
/// in-memory representation is the `Torrent` struct. | ||
pub struct NewTorrentInfoRequest { | ||
pub name: String, | ||
pub pieces: String, | ||
pub piece_length: i64, | ||
pub private: Option<i64>, | ||
pub root_hash: i64, | ||
pub files: Vec<TorrentFile>, | ||
pub announce_urls: Vec<Vec<String>>, | ||
} | ||
|
||
/// It generates a random single-file torrent for testing purposes. | ||
/// | ||
/// The torrent will contain a single text file with the UUID as its content. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This function will panic if the sample file contents length in bytes is | ||
/// greater than `i64::MAX`. | ||
#[must_use] | ||
pub fn generate_random_torrent(id: Uuid) -> Torrent { | ||
// Content of the file from which the torrent will be generated. | ||
// We use the UUID as the content of the file. | ||
let file_contents = format!("{id}\n"); | ||
|
||
let torrent_files: Vec<TorrentFile> = vec![TorrentFile { | ||
path: vec![String::new()], | ||
length: i64::try_from(file_contents.len()).expect("file contents size in bytes cannot exceed i64::MAX"), | ||
md5sum: None, | ||
}]; | ||
|
||
let torrent_announce_urls: Vec<Vec<String>> = vec![]; | ||
|
||
let torrent_info_request = NewTorrentInfoRequest { | ||
name: format!("file-{id}.txt"), | ||
pieces: sha1(&file_contents), | ||
piece_length: 16384, | ||
private: None, | ||
root_hash: 0, | ||
files: torrent_files, | ||
announce_urls: torrent_announce_urls, | ||
}; | ||
|
||
Torrent::from_new_torrent_info_request(torrent_info_request) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_bytes::ByteBuf; | ||
use uuid::Uuid; | ||
|
||
use crate::models::torrent_file::{Torrent, TorrentInfo}; | ||
use crate::services::torrent_file::generate_random_torrent; | ||
|
||
#[test] | ||
fn it_should_generate_a_random_meta_info_file() { | ||
let uuid = Uuid::parse_str("d6170378-2c14-4ccc-870d-2a8e15195e23").unwrap(); | ||
|
||
let torrent = generate_random_torrent(uuid); | ||
|
||
let expected_torrent = Torrent { | ||
info: TorrentInfo { | ||
name: "file-d6170378-2c14-4ccc-870d-2a8e15195e23.txt".to_string(), | ||
pieces: Some(ByteBuf::from(vec![ | ||
62, 231, 243, 51, 234, 165, 204, 209, 51, 132, 163, 133, 249, 50, 107, 46, 24, 15, 251, 32, | ||
])), | ||
piece_length: 16384, | ||
md5sum: None, | ||
length: Some(37), | ||
files: None, | ||
private: Some(0), | ||
path: None, | ||
root_hash: None, | ||
}, | ||
announce: None, | ||
announce_list: Some(vec![]), | ||
creation_date: None, | ||
comment: None, | ||
created_by: None, | ||
nodes: None, | ||
encoding: None, | ||
httpseeds: None, | ||
}; | ||
|
||
assert_eq!(torrent, expected_torrent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters