Skip to content

Commit 0f163cf

Browse files
committed
refactor: invert the dependency between Torrent named constructors
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.
1 parent 4b6f25c commit 0f163cf

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/models/torrent_file.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,13 @@ pub struct Torrent {
103103
}
104104

105105
impl Torrent {
106-
#[must_use]
107-
pub fn from_new_torrent_info_request(new_torrent_info: NewTorrentInfoRequest) -> Self {
108-
let torrent_info = DbTorrentInfo {
109-
torrent_id: 1,
110-
info_hash: String::new(),
111-
name: new_torrent_info.name,
112-
pieces: new_torrent_info.pieces,
113-
piece_length: 16384,
114-
private: None,
115-
root_hash: 0,
116-
};
117-
Torrent::from_db_info_files_and_announce_urls(torrent_info, new_torrent_info.files, new_torrent_info.announce_urls)
118-
}
119-
120-
/// It hydrates a `Torrent` struct from the database data.
106+
/// It builds a `Torrent` from a `NewTorrentInfoRequest`.
121107
///
122108
/// # Panics
123109
///
124110
/// This function will panic if the `torrent_info.pieces` is not a valid hex string.
125111
#[must_use]
126-
pub fn from_db_info_files_and_announce_urls(
127-
torrent_info: DbTorrentInfo,
128-
torrent_files: Vec<TorrentFile>,
129-
torrent_announce_urls: Vec<Vec<String>>,
130-
) -> Self {
112+
pub fn from_new_torrent_info_request(torrent_info: NewTorrentInfoRequest) -> Self {
131113
let private = u8::try_from(torrent_info.private.unwrap_or(0)).ok();
132114

133115
// the info part of the torrent file
@@ -152,8 +134,9 @@ impl Torrent {
152134
}
153135

154136
// either set the single file or the multiple files information
155-
if torrent_files.len() == 1 {
156-
let torrent_file = torrent_files
137+
if torrent_info.files.len() == 1 {
138+
let torrent_file = torrent_info
139+
.files
157140
.first()
158141
.expect("vector `torrent_files` should have at least one element");
159142

@@ -175,7 +158,7 @@ impl Torrent {
175158

176159
info.path = path;
177160
} else {
178-
info.files = Some(torrent_files);
161+
info.files = Some(torrent_info.files);
179162
}
180163

181164
Self {
@@ -184,13 +167,32 @@ impl Torrent {
184167
nodes: None,
185168
encoding: None,
186169
httpseeds: None,
187-
announce_list: Some(torrent_announce_urls),
170+
announce_list: Some(torrent_info.announce_urls),
188171
creation_date: None,
189172
comment: None,
190173
created_by: None,
191174
}
192175
}
193176

177+
/// It hydrates a `Torrent` struct from the database data.
178+
#[must_use]
179+
pub fn from_db_info_files_and_announce_urls(
180+
torrent_info: DbTorrentInfo,
181+
torrent_files: Vec<TorrentFile>,
182+
torrent_announce_urls: Vec<Vec<String>>,
183+
) -> Self {
184+
let torrent_info_request = NewTorrentInfoRequest {
185+
name: torrent_info.name,
186+
pieces: torrent_info.pieces,
187+
piece_length: torrent_info.piece_length,
188+
private: torrent_info.private,
189+
root_hash: torrent_info.root_hash,
190+
files: torrent_files,
191+
announce_urls: torrent_announce_urls,
192+
};
193+
Torrent::from_new_torrent_info_request(torrent_info_request)
194+
}
195+
194196
/// Sets the announce url to the tracker url and removes all other trackers
195197
/// if the torrent is private.
196198
pub async fn set_announce_urls(&mut self, cfg: &Configuration) {

src/services/torrent_file.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use uuid::Uuid;
44
use crate::models::torrent_file::{Torrent, TorrentFile};
55
use crate::services::hasher::sha1;
66

7+
/// It contains the information required to create a new torrent file.
8+
///
9+
/// It's not the full in-memory representation of a torrent file. The full
10+
/// in-memory representation is the `Torrent` struct.
711
pub struct NewTorrentInfoRequest {
812
pub name: String,
913
pub pieces: String,

0 commit comments

Comments
 (0)