From 6d86d4a081781a4762eeeb5883631d0d8e1e2d32 Mon Sep 17 00:00:00 2001 From: minish Date: Wed, 28 Jun 2023 17:24:07 -0400 Subject: [PATCH] refactor: solve clippy suggestions --- src/engine.rs | 16 ++++++++-------- src/index.rs | 2 +- src/main.rs | 8 ++++---- src/new.rs | 4 ++-- src/view.rs | 13 +------------ 5 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 15b27ee..13b3a3b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,6 +1,6 @@ use std::{ ffi::OsStr, - path::PathBuf, + path::{Path, PathBuf}, sync::atomic::{AtomicUsize, Ordering}, time::Duration, }; @@ -67,7 +67,7 @@ impl Engine { } // checks in cache or disk for an upload using a pathbuf - pub async fn upload_exists(&self, path: &PathBuf) -> bool { + pub async fn upload_exists(&self, path: &Path) -> bool { let cache = self.cache.read().await; // check if upload is in cache @@ -86,7 +86,7 @@ impl Engine { return true; } - return false; + false } // generate a new save path for an upload @@ -199,20 +199,20 @@ impl Engine { async fn read_cached_upload(&self, name: &String) -> Option { let cache = self.cache.read().await; - if !cache.contains_key(&name) { + if !cache.contains_key(name) { return None; } // fetch upload data from cache let data = cache - .get(&name) + .get(name) .expect("failed to read get upload data from cache") .to_owned(); Some(data) } - pub async fn get_upload(&self, original_path: &PathBuf) -> Result { + pub async fn get_upload(&self, original_path: &Path) -> Result { // extract upload file name let name = original_path .file_name() @@ -235,7 +235,7 @@ impl Engine { if let Some(data) = cached_data { info!("got upload from cache!!"); - return Ok(ViewSuccess::FromCache(data)); + Ok(ViewSuccess::FromCache(data)) } else { let mut file = File::open(&path).await.unwrap(); @@ -280,7 +280,7 @@ impl Engine { info!("got upload from disk!"); - return Ok(ViewSuccess::FromDisk(file)); + Ok(ViewSuccess::FromDisk(file)) } } } diff --git a/src/index.rs b/src/index.rs index 2e2d34c..a6de3e3 100644 --- a/src/index.rs +++ b/src/index.rs @@ -10,7 +10,7 @@ pub async fn index(State(engine): State>) -> String { } // robots.txt that tells web crawlers not to list uploads -const ROBOTS_TXT: &'static str = concat!( +const ROBOTS_TXT: &str = concat!( "User-Agent: *\n", "Disallow: /p/*\n", "Allow: /\n" diff --git a/src/main.rs b/src/main.rs index e42e3ff..5f599cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,10 +34,10 @@ async fn main() { // parse env vars let save_path = PathBuf::from(save_path); - let cache_max_length = usize::from_str_radix(&cache_max_length, 10).expect("failed parsing BRZ_CACHE_UPL_MAX_LENGTH! it should be a positive number without any separators"); - let cache_upl_lifetime = Duration::from_secs(u64::from_str_radix(&cache_upl_lifetime, 10).expect("failed parsing BRZ_CACHE_UPL_LIFETIME! it should be a positive number without any separators")); - let cache_scan_freq = Duration::from_secs(u64::from_str_radix(&cache_scan_freq, 10).expect("failed parsing BRZ_CACHE_SCAN_FREQ! it should be a positive number without any separators")); - let cache_mem_capacity = usize::from_str_radix(&cache_mem_capacity, 10).expect("failed parsing BRZ_CACHE_MEM_CAPACITY! it should be a positive number without any separators"); + let cache_max_length = cache_max_length.parse::().expect("failed parsing BRZ_CACHE_UPL_MAX_LENGTH! it should be a positive number without any separators"); + let cache_upl_lifetime = Duration::from_secs(cache_upl_lifetime.parse::().expect("failed parsing BRZ_CACHE_UPL_LIFETIME! it should be a positive number without any separators")); + let cache_scan_freq = Duration::from_secs(cache_scan_freq.parse::().expect("failed parsing BRZ_CACHE_SCAN_FREQ! it should be a positive number without any separators")); + let cache_mem_capacity = cache_mem_capacity.parse::().expect("failed parsing BRZ_CACHE_MEM_CAPACITY! it should be a positive number without any separators"); if !save_path.exists() || !save_path.is_dir() { panic!("the save path does not exist or is not a directory! this is invalid"); diff --git a/src/new.rs b/src/new.rs index f953455..b7d6f59 100644 --- a/src/new.rs +++ b/src/new.rs @@ -43,7 +43,7 @@ pub async fn new( .get(header::CONTENT_LENGTH) .unwrap_or(&HeaderValue::from_static("")) .to_str() - .and_then(|s| Ok(usize::from_str_radix(s, 10))) + .map(|s| s.parse::()) .unwrap() .unwrap_or(usize::MAX); @@ -51,6 +51,6 @@ pub async fn new( engine .process_upload(path, name, content_length, stream) .await; - + Ok(url) } diff --git a/src/view.rs b/src/view.rs index 0b76486..f4bc7d9 100644 --- a/src/view.rs +++ b/src/view.rs @@ -73,22 +73,12 @@ impl IntoResponse for ViewSuccess { } ViewSuccess::FromCache(data) => { // extract mutable headers from the response - let mut res = data.clone().into_response(); + let mut res = data.into_response(); let headers = res.headers_mut(); // clear the headers, let the browser imply it headers.clear(); - /* // we do not need this for FromCache because it works fine - // read the length of the data as a string - let len_str = data.len().to_string(); - - // HeaderValue::from_str will never error if only visible ASCII characters are passed (32-127) - // .. so this should be fine - let content_length = HeaderValue::from_str(&len_str).unwrap(); - headers.append("Content-Length", content_length); - */ - res } } @@ -124,7 +114,6 @@ pub async fn view( // (hopefully) prevent path traversal, just check for any non-file components if original_path .components() - .into_iter() .any(|x| !matches!(x, Component::Normal(_))) { warn!("a request attempted path traversal");