Skip to content

Commit c92e41c

Browse files
committed
feat: add a new console command to create random test torrents
You can execute it with: ``` cargo run --bin create_test_torrent ./output/test/torrents ``` Folders must be created.
1 parent eb87363 commit c92e41c

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/.coverage/
22
/.env
3+
/.idea/
34
/config.toml
45
/data_v2.db*
56
/data.db*
7+
/output/
68
/storage/
79
/target
8-
/uploads/
9-
/.idea/
10+
/uploads/

src/bin/create_test_torrent.rs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//! Command line tool to create a test torrent file.
2+
//!
3+
//! It's only used for debugging purposes.
4+
use std::env;
5+
use std::fs::File;
6+
use std::io::Write;
7+
use std::path::Path;
8+
9+
use torrust_index::models::torrent_file::{Torrent, TorrentFile, TorrentInfoDictionary};
10+
use torrust_index::services::hasher::sha1; // DevSkim: ignore DS126858
11+
use torrust_index::utils::parse_torrent;
12+
use uuid::Uuid;
13+
14+
fn main() {
15+
let args: Vec<String> = env::args().collect();
16+
17+
if args.len() != 2 {
18+
eprintln!("Usage: cargo run --bin create_test_torrent <destination_folder>");
19+
eprintln!("Example: cargo run --bin create_test_torrent ./output/test/torrents");
20+
std::process::exit(1);
21+
}
22+
23+
let destination_folder = &args[1];
24+
25+
let id = Uuid::new_v4();
26+
27+
// Content of the file from which the torrent will be generated.
28+
// We use the UUID as the content of the file.
29+
let file_contents = format!("{id}\n");
30+
let file_name = format!("file-{id}.txt");
31+
32+
let torrent = Torrent {
33+
info: TorrentInfoDictionary::with(
34+
&file_name,
35+
16384,
36+
None,
37+
0,
38+
&sha1(&file_contents), // DevSkim: ignore DS126858
39+
&[TorrentFile {
40+
path: vec![file_name.clone()], // Adjusted to include the actual file name
41+
length: i64::try_from(file_contents.len()).expect("file contents size in bytes cannot exceed i64::MAX"),
42+
md5sum: None, // DevSkim: ignore DS126858
43+
}],
44+
),
45+
announce: None,
46+
nodes: Some(vec![("99.236.6.144".to_string(), 6881), ("91.109.195.156".to_string(), 1996)]),
47+
encoding: None,
48+
httpseeds: Some(vec!["https://seeder.torrust-demo.com/seed".to_string()]),
49+
announce_list: Some(vec![vec!["https://tracker.torrust-demo.com/announce".to_string()]]),
50+
creation_date: None,
51+
comment: None,
52+
created_by: None,
53+
};
54+
55+
match parse_torrent::encode_torrent(&torrent) {
56+
Ok(bytes) => {
57+
// Construct the path where the torrent file will be saved
58+
let file_path = Path::new(destination_folder).join(format!("{file_name}.torrent"));
59+
60+
// Attempt to create and write to the file
61+
let mut file = match File::create(&file_path) {
62+
Ok(file) => file,
63+
Err(e) => panic!("Failed to create file {file_path:?}: {e}"),
64+
};
65+
66+
if let Err(e) = file.write_all(&bytes) {
67+
panic!("Failed to write to file {file_path:?}: {e}");
68+
}
69+
70+
println!("File successfully written to {file_path:?}");
71+
}
72+
Err(e) => panic!("Error encoding torrent: {e}"),
73+
};
74+
}

0 commit comments

Comments
 (0)