Skip to content

Commit

Permalink
feat: [torrust#256] store the original infohash in the database
Browse files Browse the repository at this point in the history
Torrent InfoHash migth change after uploading it becuase we do not
extract/store all the fields in the info dictionary. We store a copy of
the original infohash in the database field
`torrust_torrents::original_info_hash`.
  • Loading branch information
josecelano committed Aug 25, 2023
1 parent 0cb63dc commit d7c9d93
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN original_info_hash TEXT DEFAULT NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_torrents ADD COLUMN original_info_hash TEXT DEFAULT NULL
1 change: 1 addition & 0 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub trait Database: Sync + Send {
/// Add new torrent and return the newly inserted `torrent_id` with `torrent`, `uploader_id`, `category_id`, `title` and `description`.
async fn insert_torrent_and_get_id(
&self,
original_info_hash: &InfoHash,
torrent: &Torrent,
uploader_id: UserId,
category_id: i64,
Expand Down
4 changes: 3 additions & 1 deletion src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ impl Database for Mysql {
#[allow(clippy::too_many_lines)]
async fn insert_torrent_and_get_id(
&self,
original_info_hash: &InfoHash,
torrent: &Torrent,
uploader_id: UserId,
category_id: i64,
Expand All @@ -443,7 +444,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, `source`, 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`, original_info_hash, date_uploaded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())")
.bind(uploader_id)
.bind(category_id)
.bind(info_hash.to_lowercase())
Expand All @@ -454,6 +455,7 @@ impl Database for Mysql {
.bind(private)
.bind(root_hash)
.bind(torrent.info.source.clone())
.bind(original_info_hash.to_hex_string())
.execute(&self.pool)
.await
.map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64"))
Expand Down
4 changes: 3 additions & 1 deletion src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl Database for Sqlite {
#[allow(clippy::too_many_lines)]
async fn insert_torrent_and_get_id(
&self,
original_info_hash: &InfoHash,
torrent: &Torrent,
uploader_id: UserId,
category_id: i64,
Expand All @@ -431,7 +432,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, `source`, 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`, original_info_hash, 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 @@ -442,6 +443,7 @@ impl Database for Sqlite {
.bind(private)
.bind(root_hash)
.bind(torrent.info.source.clone())
.bind(original_info_hash.to_hex_string())
.execute(&self.pool)
.await
.map(|v| v.last_insert_rowid())
Expand Down
18 changes: 16 additions & 2 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ impl Index {

metadata.verify()?;

let original_info_hash = parse_torrent::calculate_info_hash(&add_torrent_form.torrent_buffer);

let mut torrent =
parse_torrent::decode_torrent(&add_torrent_form.torrent_buffer).map_err(|_| ServiceError::InvalidTorrentFile)?;

Expand All @@ -154,7 +156,11 @@ impl Index {
.await
.map_err(|_| ServiceError::InvalidCategory)?;

let torrent_id = self.torrent_repository.add(&torrent, &metadata, user_id, category).await?;
let torrent_id = self
.torrent_repository
.add(&original_info_hash, &torrent, &metadata, user_id, category)
.await?;

let info_hash: InfoHash = torrent
.info_hash()
.parse()
Expand Down Expand Up @@ -474,13 +480,21 @@ impl DbTorrentRepository {
/// This function will return an error there is a database error.
pub async fn add(
&self,
original_info_hash: &InfoHash,
torrent: &Torrent,
metadata: &Metadata,
user_id: UserId,
category: Category,
) -> Result<TorrentId, Error> {
self.database
.insert_torrent_and_get_id(torrent, user_id, category.category_id, &metadata.title, &metadata.description)
.insert_torrent_and_get_id(
original_info_hash,
torrent,
user_id,
category.category_id,
&metadata.title,
&metadata.description,
)
.await
}

Expand Down

0 comments on commit d7c9d93

Please sign in to comment.