From d50063462efdbbf3811396fc9af7df39e2c5ea63 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 5 Mar 2024 17:25:41 +0000 Subject: [PATCH 1/2] refactor: [#290] specific error for suplidatetorrent with a original infohashes --- src/errors.rs | 4 ++++ src/services/torrent.rs | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/errors.rs b/src/errors.rs index 92d5319c..448243c0 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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, @@ -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, diff --git a/src/services/torrent.rs b/src/services/torrent.rs index bc197881..788eec19 100644 --- a/src/services/torrent.rs +++ b/src/services/torrent.rs @@ -209,6 +209,8 @@ 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()); @@ -216,7 +218,7 @@ impl Index { // 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. @@ -229,6 +231,7 @@ impl Index { return Err(ServiceError::CanonicalInfoHashAlreadyExists); } + // No other torrent with the same canonical infohash has been uploaded before Ok(()) } From 414d468df24998c42c4bbdee3500e34813ce223a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 5 Mar 2024 17:33:17 +0000 Subject: [PATCH 2/2] refactor: [#290] return 4009 intead of 400 trying to upload duplicate torrents Torrents with the same original or canonical infohash. --- src/errors.rs | 4 ++-- tests/e2e/web/api/v1/contexts/torrent/contract.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 448243c0..71ff3ab3 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -299,8 +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::OriginalInfoHashAlreadyExists => 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, diff --git a/tests/e2e/web/api/v1/contexts/torrent/contract.rs b/tests/e2e/web/api/v1/contexts/torrent/contract.rs index 4c947924..60614deb 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/contract.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/contract.rs @@ -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] @@ -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); } }