Skip to content

Commit

Permalink
feat!: [torrust#115] change endpoint GET /torrent/download/{id} to GE…
Browse files Browse the repository at this point in the history
…T /torrent/download/{infohash}

BREAKING CHANGE: you can not use the odl endpoint enaymore: `GET /torrent/download/{id}`.

You have to use the torrent infohash.
  • Loading branch information
josecelano committed May 5, 2023
1 parent e0c01d6 commit 16bd04c
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 47 deletions.
4 changes: 4 additions & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ addrs
AUTOINCREMENT
bencode
bencoded
binascii
btih
chrono
compatiblelicenses
Expand All @@ -16,6 +17,7 @@ Dont
Grünwald
hasher
Hasher
hexlify
httpseeds
imagoodboy
imdl
Expand All @@ -27,6 +29,7 @@ LEECHERS
lettre
luckythelab
mailcatcher
metainfo
nanos
NCCA
nilm
Expand All @@ -47,6 +50,7 @@ sublist
subpoints
tempdir
tempfile
thiserror
torrust
Torrust
upgrader
Expand Down
33 changes: 32 additions & 1 deletion src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::databases::mysql::MysqlDatabase;
use crate::databases::sqlite::SqliteDatabase;
use crate::models::info_hash::InfoHash;
use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentInfo, Torrent, TorrentFile};
Expand Down Expand Up @@ -154,12 +155,42 @@ pub trait Database: Sync + Send {
description: &str,
) -> Result<i64, DatabaseError>;

/// Get `Torrent` from `InfoHash`.
async fn get_torrent_from_info_hash(&self, info_hash: &InfoHash) -> Result<Torrent, DatabaseError> {
let torrent_info = self.get_torrent_info_from_infohash(*info_hash).await?;

let torrent_files = self.get_torrent_files_from_id(torrent_info.torrent_id).await?;

let torrent_announce_urls = self.get_torrent_announce_urls_from_id(torrent_info.torrent_id).await?;

Ok(Torrent::from_db_info_files_and_announce_urls(
torrent_info,
torrent_files,
torrent_announce_urls,
))
}

/// Get `Torrent` from `torrent_id`.
async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, DatabaseError>;
async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, DatabaseError> {
let torrent_info = self.get_torrent_info_from_id(torrent_id).await?;

let torrent_files = self.get_torrent_files_from_id(torrent_id).await?;

let torrent_announce_urls = self.get_torrent_announce_urls_from_id(torrent_id).await?;

Ok(Torrent::from_db_info_files_and_announce_urls(
torrent_info,
torrent_files,
torrent_announce_urls,
))
}

/// Get torrent's info as `DbTorrentInfo` from `torrent_id`.
async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, DatabaseError>;

/// Get torrent's info as `DbTorrentInfo` from torrent `InfoHash`.
async fn get_torrent_info_from_infohash(&self, info_hash: InfoHash) -> Result<DbTorrentInfo, DatabaseError>;

/// Get all torrent's files as `Vec<TorrentFile>` from `torrent_id`.
async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result<Vec<TorrentFile>, DatabaseError>;

Expand Down
27 changes: 12 additions & 15 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sqlx::mysql::MySqlPoolOptions;
use sqlx::{query, query_as, Acquire, MySqlPool};

use crate::databases::database::{Category, Database, DatabaseDriver, DatabaseError, Sorting, TorrentCompact};
use crate::models::info_hash::InfoHash;
use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentAnnounceUrl, DbTorrentFile, DbTorrentInfo, Torrent, TorrentFile};
Expand Down Expand Up @@ -528,30 +529,26 @@ impl Database for MysqlDatabase {
}
}

async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, DatabaseError> {
let torrent_info = self.get_torrent_info_from_id(torrent_id).await?;

let torrent_files = self.get_torrent_files_from_id(torrent_id).await?;

let torrent_announce_urls = self.get_torrent_announce_urls_from_id(torrent_id).await?;

Ok(Torrent::from_db_info_files_and_announce_urls(
torrent_info,
torrent_files,
torrent_announce_urls,
))
}

async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, DatabaseError> {
query_as::<_, DbTorrentInfo>(
"SELECT name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
)
.bind(torrent_id)
.fetch_one(&self.pool)
.await
.map_err(|_| DatabaseError::TorrentNotFound)
}

async fn get_torrent_info_from_infohash(&self, info_hash: InfoHash) -> Result<DbTorrentInfo, DatabaseError> {
query_as::<_, DbTorrentInfo>(
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
)
.bind(info_hash.to_string())
.fetch_one(&self.pool)
.await
.map_err(|_| DatabaseError::TorrentNotFound)
}

async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result<Vec<TorrentFile>, DatabaseError> {
let db_torrent_files =
query_as::<_, DbTorrentFile>("SELECT md5sum, length, path FROM torrust_torrent_files WHERE torrent_id = ?")
Expand Down
27 changes: 12 additions & 15 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sqlx::sqlite::SqlitePoolOptions;
use sqlx::{query, query_as, Acquire, SqlitePool};

use crate::databases::database::{Category, Database, DatabaseDriver, DatabaseError, Sorting, TorrentCompact};
use crate::models::info_hash::InfoHash;
use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentAnnounceUrl, DbTorrentFile, DbTorrentInfo, Torrent, TorrentFile};
Expand Down Expand Up @@ -523,30 +524,26 @@ impl Database for SqliteDatabase {
}
}

async fn get_torrent_from_id(&self, torrent_id: i64) -> Result<Torrent, DatabaseError> {
let torrent_info = self.get_torrent_info_from_id(torrent_id).await?;

let torrent_files = self.get_torrent_files_from_id(torrent_id).await?;

let torrent_announce_urls = self.get_torrent_announce_urls_from_id(torrent_id).await?;

Ok(Torrent::from_db_info_files_and_announce_urls(
torrent_info,
torrent_files,
torrent_announce_urls,
))
}

async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, DatabaseError> {
query_as::<_, DbTorrentInfo>(
"SELECT name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE torrent_id = ?",
)
.bind(torrent_id)
.fetch_one(&self.pool)
.await
.map_err(|_| DatabaseError::TorrentNotFound)
}

async fn get_torrent_info_from_infohash(&self, info_hash: InfoHash) -> Result<DbTorrentInfo, DatabaseError> {
query_as::<_, DbTorrentInfo>(
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?",
)
.bind(info_hash.to_string().to_uppercase()) // info_hash is stored as uppercase
.fetch_one(&self.pool)
.await
.map_err(|_| DatabaseError::TorrentNotFound)
}

async fn get_torrent_files_from_id(&self, torrent_id: i64) -> Result<Vec<TorrentFile>, DatabaseError> {
let db_torrent_files =
query_as::<_, DbTorrentFile>("SELECT md5sum, length, path FROM torrust_torrent_files WHERE torrent_id = ?")
Expand Down
Loading

0 comments on commit 16bd04c

Please sign in to comment.