From c3e61eabf3048676021161dcb80b45fb6e644d2c Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Thu, 3 Aug 2023 18:15:11 +0100 Subject: [PATCH] fix: [#242] wrong infohash when info dict contains source field When you define a "source" field value in the "info" dictionary inside the torrent file the field changes the infohash value. We did not save that field in the database and in the in-memory struct `TorrentInfo` so the calculated infohash was wrong becuase this field belongs to the `info` key. --- .../mysql/20230803160604_torrust_torrents_add_source.sql | 1 + .../sqlite3/20230803160604_torrust_torrents_add_source.sql | 1 + src/databases/mysql.rs | 3 ++- src/databases/sqlite.rs | 3 ++- src/models/torrent_file.rs | 3 +++ src/services/torrent_file.rs | 1 + 6 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 migrations/mysql/20230803160604_torrust_torrents_add_source.sql create mode 100644 migrations/sqlite3/20230803160604_torrust_torrents_add_source.sql diff --git a/migrations/mysql/20230803160604_torrust_torrents_add_source.sql b/migrations/mysql/20230803160604_torrust_torrents_add_source.sql new file mode 100644 index 00000000..5bee0b38 --- /dev/null +++ b/migrations/mysql/20230803160604_torrust_torrents_add_source.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents ADD COLUMN source TEXT DEFAULT NULL diff --git a/migrations/sqlite3/20230803160604_torrust_torrents_add_source.sql b/migrations/sqlite3/20230803160604_torrust_torrents_add_source.sql new file mode 100644 index 00000000..5bee0b38 --- /dev/null +++ b/migrations/sqlite3/20230803160604_torrust_torrents_add_source.sql @@ -0,0 +1 @@ +ALTER TABLE torrust_torrents ADD COLUMN source TEXT DEFAULT NULL diff --git a/src/databases/mysql.rs b/src/databases/mysql.rs index 95a77812..ecde3bf7 100644 --- a/src/databases/mysql.rs +++ b/src/databases/mysql.rs @@ -441,7 +441,7 @@ impl Database for Mysql { 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, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())") + 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) .bind(category_id) .bind(info_hash.to_lowercase()) @@ -451,6 +451,7 @@ impl Database for Mysql { .bind(torrent.info.piece_length) .bind(private) .bind(root_hash) + .bind(torrent.info.source.clone()) .execute(&self.pool) .await .map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64")) diff --git a/src/databases/sqlite.rs b/src/databases/sqlite.rs index 2c69169b..98cb836f 100644 --- a/src/databases/sqlite.rs +++ b/src/databases/sqlite.rs @@ -431,7 +431,7 @@ impl Database for Sqlite { 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, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, strftime('%Y-%m-%d %H:%M:%S',DATETIME('now', 'utc')))") + 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) .bind(category_id) .bind(info_hash.to_lowercase()) @@ -441,6 +441,7 @@ impl Database for Sqlite { .bind(torrent.info.piece_length) .bind(private) .bind(root_hash) + .bind(torrent.info.source.clone()) .execute(&self.pool) .await .map(|v| v.last_insert_rowid()) diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index 8489d90b..87689fe8 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -38,6 +38,8 @@ pub struct TorrentInfo { #[serde(default)] #[serde(rename = "root hash")] pub root_hash: Option, + #[serde(default)] + pub source: Option, } impl TorrentInfo { @@ -123,6 +125,7 @@ impl Torrent { private, path: None, root_hash: None, + source: None, }; // a torrent file has a root hash or a pieces key, but not both. diff --git a/src/services/torrent_file.rs b/src/services/torrent_file.rs index 5c119930..6e474d99 100644 --- a/src/services/torrent_file.rs +++ b/src/services/torrent_file.rs @@ -80,6 +80,7 @@ mod tests { private: Some(0), path: None, root_hash: None, + source: None, }, announce: None, announce_list: Some(vec![]),