From 3892975fc2b563185526ed6e9b1f9ff3ecb06150 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Wed, 30 Oct 2024 11:08:53 -0500 Subject: [PATCH] Fixed clippy suggestions, ran `cargo fmt` --- src/database.rs | 20 +++++++++--------- src/endpoints.rs | 12 +++++------ src/lib.rs | 55 +++++++++++++++++++++++++++--------------------- src/main.rs | 21 ++++++++++++++---- src/pages.rs | 2 +- src/resources.rs | 18 ++++++++++++---- 6 files changed, 79 insertions(+), 49 deletions(-) diff --git a/src/database.rs b/src/database.rs index 4194121..09df022 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,5 +1,10 @@ use std::{ - collections::{hash_map::Values, HashMap, HashSet}, ffi::OsStr, fs::{self, File}, io, path::{Path, PathBuf}, sync::{Arc, RwLock} + collections::{hash_map::Values, HashMap, HashSet}, + ffi::OsStr, + fs::{self, File}, + io, + path::{Path, PathBuf}, + sync::{Arc, RwLock}, }; use bincode::{config::Configuration, decode_from_std_read, encode_into_std_write, Decode, Encode}; @@ -63,16 +68,13 @@ impl Mochibase { /// Save the database to its file pub fn save(&self) -> Result<(), io::Error> { // Create a file and write the LZ4 compressed stream into it - let file = File::create(&self.path.with_extension("bkp"))?; + let file = File::create(self.path.with_extension("bkp"))?; let mut lz4_file = lz4_flex::frame::FrameEncoder::new(file); encode_into_std_write(self, &mut lz4_file, BINCODE_CFG) .map_err(|e| io::Error::other(format!("failed to save database: {e}")))?; lz4_file.try_finish()?; - fs::rename( - self.path.with_extension("bkp"), - &self.path - ).unwrap(); + fs::rename(self.path.with_extension("bkp"), &self.path).unwrap(); Ok(()) } @@ -283,14 +285,12 @@ pub async fn clean_loop( /// A unique identifier for an entry in the database, 8 characters long, /// consists of ASCII alphanumeric characters (`a-z`, `A-Z`, and `0-9`). -#[derive(Debug, PartialEq, Eq, Clone, Hash)] -#[derive(Decode, Encode)] -#[derive(Deserialize, Serialize)] +#[derive(Debug, PartialEq, Eq, Clone, Hash, Decode, Encode, Deserialize, Serialize)] pub struct Mmid(String); impl Mmid { /// Create a new random MMID - pub fn new() -> Self { + pub fn new_random() -> Self { let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 8); Self(string) diff --git a/src/endpoints.rs b/src/endpoints.rs index 5268086..fba217b 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -1,4 +1,7 @@ -use std::{str::FromStr, sync::{Arc, RwLock}}; +use std::{ + str::FromStr, + sync::{Arc, RwLock}, +}; use rocket::{ get, @@ -11,7 +14,7 @@ use rocket::{ use serde::Serialize; use crate::{ - database::{Mochibase, Mmid, MochiFile}, + database::{Mmid, MochiFile, Mochibase}, settings::Settings, }; @@ -34,10 +37,7 @@ pub fn server_info(settings: &State) -> Json { /// Get information about a file #[get("/info/")] -pub async fn file_info( - db: &State>>, - mmid: &str, -) -> Option> { +pub async fn file_info(db: &State>>, mmid: &str) -> Option> { let mmid: Mmid = mmid.try_into().ok()?; let entry = db.read().unwrap().get(&mmid).cloned()?; diff --git a/src/lib.rs b/src/lib.rs index a81ac69..95bfcbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,23 +1,28 @@ pub mod database; pub mod endpoints; +pub mod pages; +pub mod resources; pub mod settings; pub mod strings; pub mod utils; -pub mod pages; -pub mod resources; use std::sync::{Arc, RwLock}; -use chrono::{DateTime, Utc}; use crate::database::{Mmid, MochiFile, Mochibase}; -use maud::{html, Markup, PreEscaped}; use crate::pages::{footer, head}; -use rocket::{ - data::ToByteUnit, form::Form, fs::TempFile, get, post, serde::{json::Json, Serialize}, FromForm, State -}; use crate::settings::Settings; use crate::strings::{parse_time_string, to_pretty_time}; use crate::utils::hash_file; +use chrono::{DateTime, Utc}; +use maud::{html, Markup, PreEscaped}; +use rocket::{ + data::ToByteUnit, + form::Form, + fs::TempFile, + get, post, + serde::{json::Json, Serialize}, + FromForm, State, +}; use uuid::Uuid; #[get("/")] @@ -97,19 +102,19 @@ pub async fn handle_upload( }; let raw_name = file_data - .file - .raw_name() - .unwrap() - .dangerous_unsafe_unsanitized_raw() - .as_str() - .to_string(); + .file + .raw_name() + .unwrap() + .dangerous_unsafe_unsanitized_raw() + .as_str() + .to_string(); // Get temp path for the file let temp_filename = settings.temp_dir.join(Uuid::new_v4().to_string()); file_data.file.persist_to(&temp_filename).await?; // Get hash and random identifier and expiry - let file_mmid = Mmid::new(); + let file_mmid = Mmid::new_random(); let file_hash = hash_file(&temp_filename).await?; let expiry = current + expire_time; @@ -118,11 +123,11 @@ pub async fn handle_upload( let constructed_file = MochiFile::new( file_mmid.clone(), - raw_name, - file_type.media_type().to_string(), - file_hash, - current, - expiry + raw_name, + file_type.media_type().to_string(), + file_hash, + current, + expiry, ); // If the hash does not exist in the database, @@ -133,15 +138,17 @@ pub async fn handle_upload( std::fs::remove_file(temp_filename)?; } - db.write().unwrap().insert(&file_mmid, constructed_file.clone()); + db.write() + .unwrap() + .insert(&file_mmid, constructed_file.clone()); Ok(Json(ClientResponse { status: true, name: constructed_file.name().clone(), - mmid: Some(constructed_file.mmid().clone()), - hash: constructed_file.hash().to_string(), - expires: Some(constructed_file.expiry()), - ..Default::default() + mmid: Some(constructed_file.mmid().clone()), + hash: constructed_file.hash().to_string(), + expires: Some(constructed_file.expiry()), + ..Default::default() })) } diff --git a/src/main.rs b/src/main.rs index dcf2aff..860239c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,14 @@ -use std::{fs, sync::{Arc, RwLock}}; +use std::{ + fs, + sync::{Arc, RwLock}, +}; use chrono::TimeDelta; -use confetti_box::{database::{clean_loop, Mochibase}, endpoints, pages, resources, settings::Settings}; +use confetti_box::{ + database::{clean_loop, Mochibase}, + endpoints, pages, resources, + settings::Settings, +}; use log::info; use rocket::{data::ToByteUnit as _, routes, tokio}; @@ -29,7 +36,9 @@ async fn main() { ..Default::default() }; - let database = Arc::new(RwLock::new(Mochibase::open_or_new(&config.database_path).expect("Failed to open or create database"))); + let database = Arc::new(RwLock::new( + Mochibase::open_or_new(&config.database_path).expect("Failed to open or create database"), + )); let local_db = database.clone(); // Start monitoring thread, cleaning the database every 2 minutes @@ -81,6 +90,10 @@ async fn main() { info!("Stopping database cleaning thread completed successfully."); info!("Saving database on shutdown..."); - local_db.write().unwrap().save().expect("Failed to save database"); + local_db + .write() + .unwrap() + .save() + .expect("Failed to save database"); info!("Saving database completed successfully."); } diff --git a/src/pages.rs b/src/pages.rs index f37a556..fce3345 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -1,5 +1,5 @@ use maud::{html, Markup, DOCTYPE}; -use rocket::{get, http::ContentType, response::content::{RawCss, RawJavaScript}, State}; +use rocket::{get, State}; use crate::settings::Settings; diff --git a/src/resources.rs b/src/resources.rs index 05d094b..8d89b99 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -1,11 +1,21 @@ -use rocket::{get, http::ContentType, response::content::{RawCss, RawJavaScript}}; +use rocket::{ + get, + http::ContentType, + response::content::{RawCss, RawJavaScript}, +}; #[get("/resources/fonts/")] pub fn font_static(font: &str) -> Option<(ContentType, &'static [u8])> { match font { - "Roboto.woff2" => Some((ContentType::WOFF2, include_bytes!("../web/fonts/roboto.woff2"))), - "FiraCode.woff2" => Some((ContentType::WOFF2, include_bytes!("../web/fonts/fira-code.woff2"))), - _ => None + "Roboto.woff2" => Some(( + ContentType::WOFF2, + include_bytes!("../web/fonts/roboto.woff2"), + )), + "FiraCode.woff2" => Some(( + ContentType::WOFF2, + include_bytes!("../web/fonts/fira-code.woff2"), + )), + _ => None, } }