Skip to content

Commit c3e61ea

Browse files
committed
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.
1 parent 27354a4 commit c3e61ea

File tree

6 files changed

+10
-2
lines changed

6 files changed

+10
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE torrust_torrents ADD COLUMN source TEXT DEFAULT NULL
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE torrust_torrents ADD COLUMN source TEXT DEFAULT NULL

src/databases/mysql.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl Database for Mysql {
441441
let private = torrent.info.private.unwrap_or(0);
442442

443443
// add torrent
444-
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())")
444+
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())")
445445
.bind(uploader_id)
446446
.bind(category_id)
447447
.bind(info_hash.to_lowercase())
@@ -451,6 +451,7 @@ impl Database for Mysql {
451451
.bind(torrent.info.piece_length)
452452
.bind(private)
453453
.bind(root_hash)
454+
.bind(torrent.info.source.clone())
454455
.execute(&self.pool)
455456
.await
456457
.map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64"))

src/databases/sqlite.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl Database for Sqlite {
431431
let private = torrent.info.private.unwrap_or(0);
432432

433433
// add torrent
434-
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')))")
434+
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')))")
435435
.bind(uploader_id)
436436
.bind(category_id)
437437
.bind(info_hash.to_lowercase())
@@ -441,6 +441,7 @@ impl Database for Sqlite {
441441
.bind(torrent.info.piece_length)
442442
.bind(private)
443443
.bind(root_hash)
444+
.bind(torrent.info.source.clone())
444445
.execute(&self.pool)
445446
.await
446447
.map(|v| v.last_insert_rowid())

src/models/torrent_file.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ pub struct TorrentInfo {
3838
#[serde(default)]
3939
#[serde(rename = "root hash")]
4040
pub root_hash: Option<String>,
41+
#[serde(default)]
42+
pub source: Option<String>,
4143
}
4244

4345
impl TorrentInfo {
@@ -123,6 +125,7 @@ impl Torrent {
123125
private,
124126
path: None,
125127
root_hash: None,
128+
source: None,
126129
};
127130

128131
// a torrent file has a root hash or a pieces key, but not both.

src/services/torrent_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ mod tests {
8080
private: Some(0),
8181
path: None,
8282
root_hash: None,
83+
source: None,
8384
},
8485
announce: None,
8586
announce_list: Some(vec![]),

0 commit comments

Comments
 (0)