From bc191587478ad3dcab53c1de46676a63de6194e8 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Wed, 2 Aug 2023 22:41:23 +0200 Subject: [PATCH] refactor(server): clean up some stuff --- server/src/api/pagination.rs | 2 - server/src/error.rs | 2 - server/src/repo/mod.rs | 77 +++++++++++++++++++----------------- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/server/src/api/pagination.rs b/server/src/api/pagination.rs index ae3812d..3904ea4 100644 --- a/server/src/api/pagination.rs +++ b/server/src/api/pagination.rs @@ -1,5 +1,3 @@ -use axum::response::{IntoResponse, Response}; -use axum::Json; use serde::{Deserialize, Serialize}; pub const DEFAULT_PAGE: u64 = 0; diff --git a/server/src/error.rs b/server/src/error.rs index 1299d2f..4fbb7c4 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -21,7 +21,6 @@ impl fmt::Display for ServerError { ServerError::Axum(err) => write!(fmt, "{}", err), ServerError::Status(status) => write!(fmt, "{}", status), ServerError::Db(err) => write!(fmt, "{}", err), - ServerError::Status(status) => write!(fmt, "{}", status), } } } @@ -40,7 +39,6 @@ impl IntoResponse for ServerError { StatusCode::NOT_FOUND.into_response() } ServerError::Db(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(), - ServerError::Status(status) => status.into_response(), } } } diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index b2942a4..0a28969 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -3,8 +3,8 @@ mod package; pub use manager::RepoGroupManager; -use axum::body::Body; use crate::db::entities::{package as db_package, repo as db_repo}; +use axum::body::Body; use axum::extract::{BodyStream, Path, State}; use axum::http::Request; use axum::http::StatusCode; @@ -47,46 +47,51 @@ async fn post_package_archive( let path_clone = path.clone(); let repo_clone = repo.clone(); let res = tokio::task::spawn_blocking(move || { - clone.write().unwrap().add_pkg_from_path(&repo_clone, &path_clone) + clone + .write() + .unwrap() + .add_pkg_from_path(&repo_clone, &path_clone) }) .await?; - // Remove the downloaded file if the adding failed - if let Err(err) = res { - let _ = tokio::fs::remove_file(path).await; + match res { + // Insert the newly added package into the database + Ok(pkg) => { + // Query the repo for its ID, or create it if it does not already exist + let repo_entity = db_repo::Entity::find() + .filter(db_repo::Column::Name.eq(&repo)) + .one(&global.db) + .await?; - return Err(err.into()); + let repo_id = if let Some(repo_entity) = repo_entity { + repo_entity.id + } else { + let model = db_repo::ActiveModel { + name: sea_orm::Set(repo.clone()), + ..Default::default() + }; + + db_repo::Entity::insert(model) + .exec(&global.db) + .await? + .last_insert_id + }; + + // Insert the package's data into the database + let mut model: db_package::ActiveModel = pkg.into(); + model.repo_id = sea_orm::Set(repo_id); + + model.insert(&global.db).await?; + + Ok(()) + } + // Remove the uploaded file and return the error + Err(err) => { + tokio::fs::remove_file(path).await?; + + Err(err.into()) + } } - - let pkg = res.unwrap(); - - // Query the repo for its ID, or create it if it does not already exist - let repo_entity = db_repo::Entity::find() - .filter(db_repo::Column::Name.eq(&repo)) - .one(&global.db) - .await?; - - let repo_id = if let Some(repo_entity) = repo_entity { - repo_entity.id - } else { - let model = db_repo::ActiveModel { - name: sea_orm::Set(repo.clone()), - ..Default::default() - }; - - db_repo::Entity::insert(model) - .exec(&global.db) - .await? - .last_insert_id - }; - - // Insert the package's data into the database - let mut model: db_package::ActiveModel = pkg.into(); - model.repo_id = sea_orm::Set(repo_id); - - model.insert(&global.db).await?; - - Ok(()) } /// Serve the package archive files and database archives. If files are requested for an