Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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