remove small test

This commit is contained in:
minish 2022-12-28 23:05:28 -05:00 committed by minish
parent 05c6460d12
commit a2fccd1f1c
2 changed files with 142 additions and 140 deletions

14
Cargo.lock generated
View File

@ -81,7 +81,13 @@ dependencies = [
] ]
[[package]] [[package]]
name = "axum-demo" name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "breeze"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
@ -95,12 +101,6 @@ dependencies = [
"tower", "tower",
] ]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.3.0" version = "1.3.0"

View File

@ -1,133 +1,135 @@
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
path::{Component, PathBuf}, path::{Component, PathBuf},
sync::Arc, sync::Arc,
}; };
use axum::{ use axum::{
body::StreamBody, body::StreamBody,
extract::{Path, State}, extract::{Path, State},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
use bytes::{buf::Reader, Bytes}; use bytes::{buf::Reader, Bytes};
use hyper::StatusCode; use hyper::StatusCode;
use tokio::fs::File; use tokio::fs::File;
use tokio_util::io::ReaderStream; use tokio_util::io::ReaderStream;
/* pub enum ViewResponse { /* pub enum ViewResponse {
FromDisk(StreamBody<ReaderStream<File>>), FromDisk(StreamBody<ReaderStream<File>>),
FromCache(Bytes) FromCache(Bytes)
} }
impl IntoResponse for ViewResponse { impl IntoResponse for ViewResponse {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self { match self {
ViewResponse::FromDisk(stream) => stream.into_response(), ViewResponse::FromDisk(stream) => stream.into_response(),
ViewResponse::FromCache(data) => data.into_response() ViewResponse::FromCache(data) => data.into_response()
} }
} }
} */ } */
pub async fn view( pub async fn view(
State(state): State<Arc<crate::state::AppState>>, State(state): State<Arc<crate::state::AppState>>,
Path(original_path): Path<PathBuf>, Path(original_path): Path<PathBuf>,
) -> Response { ) -> Response {
println!("{:?}", original_path); println!("{:?}", original_path);
// (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() .into_iter()
.any(|x| !matches!(x, Component::Normal(_))) .any(|x| !matches!(x, Component::Normal(_)))
{ {
println!("lol NOPE"); println!("lol NOPE");
return StatusCode::NOT_FOUND.into_response(); return StatusCode::NOT_FOUND.into_response();
} }
let name = original_path let name = original_path
.file_name() .file_name()
.and_then(OsStr::to_str) .and_then(OsStr::to_str)
.unwrap_or_default() .unwrap_or_default()
.to_string(); .to_string();
let cache = state.cache.lock().unwrap(); let cache = state.cache.lock().unwrap();
let cache_item = cache.get(&name); let cache_item = cache.get(&name);
if true /* cache_item.is_none() */ { if true /* cache_item.is_none() */ {
let mut path = PathBuf::new(); let mut path = PathBuf::new();
path.push("uploads/"); path.push("uploads/");
path.push(name); path.push(name);
if !path.exists() || !path.is_file() { if !path.exists() || !path.is_file() {
return StatusCode::NOT_FOUND.into_response(); return StatusCode::NOT_FOUND.into_response();
} }
let file = File::open(path).await.unwrap(); let file = File::open(path).await.unwrap();
let reader = ReaderStream::new(file); let reader = ReaderStream::new(file);
let stream = StreamBody::new(reader); let stream = StreamBody::new(reader);
println!("from disk"); println!("from disk");
return stream.into_response(); return stream.into_response();
} }
println!("from cache! :D"); println!("from cache! :D");
return "asdf".into_response(); let data = cache_item.unwrap().clone();
}
return data.into_response();
/* pub async fn view( }
State(mem_cache): State<Arc<crate::cache::MemCache>>,
Path(original_path): Path<PathBuf>, /* pub async fn view(
) -> Response { State(mem_cache): State<Arc<crate::cache::MemCache>>,
for component in original_path.components() { Path(original_path): Path<PathBuf>,
println!("{:?}", component); ) -> Response {
} for component in original_path.components() {
println!("{:?}", component);
// (hopefully) prevent path traversal, just check for any non-file components }
if original_path
.components() // (hopefully) prevent path traversal, just check for any non-file components
.into_iter() if original_path
.any(|x| !matches!(x, Component::Normal(_))) .components()
{ .into_iter()
return StatusCode::NOT_FOUND.into_response() .any(|x| !matches!(x, Component::Normal(_)))
} {
return StatusCode::NOT_FOUND.into_response()
// this causes an obscure bug where filenames like hiworld%2fnamehere.png will still load namehere.png }
// i could limit the path components to 1 and sort of fix this
let name = original_path // this causes an obscure bug where filenames like hiworld%2fnamehere.png will still load namehere.png
.file_name() // i could limit the path components to 1 and sort of fix this
.and_then(OsStr::to_str) let name = original_path
.unwrap_or_default() .file_name()
.to_string(); .and_then(OsStr::to_str)
.unwrap_or_default()
let cache = mem_cache.cache.lock().unwrap(); .to_string();
let cache_item = cache.get(&name); let cache = mem_cache.cache.lock().unwrap();
if cache_item.is_some() { let cache_item = cache.get(&name);
println!("they requested something in the cache!");
if cache_item.is_some() {
let data = cache_item.unwrap().clone(); println!("they requested something in the cache!");
return data.into_response() let data = cache_item.unwrap().clone();
}
return data.into_response()
let mut path = PathBuf::new(); }
path.push("uploads/");
path.push(name); let mut path = PathBuf::new();
path.push("uploads/");
if !path.exists() || !path.is_file() { path.push(name);
return StatusCode::NOT_FOUND.into_response()
} if !path.exists() || !path.is_file() {
return StatusCode::NOT_FOUND.into_response()
let file = File::open(path).await.unwrap(); }
let reader = ReaderStream::new(file); let file = File::open(path).await.unwrap();
let stream = StreamBody::new(reader);
let reader = ReaderStream::new(file);
stream.into_response() let stream = StreamBody::new(reader);
}
*/ stream.into_response()
}
*/