Skip to content

Commit

Permalink
chore: normalize infohash -> info_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 16, 2023
1 parent 25016e0 commit d1a3077
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 53 deletions.
8 changes: 4 additions & 4 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ pub trait Database: Sync + Send {
) -> Result<i64, Error>;

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

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

Expand Down Expand Up @@ -205,7 +205,7 @@ pub trait Database: Sync + Send {
async fn get_torrent_info_from_id(&self, torrent_id: i64) -> Result<DbTorrentInfo, Error>;

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

/// 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>, Error>;
Expand All @@ -217,7 +217,7 @@ pub trait Database: Sync + Send {
async fn get_torrent_listing_from_id(&self, torrent_id: i64) -> Result<TorrentListing, Error>;

/// Get `TorrentListing` from `InfoHash`.
async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result<TorrentListing, Error>;
async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result<TorrentListing, Error>;

/// Get all torrents as `Vec<TorrentCompact>`.
async fn get_all_torrents_compact(&self) -> Result<Vec<TorrentCompact>, Error>;
Expand Down
8 changes: 4 additions & 4 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,11 @@ impl Database for Mysql {
.map_err(|_| database::Error::TorrentNotFound)
}

async fn get_torrent_info_from_infohash(&self, infohash: &InfoHash) -> Result<DbTorrentInfo, database::Error> {
async fn get_torrent_info_from_info_hash(&self, info_hash: &InfoHash) -> Result<DbTorrentInfo, database::Error> {
query_as::<_, DbTorrentInfo>(
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?",
)
.bind(infohash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down Expand Up @@ -603,7 +603,7 @@ impl Database for Mysql {
.map_err(|_| database::Error::TorrentNotFound)
}

async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result<TorrentListing, database::Error> {
async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result<TorrentListing, database::Error> {
query_as::<_, TorrentListing>(
"SELECT tt.torrent_id, tp.username AS uploader, tt.info_hash, ti.title, ti.description, tt.category_id, DATE_FORMAT(tt.date_uploaded, '%Y-%m-%d %H:%i:%s') AS date_uploaded, tt.size AS file_size,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
Expand All @@ -615,7 +615,7 @@ impl Database for Mysql {
WHERE tt.info_hash = ?
GROUP BY torrent_id"
)
.bind(infohash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down
8 changes: 4 additions & 4 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ impl Database for Sqlite {
.map_err(|_| database::Error::TorrentNotFound)
}

async fn get_torrent_info_from_infohash(&self, infohash: &InfoHash) -> Result<DbTorrentInfo, database::Error> {
async fn get_torrent_info_from_info_hash(&self, info_hash: &InfoHash) -> Result<DbTorrentInfo, database::Error> {
query_as::<_, DbTorrentInfo>(
"SELECT torrent_id, info_hash, name, pieces, piece_length, private, root_hash FROM torrust_torrents WHERE info_hash = ?",
)
.bind(infohash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.bind(info_hash.to_hex_string().to_uppercase()) // `info_hash` is stored as uppercase hex string
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down Expand Up @@ -593,7 +593,7 @@ impl Database for Sqlite {
.map_err(|_| database::Error::TorrentNotFound)
}

async fn get_torrent_listing_from_infohash(&self, infohash: &InfoHash) -> Result<TorrentListing, database::Error> {
async fn get_torrent_listing_from_info_hash(&self, info_hash: &InfoHash) -> Result<TorrentListing, database::Error> {
query_as::<_, TorrentListing>(
"SELECT tt.torrent_id, tp.username AS uploader, tt.info_hash, ti.title, ti.description, tt.category_id, tt.date_uploaded, tt.size AS file_size,
CAST(COALESCE(sum(ts.seeders),0) as signed) as seeders,
Expand All @@ -605,7 +605,7 @@ impl Database for Sqlite {
WHERE tt.info_hash = ?
GROUP BY ts.torrent_id"
)
.bind(infohash.to_string().to_uppercase()) // `info_hash` is stored as uppercase
.bind(info_hash.to_string().to_uppercase()) // `info_hash` is stored as uppercase
.fetch_one(&self.pool)
.await
.map_err(|_| database::Error::TorrentNotFound)
Expand Down
14 changes: 7 additions & 7 deletions src/models/info_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//! See [BEP 3. The `BitTorrent` Protocol Specification](https://www.bittorrent.org/beps/bep_0003.html)
//! for the official specification.
//!
//! This modules provides a type that can be used to represent infohashes.
//! This modules provides a type that can be used to represent info-hashes.
//!
//! > **NOTICE**: It only supports Info Hash v1.
//!
//! Typically infohashes are represented as hex strings, but internally they are
//! Typically info-hashes are represented as hex strings, but internally they are
//! a 20-byte array.
//!
//! # Calculating the info-hash of a torrent file
Expand Down Expand Up @@ -109,7 +109,7 @@
//! }
//! ```
//!
//! The infohash is the [SHA1](https://en.wikipedia.org/wiki/SHA-1) hash
//! The info-hash is the [SHA1](https://en.wikipedia.org/wiki/SHA-1) hash
//! of the `info` attribute. That is, the SHA1 hash of:
//!
//! ```text
Expand Down Expand Up @@ -217,14 +217,14 @@ impl std::convert::From<[u8; 20]> for InfoHash {
/// Errors that can occur when converting from a `Vec<u8>` to an `InfoHash`.
#[derive(Error, Debug)]
pub enum ConversionError {
/// Not enough bytes for infohash. An infohash is 20 bytes.
#[error("not enough bytes for infohash: {message} {location}")]
/// Not enough bytes for info-hash. An info-hash is 20 bytes.
#[error("not enough bytes for info-hash: {message} {location}")]
NotEnoughBytes {
location: &'static Location<'static>,
message: String,
},
/// Too many bytes for infohash. An infohash is 20 bytes.
#[error("too many bytes for infohash: {message} {location}")]
/// Too many bytes for info-hash. An info-hash is 20 bytes.
#[error("too many bytes for info-hash: {message} {location}")]
TooManyBytes {
location: &'static Location<'static>,
message: String,
Expand Down
22 changes: 11 additions & 11 deletions src/routes/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ pub async fn upload(req: HttpRequest, payload: Multipart, app_data: WebAppData)
///
/// # Errors
///
/// Returns `ServiceError::BadRequest` if the torrent infohash is invalid.
/// Returns `ServiceError::BadRequest` if the torrent info-hash is invalid.
pub async fn download_torrent_handler(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl Responder> {
let info_hash = get_torrent_infohash_from_request(&req)?;
let info_hash = get_torrent_info_hash_from_request(&req)?;

// optional
let user = app_data.auth.get_user_compact_from_request(&req).await;

let mut torrent = app_data.database.get_torrent_from_infohash(&info_hash).await?;
let mut torrent = app_data.database.get_torrent_from_info_hash(&info_hash).await?;

let settings = app_data.cfg.settings.read().await;

Expand Down Expand Up @@ -199,9 +199,9 @@ pub async fn get(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl R

let settings = app_data.cfg.settings.read().await;

let infohash = get_torrent_infohash_from_request(&req)?;
let info_hash = get_torrent_info_hash_from_request(&req)?;

let torrent_listing = app_data.database.get_torrent_listing_from_infohash(&infohash).await?;
let torrent_listing = app_data.database.get_torrent_listing_from_info_hash(&info_hash).await?;

let torrent_id = torrent_listing.torrent_id;

Expand All @@ -218,7 +218,7 @@ pub async fn get(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl R
torrent_response.files = app_data.database.get_torrent_files_from_id(torrent_id).await?;

if torrent_response.files.len() == 1 {
let torrent_info = app_data.database.get_torrent_info_from_infohash(&infohash).await?;
let torrent_info = app_data.database.get_torrent_info_from_info_hash(&info_hash).await?;

torrent_response
.files
Expand Down Expand Up @@ -290,9 +290,9 @@ pub async fn get(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl R
pub async fn update(req: HttpRequest, payload: web::Json<Update>, app_data: WebAppData) -> ServiceResult<impl Responder> {
let user = app_data.auth.get_user_compact_from_request(&req).await?;

let infohash = get_torrent_infohash_from_request(&req)?;
let info_hash = get_torrent_info_hash_from_request(&req)?;

let torrent_listing = app_data.database.get_torrent_listing_from_infohash(&infohash).await?;
let torrent_listing = app_data.database.get_torrent_listing_from_info_hash(&info_hash).await?;

// check if user is owner or administrator
if torrent_listing.uploader != user.username && !user.administrator {
Expand Down Expand Up @@ -341,10 +341,10 @@ pub async fn delete(req: HttpRequest, app_data: WebAppData) -> ServiceResult<imp
return Err(ServiceError::Unauthorized);
}

let infohash = get_torrent_infohash_from_request(&req)?;
let info_hash = get_torrent_info_hash_from_request(&req)?;

// needed later for removing torrent from tracker whitelist
let torrent_listing = app_data.database.get_torrent_listing_from_infohash(&infohash).await?;
let torrent_listing = app_data.database.get_torrent_listing_from_info_hash(&info_hash).await?;

app_data.database.delete_torrent(torrent_listing.torrent_id).await?;

Expand Down Expand Up @@ -397,7 +397,7 @@ pub async fn get_torrents_handler(params: Query<Search>, app_data: WebAppData) -
Ok(HttpResponse::Ok().json(OkResponse { data: torrents_response }))
}

fn get_torrent_infohash_from_request(req: &HttpRequest) -> Result<InfoHash, ServiceError> {
fn get_torrent_info_hash_from_request(req: &HttpRequest) -> Result<InfoHash, ServiceError> {
match req.match_info().get("info_hash") {
None => Err(ServiceError::BadRequest),
Some(info_hash) => match InfoHash::from_str(info_hash) {
Expand Down
12 changes: 6 additions & 6 deletions tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ impl Client {
self.http_client.get("torrents", params).await
}

pub async fn get_torrent(&self, infohash: &InfoHash) -> TextResponse {
self.http_client.get(&format!("torrent/{infohash}"), Query::empty()).await
pub async fn get_torrent(&self, info_hash: &InfoHash) -> TextResponse {
self.http_client.get(&format!("torrent/{info_hash}"), Query::empty()).await
}

pub async fn delete_torrent(&self, infohash: &InfoHash) -> TextResponse {
self.http_client.delete(&format!("torrent/{infohash}")).await
pub async fn delete_torrent(&self, info_hash: &InfoHash) -> TextResponse {
self.http_client.delete(&format!("torrent/{info_hash}")).await
}

pub async fn update_torrent(&self, infohash: &InfoHash, update_torrent_form: UpdateTorrentFrom) -> TextResponse {
pub async fn update_torrent(&self, info_hash: &InfoHash, update_torrent_form: UpdateTorrentFrom) -> TextResponse {
self.http_client
.put(&format!("torrent/{infohash}"), &update_torrent_form)
.put(&format!("torrent/{info_hash}"), &update_torrent_form)
.await
}

Expand Down
2 changes: 1 addition & 1 deletion tests/common/contexts/torrent/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl TestTorrent {
}
}

pub fn infohash(&self) -> InfoHash {
pub fn info_hash(&self) -> InfoHash {
self.file_info.info_hash.clone()
}
}
Expand Down
32 changes: 16 additions & 16 deletions tests/e2e/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod for_guests {
}

#[tokio::test]
async fn it_should_allow_guests_to_get_torrent_details_searching_by_infohash() {
async fn it_should_allow_guests_to_get_torrent_details_searching_by_info_hash() {
let mut env = TestEnv::new();
env.start().await;

Expand All @@ -164,7 +164,7 @@ mod for_guests {
let uploader = new_logged_in_user(&env).await;
let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;

let response = client.get_torrent(&test_torrent.infohash()).await;
let response = client.get_torrent(&test_torrent.info_hash()).await;

let torrent_details_response: TorrentDetailsResponse = serde_json::from_str(&response.body).unwrap();

Expand Down Expand Up @@ -213,7 +213,7 @@ mod for_guests {
}

#[tokio::test]
async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_infohash() {
async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_info_hash() {
let mut env = TestEnv::new();
env.start().await;

Expand All @@ -227,7 +227,7 @@ mod for_guests {
let uploader = new_logged_in_user(&env).await;
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;

let response = client.download_torrent(&test_torrent.infohash()).await;
let response = client.download_torrent(&test_torrent.info_hash()).await;

let torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent");
let uploaded_torrent =
Expand Down Expand Up @@ -272,7 +272,7 @@ mod for_guests {
let uploader = new_logged_in_user(&env).await;
let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;

let response = client.delete_torrent(&test_torrent.infohash()).await;
let response = client.delete_torrent(&test_torrent.info_hash()).await;

assert_eq!(response.status, 401);
}
Expand Down Expand Up @@ -305,7 +305,7 @@ mod for_authenticated_users {
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token);

let test_torrent = random_torrent();
let infohash = test_torrent.infohash().clone();
let info_hash = test_torrent.info_hash().clone();

let form: UploadTorrentMultipartForm = test_torrent.index_info.into();

Expand All @@ -315,7 +315,7 @@ mod for_authenticated_users {

assert_eq!(
uploaded_torrent_response.data.info_hash.to_lowercase(),
infohash.to_lowercase()
info_hash.to_lowercase()
);
assert!(response.is_json_and_ok());
}
Expand Down Expand Up @@ -369,7 +369,7 @@ mod for_authenticated_users {
}

#[tokio::test]
async fn it_should_not_allow_uploading_a_torrent_with_a_infohash_that_already_exists() {
async fn it_should_not_allow_uploading_a_torrent_with_a_info_hash_that_already_exists() {
let mut env = TestEnv::new();
env.start().await;

Expand All @@ -388,7 +388,7 @@ mod for_authenticated_users {
let form: UploadTorrentMultipartForm = first_torrent.index_info.into();
let _response = client.upload_torrent(form.into()).await;

// Upload the second torrent with the same infohash as the first one.
// Upload the second torrent with the same info-hash as the first one.
// We need to change the title otherwise the torrent will be rejected
// because of the duplicate title.
first_torrent_clone.index_info.title = format!("{first_torrent_title}-clone");
Expand Down Expand Up @@ -417,7 +417,7 @@ mod for_authenticated_users {
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &downloader.token);

// When the user downloads the torrent
let response = client.download_torrent(&test_torrent.infohash()).await;
let response = client.download_torrent(&test_torrent.info_hash()).await;

let torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent");

Expand Down Expand Up @@ -456,7 +456,7 @@ mod for_authenticated_users {

let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token);

let response = client.delete_torrent(&test_torrent.infohash()).await;
let response = client.delete_torrent(&test_torrent.info_hash()).await;

assert_eq!(response.status, 403);
}
Expand Down Expand Up @@ -484,7 +484,7 @@ mod for_authenticated_users {

let response = client
.update_torrent(
&test_torrent.infohash(),
&test_torrent.info_hash(),
UpdateTorrentFrom {
title: Some(new_title.clone()),
description: Some(new_description.clone()),
Expand Down Expand Up @@ -524,7 +524,7 @@ mod for_authenticated_users {

let response = client
.update_torrent(
&test_torrent.infohash(),
&test_torrent.info_hash(),
UpdateTorrentFrom {
title: Some(new_title.clone()),
description: Some(new_description.clone()),
Expand All @@ -551,7 +551,7 @@ mod for_authenticated_users {
use crate::e2e::environment::TestEnv;

#[tokio::test]
async fn it_should_allow_admins_to_delete_torrents_searching_by_infohash() {
async fn it_should_allow_admins_to_delete_torrents_searching_by_info_hash() {
let mut env = TestEnv::new();
env.start().await;

Expand All @@ -566,7 +566,7 @@ mod for_authenticated_users {
let admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &admin.token);

let response = client.delete_torrent(&test_torrent.infohash()).await;
let response = client.delete_torrent(&test_torrent.info_hash()).await;

let deleted_torrent_response: DeletedTorrentResponse = serde_json::from_str(&response.body).unwrap();

Expand Down Expand Up @@ -595,7 +595,7 @@ mod for_authenticated_users {

let response = client
.update_torrent(
&test_torrent.infohash(),
&test_torrent.info_hash(),
UpdateTorrentFrom {
title: Some(new_title.clone()),
description: Some(new_description.clone()),
Expand Down

0 comments on commit d1a3077

Please sign in to comment.