From 6ff360050cabecf7311bb72757ca75bfacc0a65e Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 13 Sep 2023 14:09:22 +0100 Subject: [PATCH] fix: [#282] downloaded torrent info-hash matches uploaded one NOTICE: They only matche when the index doesn't change it becuase it removs non-standard fields from the `info` key dictionary. They should match if the uploaded torrent does not have any non-stanrdard field in the `info` dictionary key. --- src/databases/mysql.rs | 4 +--- src/databases/sqlite.rs | 4 +--- src/models/torrent_file.rs | 6 ++---- src/services/torrent_file.rs | 4 ++-- tests/e2e/config.rs | 2 +- tests/e2e/web/api/v1/contexts/torrent/asserts.rs | 6 ++++-- 6 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index 38edcdde..503d30b5 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -442,8 +442,6 @@ impl Database for Mysql { (root_hash.to_string(), true) }; - let private = torrent.info.private.unwrap_or(0); - // add torrent let torrent_id = query("INSERT INTO torrust_torrents (uploader_id, category_id, info_hash, size, name, pieces, piece_length, private, root_hash, `source`, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())") .bind(uploader_id) @@ -453,7 +451,7 @@ impl Database for Mysql { .bind(torrent.info.name.to_string()) .bind(pieces) .bind(torrent.info.piece_length) - .bind(private) + .bind(torrent.info.private) .bind(root_hash) .bind(torrent.info.source.clone()) .execute(&mut tx) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index 6cae2d4a..085b3960 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -432,8 +432,6 @@ impl Database for Sqlite { (root_hash.to_string(), true) }; - let private = torrent.info.private.unwrap_or(0); - // add torrent let torrent_id = query("INSERT INTO torrust_torrents (uploader_id, category_id, info_hash, size, name, pieces, piece_length, private, root_hash, `source`, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')))") .bind(uploader_id) @@ -443,7 +441,7 @@ impl Database for Sqlite { .bind(torrent.info.name.to_string()) .bind(pieces) .bind(torrent.info.piece_length) - .bind(private) + .bind(torrent.info.private) .bind(root_hash) .bind(torrent.info.source.clone()) .execute(&mut tx) diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 97294252..c8849170 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -113,8 +113,6 @@ impl Torrent { /// This function will panic if the `torrent_info.pieces` is not a valid hex string. #[must_use] 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 let mut info = TorrentInfo { name: torrent_info.name.to_string(), @@ -123,7 +121,7 @@ impl Torrent { md5sum: None, length: None, files: None, - private, + private: torrent_info.private, path: None, root_hash: None, source: None, @@ -296,7 +294,7 @@ pub struct DbTorrentInfo { pub pieces: String, pub piece_length: i64, #[serde(default)] - pub private: Option, + pub private: Option, pub root_hash: i64, } diff --git a/src/services/torrent_file.rs b/src/services/torrent_file.rs index 6e474d99..dfa72dbd 100644 --- a/src/services/torrent_file.rs +++ b/src/services/torrent_file.rs @@ -12,7 +12,7 @@ pub struct NewTorrentInfoRequest { pub name: String, pub pieces: String, pub piece_length: i64, - pub private: Option, + pub private: Option, pub root_hash: i64, pub files: Vec, pub announce_urls: Vec>, @@ -77,7 +77,7 @@ mod tests { md5sum: None, length: Some(37), files: None, - private: Some(0), + private: None, path: None, root_hash: None, source: None, diff --git a/tests/e2e/config.rs b/tests/e2e/config.rs index 60595c79..86b857c7 100644 --- a/tests/e2e/config.rs +++ b/tests/e2e/config.rs @@ -19,7 +19,7 @@ pub const ENV_VAR_E2E_CONFIG_PATH: &str = "TORRUST_IDX_BACK_E2E_CONFIG_PATH"; // Default values -pub const ENV_VAR_E2E_DEFAULT_CONFIG_PATH: &str = "./config-idx-back.local.toml"; +pub const ENV_VAR_E2E_DEFAULT_CONFIG_PATH: &str = "./config-idx-back.sqlite.local.toml"; /// Initialize configuration from file or env var. /// diff --git a/tests/e2e/web/api/v1/contexts/torrent/asserts.rs b/tests/e2e/web/api/v1/contexts/torrent/asserts.rs index f8a31714..9f08306d 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/asserts.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/asserts.rs @@ -24,10 +24,12 @@ pub async fn expected_torrent(mut uploaded_torrent: Torrent, env: &TestEnv, down None => None, }; - uploaded_torrent.info.private = Some(0); uploaded_torrent.announce = Some(build_announce_url(&tracker_url, &tracker_key)); - uploaded_torrent.encoding = None; uploaded_torrent.announce_list = Some(build_announce_list(&tracker_url, &tracker_key)); + + // These fields are not persisted in the database yet. + // See https://github.com/torrust/torrust-index-backend/issues/284 + uploaded_torrent.encoding = None; uploaded_torrent.creation_date = None; uploaded_torrent.created_by = None;