refactor: solve clippy suggestions

This commit is contained in:
minish 2023-06-28 17:24:07 -04:00
parent b44fa6c4a1
commit 6d86d4a081
Signed by: min
GPG Key ID: FEECFF24EF0CE9E9
5 changed files with 16 additions and 27 deletions

View File

@ -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<Bytes> {
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<ViewSuccess, ViewError> {
pub async fn get_upload(&self, original_path: &Path) -> Result<ViewSuccess, ViewError> {
// 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))
}
}
}

View File

@ -10,7 +10,7 @@ pub async fn index(State(engine): State<Arc<crate::engine::Engine>>) -> 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"

View File

@ -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::<usize>().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::<u64>().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::<u64>().expect("failed parsing BRZ_CACHE_SCAN_FREQ! it should be a positive number without any separators"));
let cache_mem_capacity = cache_mem_capacity.parse::<usize>().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");

View File

@ -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::<usize>())
.unwrap()
.unwrap_or(usize::MAX);

View File

@ -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");