refactor: solve clippy suggestions
This commit is contained in:
parent
b44fa6c4a1
commit
6d86d4a081
|
@ -1,6 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
sync::atomic::{AtomicUsize, Ordering},
|
sync::atomic::{AtomicUsize, Ordering},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
@ -67,7 +67,7 @@ impl Engine {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks in cache or disk for an upload using a pathbuf
|
// 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;
|
let cache = self.cache.read().await;
|
||||||
|
|
||||||
// check if upload is in cache
|
// check if upload is in cache
|
||||||
|
@ -86,7 +86,7 @@ impl Engine {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate a new save path for an upload
|
// generate a new save path for an upload
|
||||||
|
@ -199,20 +199,20 @@ impl Engine {
|
||||||
async fn read_cached_upload(&self, name: &String) -> Option<Bytes> {
|
async fn read_cached_upload(&self, name: &String) -> Option<Bytes> {
|
||||||
let cache = self.cache.read().await;
|
let cache = self.cache.read().await;
|
||||||
|
|
||||||
if !cache.contains_key(&name) {
|
if !cache.contains_key(name) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch upload data from cache
|
// fetch upload data from cache
|
||||||
let data = cache
|
let data = cache
|
||||||
.get(&name)
|
.get(name)
|
||||||
.expect("failed to read get upload data from cache")
|
.expect("failed to read get upload data from cache")
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
Some(data)
|
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
|
// extract upload file name
|
||||||
let name = original_path
|
let name = original_path
|
||||||
.file_name()
|
.file_name()
|
||||||
|
@ -235,7 +235,7 @@ impl Engine {
|
||||||
if let Some(data) = cached_data {
|
if let Some(data) = cached_data {
|
||||||
info!("got upload from cache!!");
|
info!("got upload from cache!!");
|
||||||
|
|
||||||
return Ok(ViewSuccess::FromCache(data));
|
Ok(ViewSuccess::FromCache(data))
|
||||||
} else {
|
} else {
|
||||||
let mut file = File::open(&path).await.unwrap();
|
let mut file = File::open(&path).await.unwrap();
|
||||||
|
|
||||||
|
@ -280,7 +280,7 @@ impl Engine {
|
||||||
|
|
||||||
info!("got upload from disk!");
|
info!("got upload from disk!");
|
||||||
|
|
||||||
return Ok(ViewSuccess::FromDisk(file));
|
Ok(ViewSuccess::FromDisk(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
|
// robots.txt that tells web crawlers not to list uploads
|
||||||
const ROBOTS_TXT: &'static str = concat!(
|
const ROBOTS_TXT: &str = concat!(
|
||||||
"User-Agent: *\n",
|
"User-Agent: *\n",
|
||||||
"Disallow: /p/*\n",
|
"Disallow: /p/*\n",
|
||||||
"Allow: /\n"
|
"Allow: /\n"
|
||||||
|
|
|
@ -34,10 +34,10 @@ async fn main() {
|
||||||
|
|
||||||
// parse env vars
|
// parse env vars
|
||||||
let save_path = PathBuf::from(save_path);
|
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_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(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_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(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_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 = 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_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() {
|
if !save_path.exists() || !save_path.is_dir() {
|
||||||
panic!("the save path does not exist or is not a directory! this is invalid");
|
panic!("the save path does not exist or is not a directory! this is invalid");
|
||||||
|
|
|
@ -43,7 +43,7 @@ pub async fn new(
|
||||||
.get(header::CONTENT_LENGTH)
|
.get(header::CONTENT_LENGTH)
|
||||||
.unwrap_or(&HeaderValue::from_static(""))
|
.unwrap_or(&HeaderValue::from_static(""))
|
||||||
.to_str()
|
.to_str()
|
||||||
.and_then(|s| Ok(usize::from_str_radix(s, 10)))
|
.map(|s| s.parse::<usize>())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap_or(usize::MAX);
|
.unwrap_or(usize::MAX);
|
||||||
|
|
||||||
|
@ -51,6 +51,6 @@ pub async fn new(
|
||||||
engine
|
engine
|
||||||
.process_upload(path, name, content_length, stream)
|
.process_upload(path, name, content_length, stream)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
Ok(url)
|
Ok(url)
|
||||||
}
|
}
|
||||||
|
|
13
src/view.rs
13
src/view.rs
|
@ -73,22 +73,12 @@ impl IntoResponse for ViewSuccess {
|
||||||
}
|
}
|
||||||
ViewSuccess::FromCache(data) => {
|
ViewSuccess::FromCache(data) => {
|
||||||
// extract mutable headers from the response
|
// extract mutable headers from the response
|
||||||
let mut res = data.clone().into_response();
|
let mut res = data.into_response();
|
||||||
let headers = res.headers_mut();
|
let headers = res.headers_mut();
|
||||||
|
|
||||||
// clear the headers, let the browser imply it
|
// clear the headers, let the browser imply it
|
||||||
headers.clear();
|
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
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,7 +114,6 @@ pub async fn view(
|
||||||
// (hopefully) prevent path traversal, just check for any non-file components
|
// (hopefully) prevent path traversal, just check for any non-file components
|
||||||
if original_path
|
if original_path
|
||||||
.components()
|
.components()
|
||||||
.into_iter()
|
|
||||||
.any(|x| !matches!(x, Component::Normal(_)))
|
.any(|x| !matches!(x, Component::Normal(_)))
|
||||||
{
|
{
|
||||||
warn!("a request attempted path traversal");
|
warn!("a request attempted path traversal");
|
||||||
|
|
Loading…
Reference in New Issue