Skip to content

Commit

Permalink
dev: fix clippy warnings for: src/routes/torrent.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed May 10, 2023
1 parent c20608f commit 33c3218
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/models/torrent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::models::torrent_file::Torrent;
use crate::routes::torrent::CreateTorrent;
use crate::routes::torrent::Create;

#[allow(clippy::module_name_repetitions)]
#[allow(dead_code)]
Expand All @@ -22,6 +22,6 @@ pub struct TorrentListing {
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct TorrentRequest {
pub fields: CreateTorrent,
pub fields: Create,
pub torrent: Torrent,
}
85 changes: 61 additions & 24 deletions src/routes/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,47 @@ use crate::AsCSV;
pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/torrent")
.service(web::resource("/upload").route(web::post().to(upload_torrent)))
.service(web::resource("/upload").route(web::post().to(upload)))
.service(web::resource("/download/{info_hash}").route(web::get().to(download_torrent_handler)))
.service(
web::resource("/{info_hash}")
.route(web::get().to(get_torrent_handler))
.route(web::put().to(update_torrent_handler))
.route(web::delete().to(delete_torrent_handler)),
.route(web::get().to(get))
.route(web::put().to(update))
.route(web::delete().to(delete)),
),
);
cfg.service(web::scope("/torrents").service(web::resource("").route(web::get().to(get_torrents_handler))));
}

#[derive(FromRow)]
pub struct TorrentCount {
pub struct Count {
pub count: i32,
}

#[derive(Debug, Deserialize)]
pub struct CreateTorrent {
pub struct Create {
pub title: String,
pub description: String,
pub category: String,
}

impl CreateTorrent {
impl Create {
/// Returns the verify of this [`Create`].
///
/// # Errors
///
/// This function will return an `BadRequest` error if the `title` or the `category` is empty.
pub fn verify(&self) -> Result<(), ServiceError> {
if !self.title.is_empty() && !self.category.is_empty() {
return Ok(());
if self.title.is_empty() || self.category.is_empty() {
Err(ServiceError::BadRequest)
} else {
Ok(())
}

Err(ServiceError::BadRequest)
}
}

#[derive(Debug, Deserialize)]
pub struct TorrentSearch {
pub struct Search {
page_size: Option<u8>,
page: Option<u32>,
sort: Option<Sorting>,
Expand All @@ -65,12 +70,21 @@ pub struct TorrentSearch {
}

#[derive(Debug, Deserialize)]
pub struct TorrentUpdate {
pub struct Update {
title: Option<String>,
description: Option<String>,
}

pub async fn upload_torrent(req: HttpRequest, payload: Multipart, app_data: WebAppData) -> ServiceResult<impl Responder> {
/// Upload a Torrent to the Index
///
/// # Errors
///
/// This function will return an error if unable to get the user from the database.
/// This function will return an error if unable to get torrent request from payload.
/// This function will return an error if unable to get the category from the database.
/// This function will return an error if unable to insert the torrent into the database.
/// This function will return an error if unable to add the torrent to the whitelist.
pub async fn upload(req: HttpRequest, payload: Multipart, app_data: WebAppData) -> ServiceResult<impl Responder> {
let user = app_data.auth.get_user_compact_from_request(&req).await?;

// get torrent and fields from request
Expand Down Expand Up @@ -166,7 +180,17 @@ pub async fn download_torrent_handler(req: HttpRequest, app_data: WebAppData) ->
Ok(HttpResponse::Ok().content_type("application/x-bittorrent").body(buffer))
}

pub async fn get_torrent_handler(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl Responder> {
/// Get Torrent from the Index
///
/// # Errors
///
/// This function will return an error if unable to get torrent ID.
/// This function will return an error if unable to get torrent listing from id.
/// This function will return an error if unable to get torrent category from id.
/// This function will return an error if unable to get torrent files from id.
/// This function will return an error if unable to get torrent info from id.
/// This function will return an error if unable to get torrent announce url(s) from id.
pub async fn get(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl Responder> {
// optional
let user = app_data.auth.get_user_compact_from_request(&req).await;

Expand Down Expand Up @@ -251,11 +275,16 @@ pub async fn get_torrent_handler(req: HttpRequest, app_data: WebAppData) -> Serv
Ok(HttpResponse::Ok().json(OkResponse { data: torrent_response }))
}

pub async fn update_torrent_handler(
req: HttpRequest,
payload: web::Json<TorrentUpdate>,
app_data: WebAppData,
) -> ServiceResult<impl Responder> {
/// Update a Torrent in the Index
///
/// # Errors
///
/// This function will return an error if unable to get user.
/// This function will return an error if unable to get torrent id from request.
/// This function will return an error if unable to get listing from id.
/// This function will return an `ServiceError::Unauthorized` if user is not a owner or an administrator.
/// This function will return an error if unable to update the torrent tile or description.
pub async fn update(req: HttpRequest, payload: web::Json<Update>, app_data: WebAppData) -> ServiceResult<impl Responder> {
let user = app_data.auth.get_user_compact_from_request(&req).await?;

let infohash = get_torrent_infohash_from_request(&req)?;
Expand Down Expand Up @@ -293,7 +322,15 @@ pub async fn update_torrent_handler(
Ok(HttpResponse::Ok().json(OkResponse { data: torrent_response }))
}

pub async fn delete_torrent_handler(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl Responder> {
/// Delete a Torrent from the Index
///
/// # Errors
///
/// This function will return an error if unable to get the user.
/// This function will return an `ServiceError::Unauthorized` if the user is not an administrator.
/// This function will return an error if unable to get the torrent listing from it's ID.
/// This function will return an error if unable to delete the torrent from the database.
pub async fn delete(req: HttpRequest, app_data: WebAppData) -> ServiceResult<impl Responder> {
let user = app_data.auth.get_user_compact_from_request(&req).await?;

// check if user is administrator
Expand Down Expand Up @@ -327,7 +364,7 @@ pub async fn delete_torrent_handler(req: HttpRequest, app_data: WebAppData) -> S
/// # Errors
///
/// Returns a `ServiceError::DatabaseError` if the database query fails.
pub async fn get_torrents_handler(params: Query<TorrentSearch>, app_data: WebAppData) -> ServiceResult<impl Responder> {
pub async fn get_torrents_handler(params: Query<Search>, app_data: WebAppData) -> ServiceResult<impl Responder> {
let settings = app_data.cfg.settings.read().await;

let sort = params.sort.unwrap_or(Sorting::UploadedDesc);
Expand Down Expand Up @@ -405,15 +442,15 @@ async fn get_torrent_request_from_payload(mut payload: Multipart) -> Result<Torr
}
}

let fields = CreateTorrent {
let fields = Create {
title,
description,
category,
};

fields.verify()?;

let position = torrent_cursor.position() as usize;
let position = usize::try_from(torrent_cursor.position()).map_err(|_| ServiceError::InvalidTorrentFile)?;
let inner = torrent_cursor.get_ref();

let torrent = parse_torrent::decode_torrent(&inner[..position]).map_err(|_| ServiceError::InvalidTorrentFile)?;
Expand Down

0 comments on commit 33c3218

Please sign in to comment.