Skip to content

Commit

Permalink
refactor: invert the dependency between Torrent named constructors
Browse files Browse the repository at this point in the history
The constructor to hidrate the object from the database should depeden
on the other to create a new Torrent from scracth. In fact, the
`torrent_id` and `info_hash` in the `DbTorrentInfo` are not needed.
  • Loading branch information
josecelano committed Aug 1, 2023
1 parent 4b6f25c commit 0f163cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,13 @@ pub struct Torrent {
}

impl Torrent {
#[must_use]
pub fn from_new_torrent_info_request(new_torrent_info: NewTorrentInfoRequest) -> Self {
let torrent_info = DbTorrentInfo {
torrent_id: 1,
info_hash: String::new(),
name: new_torrent_info.name,
pieces: new_torrent_info.pieces,
piece_length: 16384,
private: None,
root_hash: 0,
};
Torrent::from_db_info_files_and_announce_urls(torrent_info, new_torrent_info.files, new_torrent_info.announce_urls)
}

/// It hydrates a `Torrent` struct from the database data.
/// It builds a `Torrent` from a `NewTorrentInfoRequest`.
///
/// # Panics
///
/// This function will panic if the `torrent_info.pieces` is not a valid hex string.
#[must_use]
pub fn from_db_info_files_and_announce_urls(
torrent_info: DbTorrentInfo,
torrent_files: Vec<TorrentFile>,
torrent_announce_urls: Vec<Vec<String>>,
) -> Self {
pub fn from_new_torrent_info_request(torrent_info: NewTorrentInfoRequest) -> Self {
let private = u8::try_from(torrent_info.private.unwrap_or(0)).ok();

// the info part of the torrent file
Expand All @@ -152,8 +134,9 @@ impl Torrent {
}

// either set the single file or the multiple files information
if torrent_files.len() == 1 {
let torrent_file = torrent_files
if torrent_info.files.len() == 1 {
let torrent_file = torrent_info
.files
.first()
.expect("vector `torrent_files` should have at least one element");

Expand All @@ -175,7 +158,7 @@ impl Torrent {

info.path = path;
} else {
info.files = Some(torrent_files);
info.files = Some(torrent_info.files);
}

Self {
Expand All @@ -184,13 +167,32 @@ impl Torrent {
nodes: None,
encoding: None,
httpseeds: None,
announce_list: Some(torrent_announce_urls),
announce_list: Some(torrent_info.announce_urls),
creation_date: None,
comment: None,
created_by: None,
}
}

/// It hydrates a `Torrent` struct from the database data.
#[must_use]
pub fn from_db_info_files_and_announce_urls(
torrent_info: DbTorrentInfo,
torrent_files: Vec<TorrentFile>,
torrent_announce_urls: Vec<Vec<String>>,
) -> Self {
let torrent_info_request = NewTorrentInfoRequest {
name: torrent_info.name,
pieces: torrent_info.pieces,
piece_length: torrent_info.piece_length,
private: torrent_info.private,
root_hash: torrent_info.root_hash,
files: torrent_files,
announce_urls: torrent_announce_urls,
};
Torrent::from_new_torrent_info_request(torrent_info_request)
}

/// Sets the announce url to the tracker url and removes all other trackers
/// if the torrent is private.
pub async fn set_announce_urls(&mut self, cfg: &Configuration) {
Expand Down
4 changes: 4 additions & 0 deletions src/services/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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,
Expand Down

0 comments on commit 0f163cf

Please sign in to comment.