Skip to content

Commit

Permalink
Merge torrust#239: Add minimum length in Torrent Title
Browse files Browse the repository at this point in the history
f739657 torrust#72: Change MIN_TORRENT_TITLE_LENGTH type from u32 to usize for Rust compatibility (Alex Wellnitz)
1828883 torrust#72: Constant added so you can easily adjust the value (Alex Wellnitz)
7db0275 Remove referencing the reference (Alex Wellnitz)
a15af48 torrust#72: Specific error code added (Alex Wellnitz)
946ea97 torrust#72: Add minimum length in Torrent Title (Alex Wellnitz)

Pull request description:

  - Torrent title is checked that the title has at least 3 characters
  - If the title is smaller, a service error is issued

ACKs for top commit:
  josecelano:
    ACK f739657

Tree-SHA512: d68bca436dfd721bfabade4d7ba04dc4f56fe20cb87f2253127219f08f4778434f6e39f6847ef0525e2e398a48c75bb352e2dee610479e46f693f2fff7a4b4ff
  • Loading branch information
josecelano committed Jul 31, 2023
2 parents 6bf1b19 + f739657 commit 4415144
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ pub enum ServiceError {
#[display(fmt = "Only .torrent files can be uploaded.")]
InvalidFileType,

#[display(fmt = "Torrent title is too short.")]
InvalidTorrentTitleLength,

#[display(fmt = "Bad request.")]
BadRequest,

Expand Down Expand Up @@ -219,6 +222,7 @@ pub fn http_status_code_for_service_error(error: &ServiceError) -> StatusCode {
ServiceError::InvalidTorrentFile => StatusCode::BAD_REQUEST,
ServiceError::InvalidTorrentPiecesLength => StatusCode::BAD_REQUEST,
ServiceError::InvalidFileType => StatusCode::BAD_REQUEST,
ServiceError::InvalidTorrentTitleLength => StatusCode::BAD_REQUEST,
ServiceError::BadRequest => StatusCode::BAD_REQUEST,
ServiceError::InvalidCategory => StatusCode::BAD_REQUEST,
ServiceError::InvalidTag => StatusCode::BAD_REQUEST,
Expand Down
7 changes: 7 additions & 0 deletions src/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::models::user::UserId;
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::{tracker, AsCSV};

const MIN_TORRENT_TITLE_LENGTH: usize = 3;

pub struct Index {
configuration: Arc<Configuration>,
tracker_statistics_importer: Arc<StatisticsImporter>,
Expand Down Expand Up @@ -98,11 +100,16 @@ impl Index {
/// * Unable to get the category from the database.
/// * Unable to insert the torrent into the database.
/// * Unable to add the torrent to the whitelist.
/// * Torrent title is too short.
pub async fn add_torrent(&self, mut torrent_request: AddTorrentRequest, user_id: UserId) -> Result<TorrentId, ServiceError> {
let _user = self.user_repository.get_compact(&user_id).await?;

torrent_request.torrent.set_announce_urls(&self.configuration).await;

if torrent_request.metadata.title.len() < MIN_TORRENT_TITLE_LENGTH {
return Err(ServiceError::InvalidTorrentTitleLength);
}

let category = self
.category_repository
.get_by_name(&torrent_request.metadata.category)
Expand Down

0 comments on commit 4415144

Please sign in to comment.