Skip to content

Commit

Permalink
Merge torrust#511: New feature: a new console command to create rando…
Browse files Browse the repository at this point in the history
…m test torrents

c92e41c feat: add a new console command to create random test torrents (Jose Celano)

Pull request description:

  You can execute it with:

  ```
  cargo run --bin create_test_torrent ./output/test/torrents
  ```

  All fields are always the same except for the content and hashes.

  ```json
  {
     "announce-list": [
        [
           "https://tracker.torrust-demo.com/announce"
        ]
     ],
     "httpseeds": [
        "https://seeder.torrust-demo.com/seed"
     ],
     "info": {
        "length": 37,
        "name": "file-e1fdbe05-4d33-4748-bab0-6abd297f5230.txt",
        "path": [
           "file-e1fdbe05-4d33-4748-bab0-6abd297f5230.txt"
        ],
        "piece length": 16384,
        "pieces": "<hex>AA 2C 11 CB 66 F4 CE D8 CA AA 09 2C 40 AC EA BC 95 34 44 F8</hex>"
     },
     "nodes": [
        [
           "99.236.6.144",
           6881
        ],
        [
           "91.109.195.156",
           1996
        ]
     ]
  }
  ```

  Output dir must exist.

ACKs for top commit:
  josecelano:
    ACK c92e41c

Tree-SHA512: 7f35e33ddee9e471c6cf699e905289e305b707a64ceb30980f72274bee43e31b2d2d25d3616acfa621db363c30e8ccf986b652a84869a1d147b3e73d0aeb3d7a
  • Loading branch information
josecelano committed Feb 29, 2024
2 parents eb87363 + c92e41c commit 11519b4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/.coverage/
/.env
/.idea/
/config.toml
/data_v2.db*
/data.db*
/output/
/storage/
/target
/uploads/
/.idea/
/uploads/
74 changes: 74 additions & 0 deletions src/bin/create_test_torrent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! Command line tool to create a test torrent file.
//!
//! It's only used for debugging purposes.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

use torrust_index::models::torrent_file::{Torrent, TorrentFile, TorrentInfoDictionary};
use torrust_index::services::hasher::sha1; // DevSkim: ignore DS126858
use torrust_index::utils::parse_torrent;
use uuid::Uuid;

fn main() {
let args: Vec<String> = env::args().collect();

if args.len() != 2 {
eprintln!("Usage: cargo run --bin create_test_torrent <destination_folder>");
eprintln!("Example: cargo run --bin create_test_torrent ./output/test/torrents");
std::process::exit(1);
}

let destination_folder = &args[1];

let id = Uuid::new_v4();

// 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 file_name = format!("file-{id}.txt");

let torrent = Torrent {
info: TorrentInfoDictionary::with(
&file_name,
16384,
None,
0,
&sha1(&file_contents), // DevSkim: ignore DS126858
&[TorrentFile {
path: vec![file_name.clone()], // Adjusted to include the actual file name
length: i64::try_from(file_contents.len()).expect("file contents size in bytes cannot exceed i64::MAX"),
md5sum: None, // DevSkim: ignore DS126858
}],
),
announce: None,
nodes: Some(vec![("99.236.6.144".to_string(), 6881), ("91.109.195.156".to_string(), 1996)]),
encoding: None,
httpseeds: Some(vec!["https://seeder.torrust-demo.com/seed".to_string()]),
announce_list: Some(vec![vec!["https://tracker.torrust-demo.com/announce".to_string()]]),
creation_date: None,
comment: None,
created_by: None,
};

match parse_torrent::encode_torrent(&torrent) {
Ok(bytes) => {
// Construct the path where the torrent file will be saved
let file_path = Path::new(destination_folder).join(format!("{file_name}.torrent"));

// Attempt to create and write to the file
let mut file = match File::create(&file_path) {
Ok(file) => file,
Err(e) => panic!("Failed to create file {file_path:?}: {e}"),
};

if let Err(e) = file.write_all(&bytes) {
panic!("Failed to write to file {file_path:?}: {e}");
}

println!("File successfully written to {file_path:?}");
}
Err(e) => panic!("Error encoding torrent: {e}"),
};
}

0 comments on commit 11519b4

Please sign in to comment.