Skip to content

Commit

Permalink
refactor: [#276] extract fn Torrent::canonical_info_hash_group_checks
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Sep 20, 2023
1 parent bbadd59 commit 6a75c54
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,9 @@ impl Index {

let canonical_info_hash = torrent.canonical_info_hash();

// Canonical InfoHash Group checks

let original_info_hashes = self
.torrent_info_hash_repository
.get_canonical_info_hash_group(&canonical_info_hash)
self.canonical_info_hash_group_checks(&original_info_hash, &canonical_info_hash)
.await?;

if !original_info_hashes.is_empty() {
// Torrent with the same canonical infohash was already uploaded
debug!("Canonical infohash found: {:?}", canonical_info_hash.to_hex_string());

if let Some(original_info_hash) = original_info_hashes.find(&original_info_hash) {
// The exact original infohash was already uploaded
debug!("Original infohash found: {:?}", original_info_hash.to_hex_string());

return Err(ServiceError::InfoHashAlreadyExists);
}

// A new original infohash is being uploaded with a canonical infohash that already exists.
debug!("Original infohash not found: {:?}", original_info_hash.to_hex_string());

// Add the new associated original infohash to the canonical one.
self.torrent_info_hash_repository
.add_info_hash_to_canonical_info_hash_group(&original_info_hash, &canonical_info_hash)
.await?;
return Err(ServiceError::CanonicalInfoHashAlreadyExists);
}

// Store the torrent into the database

let torrent_id = self
Expand Down Expand Up @@ -231,6 +206,40 @@ impl Index {
Ok(metadata)
}

async fn canonical_info_hash_group_checks(
&self,
original_info_hash: &InfoHash,
canonical_info_hash: &InfoHash,
) -> Result<(), ServiceError> {
let original_info_hashes = self
.torrent_info_hash_repository
.get_canonical_info_hash_group(canonical_info_hash)
.await?;

if !original_info_hashes.is_empty() {
// Torrent with the same canonical infohash was already uploaded
debug!("Canonical infohash found: {:?}", canonical_info_hash.to_hex_string());

if let Some(original_info_hash) = original_info_hashes.find(original_info_hash) {
// The exact original infohash was already uploaded
debug!("Original infohash found: {:?}", original_info_hash.to_hex_string());

return Err(ServiceError::InfoHashAlreadyExists);
}

// A new original infohash is being uploaded with a canonical infohash that already exists.
debug!("Original infohash not found: {:?}", original_info_hash.to_hex_string());

// Add the new associated original infohash to the canonical one.
self.torrent_info_hash_repository
.add_info_hash_to_canonical_info_hash_group(original_info_hash, canonical_info_hash)
.await?;
return Err(ServiceError::CanonicalInfoHashAlreadyExists);
}

Ok(())
}

async fn customize_announcement_info_for(&self, torrent: &mut Torrent) {
let settings = self.configuration.settings.read().await;
let tracker_url = settings.tracker.url.clone();
Expand Down

0 comments on commit 6a75c54

Please sign in to comment.