From a2fccd1f1cc3c7ad14d798399d8f5839793d243a Mon Sep 17 00:00:00 2001 From: minish Date: Wed, 28 Dec 2022 23:05:28 -0500 Subject: [PATCH] remove small test --- Cargo.lock | 14 +-- src/view.rs | 268 ++++++++++++++++++++++++++-------------------------- 2 files changed, 142 insertions(+), 140 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7735dd0..54c6443 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,7 +81,13 @@ dependencies = [ ] [[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" dependencies = [ "axum", @@ -95,12 +101,6 @@ dependencies = [ "tower", ] -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bytes" version = "1.3.0" diff --git a/src/view.rs b/src/view.rs index ab7e628..0b3a97a 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,133 +1,135 @@ -use std::{ - ffi::OsStr, - path::{Component, PathBuf}, - sync::Arc, -}; - -use axum::{ - body::StreamBody, - extract::{Path, State}, - response::{IntoResponse, Response}, -}; -use bytes::{buf::Reader, Bytes}; -use hyper::StatusCode; -use tokio::fs::File; -use tokio_util::io::ReaderStream; - -/* pub enum ViewResponse { - FromDisk(StreamBody>), - FromCache(Bytes) -} - -impl IntoResponse for ViewResponse { - fn into_response(self) -> Response { - match self { - ViewResponse::FromDisk(stream) => stream.into_response(), - ViewResponse::FromCache(data) => data.into_response() - } - } -} */ - -pub async fn view( - State(state): State>, - Path(original_path): Path, -) -> Response { - println!("{:?}", original_path); - - // (hopefully) prevent path traversal, just check for any non-file components - if original_path - .components() - .into_iter() - .any(|x| !matches!(x, Component::Normal(_))) - { - println!("lol NOPE"); - return StatusCode::NOT_FOUND.into_response(); - } - - let name = original_path - .file_name() - .and_then(OsStr::to_str) - .unwrap_or_default() - .to_string(); - - let cache = state.cache.lock().unwrap(); - - let cache_item = cache.get(&name); - - if true /* cache_item.is_none() */ { - let mut path = PathBuf::new(); - path.push("uploads/"); - path.push(name); - - 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 stream = StreamBody::new(reader); - - println!("from disk"); - - return stream.into_response(); - } - - println!("from cache! :D"); - - return "asdf".into_response(); -} - -/* pub async fn view( - State(mem_cache): State>, - Path(original_path): Path, -) -> Response { - for component in original_path.components() { - println!("{:?}", component); - } - - // (hopefully) prevent path traversal, just check for any non-file components - if original_path - .components() - .into_iter() - .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 - .file_name() - .and_then(OsStr::to_str) - .unwrap_or_default() - .to_string(); - - let cache = mem_cache.cache.lock().unwrap(); - - let cache_item = cache.get(&name); - - if cache_item.is_some() { - println!("they requested something in the cache!"); - - let data = cache_item.unwrap().clone(); - - return data.into_response() - } - - let mut path = PathBuf::new(); - path.push("uploads/"); - path.push(name); - - 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 stream = StreamBody::new(reader); - - stream.into_response() -} - */ +use std::{ + ffi::OsStr, + path::{Component, PathBuf}, + sync::Arc, +}; + +use axum::{ + body::StreamBody, + extract::{Path, State}, + response::{IntoResponse, Response}, +}; +use bytes::{buf::Reader, Bytes}; +use hyper::StatusCode; +use tokio::fs::File; +use tokio_util::io::ReaderStream; + +/* pub enum ViewResponse { + FromDisk(StreamBody>), + FromCache(Bytes) +} + +impl IntoResponse for ViewResponse { + fn into_response(self) -> Response { + match self { + ViewResponse::FromDisk(stream) => stream.into_response(), + ViewResponse::FromCache(data) => data.into_response() + } + } +} */ + +pub async fn view( + State(state): State>, + Path(original_path): Path, +) -> Response { + println!("{:?}", original_path); + + // (hopefully) prevent path traversal, just check for any non-file components + if original_path + .components() + .into_iter() + .any(|x| !matches!(x, Component::Normal(_))) + { + println!("lol NOPE"); + return StatusCode::NOT_FOUND.into_response(); + } + + let name = original_path + .file_name() + .and_then(OsStr::to_str) + .unwrap_or_default() + .to_string(); + + let cache = state.cache.lock().unwrap(); + + let cache_item = cache.get(&name); + + if true /* cache_item.is_none() */ { + let mut path = PathBuf::new(); + path.push("uploads/"); + path.push(name); + + 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 stream = StreamBody::new(reader); + + println!("from disk"); + + return stream.into_response(); + } + + println!("from cache! :D"); + + let data = cache_item.unwrap().clone(); + + return data.into_response(); +} + +/* pub async fn view( + State(mem_cache): State>, + Path(original_path): Path, +) -> Response { + for component in original_path.components() { + println!("{:?}", component); + } + + // (hopefully) prevent path traversal, just check for any non-file components + if original_path + .components() + .into_iter() + .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 + .file_name() + .and_then(OsStr::to_str) + .unwrap_or_default() + .to_string(); + + let cache = mem_cache.cache.lock().unwrap(); + + let cache_item = cache.get(&name); + + if cache_item.is_some() { + println!("they requested something in the cache!"); + + let data = cache_item.unwrap().clone(); + + return data.into_response() + } + + let mut path = PathBuf::new(); + path.push("uploads/"); + path.push(name); + + 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 stream = StreamBody::new(reader); + + stream.into_response() +} + */