Skip to content

Commit

Permalink
refactor: [#290] specific error for suplidatetorrent with a original …
Browse files Browse the repository at this point in the history
…infohashes
  • Loading branch information
josecelano committed Mar 5, 2024
1 parent 61ce771 commit d500634
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ pub enum ServiceError {
#[display(fmt = "A torrent with the same canonical infohash already exists in our database.")]
CanonicalInfoHashAlreadyExists,

#[display(fmt = "A torrent with the same original infohash already exists in our database.")]
OriginalInfoHashAlreadyExists,

#[display(fmt = "This torrent title has already been used.")]
TorrentTitleAlreadyExists,

Expand Down Expand Up @@ -297,6 +300,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
ServiceError::Unauthorized => StatusCode::FORBIDDEN,
ServiceError::InfoHashAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::CanonicalInfoHashAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::OriginalInfoHashAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::TorrentTitleAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::TrackerOffline => StatusCode::SERVICE_UNAVAILABLE,
ServiceError::CategoryNameEmpty => StatusCode::BAD_REQUEST,
Expand Down
5 changes: 4 additions & 1 deletion src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ impl Index {
.await?;

if !original_info_hashes.is_empty() {
// A previous torrent with the same canonical infohash has been uploaded before

// 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);
return Err(ServiceError::OriginalInfoHashAlreadyExists);
}

// A new original infohash is being uploaded with a canonical infohash that already exists.
Expand All @@ -229,6 +231,7 @@ impl Index {
return Err(ServiceError::CanonicalInfoHashAlreadyExists);
}

// No other torrent with the same canonical infohash has been uploaded before
Ok(())
}

Expand Down

0 comments on commit d500634

Please sign in to comment.