Skip to content

Commit

Permalink
fix: [torrust#242] wrong infohash when info dict contains source field
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josecelano committed Aug 4, 2023
1 parent 27354a4 commit c3e61ea
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN source TEXT DEFAULT NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN source TEXT DEFAULT NULL
3 changes: 2 additions & 1 deletion src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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"))
Expand Down
3 changes: 2 additions & 1 deletion src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct TorrentInfo {
#[serde(default)]
#[serde(rename = "root hash")]
pub root_hash: Option<String>,
#[serde(default)]
pub source: Option<String>,
}

impl TorrentInfo {
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/services/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod tests {
private: Some(0),
path: None,
root_hash: None,
source: None,
},
announce: None,
announce_list: Some(vec![]),
Expand Down

0 comments on commit c3e61ea

Please sign in to comment.