revert cache refreshing upon view for now
This commit is contained in:
parent
7403b7d645
commit
327443da32
39
src/cache.rs
39
src/cache.rs
|
@ -1,42 +1,5 @@
|
|||
use std::{ffi::OsStr, path::PathBuf, sync::atomic::AtomicUsize, time::Duration};
|
||||
|
||||
use axum::{
|
||||
extract::BodyStream,
|
||||
http::HeaderValue,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use memory_cache::MemoryCache;
|
||||
use mime_guess::mime;
|
||||
use std::time::Duration;
|
||||
|
||||
pub const MAX_LENGTH: usize = 80_000_000;
|
||||
pub const DURATION: Duration = Duration::from_secs(8);
|
||||
pub const FULL_SCAN_FREQ: Duration = Duration::from_secs(1);
|
||||
|
||||
pub fn get_response(cache: &mut MemoryCache<String, Bytes>, original_path: PathBuf) -> Response {
|
||||
let name = original_path
|
||||
.file_name()
|
||||
.and_then(OsStr::to_str)
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
|
||||
let cache_item = cache.get(&name.clone());
|
||||
|
||||
let data = cache_item.unwrap().clone();
|
||||
|
||||
let content_type = mime_guess::from_path(original_path)
|
||||
.first()
|
||||
.unwrap_or(mime::APPLICATION_OCTET_STREAM)
|
||||
.to_string();
|
||||
|
||||
let mut res = data.into_response();
|
||||
let headers = res.headers_mut();
|
||||
|
||||
headers.clear();
|
||||
headers.insert(
|
||||
"content-type",
|
||||
HeaderValue::from_str(content_type.as_str()).unwrap(),
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
71
src/view.rs
71
src/view.rs
|
@ -11,10 +11,9 @@ use axum::{
|
|||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use hyper::StatusCode;
|
||||
use mime_guess::mime;
|
||||
use tokio::{fs::File, io::AsyncReadExt};
|
||||
use tokio::fs::File;
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
||||
use crate::cache;
|
||||
|
@ -48,68 +47,6 @@ pub async fn view(
|
|||
return StatusCode::NOT_FOUND.into_response();
|
||||
}
|
||||
|
||||
let name = original_path
|
||||
.file_name()
|
||||
.and_then(OsStr::to_str)
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
|
||||
let mut cache = state.cache.lock().await;
|
||||
|
||||
let cache_item = cache.get(&name.clone());
|
||||
|
||||
if cache_item.is_none() {
|
||||
let mut path = PathBuf::new();
|
||||
path.push("uploads/");
|
||||
path.push(name.clone());
|
||||
|
||||
if !path.exists() || !path.is_file() {
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
}
|
||||
|
||||
let mut file = File::open(path).await.unwrap();
|
||||
let file_len = file.metadata().await.unwrap().len() as usize;
|
||||
|
||||
if file_len < cache::MAX_LENGTH {
|
||||
info!(target: "view", "recaching upload from disk");
|
||||
|
||||
let mut data = BytesMut::zeroed(file_len);
|
||||
file.read_buf(&mut data.as_mut()).await.unwrap();
|
||||
let data = data.freeze();
|
||||
|
||||
cache.insert(name.clone(), data.clone(), Some(cache::DURATION));
|
||||
|
||||
return cache::get_response(&mut cache, original_path);
|
||||
} else {
|
||||
let reader = ReaderStream::new(file);
|
||||
let stream = StreamBody::new(reader);
|
||||
|
||||
info!(target: "view", "reading upload from disk");
|
||||
|
||||
return stream.into_response();
|
||||
}
|
||||
}
|
||||
|
||||
info!(target: "view", "reading upload from cache");
|
||||
|
||||
return cache::get_response(&mut cache, original_path);
|
||||
}
|
||||
|
||||
/* #[axum::debug_handler]
|
||||
pub async fn view(
|
||||
State(state): State<Arc<crate::state::AppState>>,
|
||||
Path(original_path): Path<PathBuf>,
|
||||
) -> Response {
|
||||
// (hopefully) prevent path traversal, just check for any non-file components
|
||||
if original_path
|
||||
.components()
|
||||
.into_iter()
|
||||
.any(|x| !matches!(x, Component::Normal(_)))
|
||||
{
|
||||
error!(target: "view", "a request attempted path traversal");
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
}
|
||||
|
||||
let name = original_path
|
||||
.file_name()
|
||||
.and_then(OsStr::to_str)
|
||||
|
@ -131,10 +68,6 @@ pub async fn view(
|
|||
|
||||
let file = File::open(path).await.unwrap();
|
||||
|
||||
if file.metadata().await.unwrap().len() < (cache::MAX_LENGTH as u64) {
|
||||
info!("file can be cached");
|
||||
}
|
||||
|
||||
let reader = ReaderStream::new(file);
|
||||
let stream = StreamBody::new(reader);
|
||||
|
||||
|
@ -162,4 +95,4 @@ pub async fn view(
|
|||
);
|
||||
|
||||
return res;
|
||||
} */
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue