Skip to content

Commit

Permalink
Merge torrust#519: Refactor error handling for torrent upload
Browse files Browse the repository at this point in the history
414d468 refactor: [torrust#290] return 4009 intead of 400 trying to upload duplicate torrents (Jose Celano)
d500634 refactor: [torrust#290] specific error for suplidatetorrent with a original infohashes (Jose Celano)

Pull request description:

  - [x] Return a specific error when a user tries to upload a torrent with an original infohash that already exists.
  - [x] Return 409 when a user tries to upload a torrent whose canonical or original infohash already exists (instead of 400).

ACKs for top commit:
  josecelano:
    ACK 414d468

Tree-SHA512: f918d65915212d470543ea9ebf9d85536670422aeac658bd58a9f6d488eb35ebcda3fbf3792b36a989ebc8a6c09163cc519cb77383baf3ebc9affa6565e1f967
  • Loading branch information
josecelano committed Mar 5, 2024
2 parents 61ce771 + 414d468 commit f5cfc33
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion 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 @@ -296,7 +299,8 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
ServiceError::InvalidTag => StatusCode::BAD_REQUEST,
ServiceError::Unauthorized => StatusCode::FORBIDDEN,
ServiceError::InfoHashAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::CanonicalInfoHashAlreadyExists => StatusCode::BAD_REQUEST,
ServiceError::CanonicalInfoHashAlreadyExists => StatusCode::CONFLICT,
ServiceError::OriginalInfoHashAlreadyExists => StatusCode::CONFLICT,
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
4 changes: 2 additions & 2 deletions tests/e2e/web/api/v1/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ mod for_authenticated_users {
let form: UploadTorrentMultipartForm = first_torrent_clone.index_info.into();
let response = client.upload_torrent(form.into()).await;

assert_eq!(response.status, 400);
assert_eq!(response.status, 409);
}

#[tokio::test]
Expand Down Expand Up @@ -761,7 +761,7 @@ mod for_authenticated_users {
let form: UploadTorrentMultipartForm = torrent_with_the_same_canonical_info_hash.index_info.into();
let response = client.upload_torrent(form.into()).await;

assert_eq!(response.status, 400);
assert_eq!(response.status, 409);
}
}

Expand Down

0 comments on commit f5cfc33

Please sign in to comment.