diff --git a/Cargo.lock b/Cargo.lock index f61015f..e6f099f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -107,6 +107,7 @@ dependencies = [ "bytes", "hyper", "memory-cache-rs", + "mime_guess", "rand", "tokio", "tokio-stream", @@ -385,6 +386,16 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "mio" version = "0.8.5" @@ -778,6 +789,15 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-ident" version = "1.0.5" diff --git a/Cargo.toml b/Cargo.toml index 350ba99..786327b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ tokio-stream = "0.1" tower = "0.4.13" bytes = "1" rand = "0.8.5" +mime_guess = "2.0.4" memory-cache-rs = "0.2.0" diff --git a/src/new.rs b/src/new.rs index 44f4162..da0d8c9 100644 --- a/src/new.rs +++ b/src/new.rs @@ -139,6 +139,8 @@ pub async fn new( println!("[upl] caching upload!!"); cache.insert(name, data.freeze(), Some(Duration::from_secs(120))); } + + drop(cache); }); Ok(url) diff --git a/src/view.rs b/src/view.rs index c6efb00..71fcf54 100644 --- a/src/view.rs +++ b/src/view.rs @@ -7,10 +7,11 @@ use std::{ use axum::{ body::StreamBody, extract::{Path, State}, - response::{IntoResponse, Response}, debug_handler, + response::{IntoResponse, Response}, debug_handler, http::HeaderValue, }; use bytes::{buf::Reader, Bytes}; use hyper::StatusCode; +use mime_guess::{mime, Mime}; use tokio::fs::File; use tokio_util::io::ReaderStream; @@ -76,7 +77,18 @@ pub async fn view( let data = cache_item.unwrap().clone(); - return data.into_response(); + drop(cache); + + let contentType = mime_guess::from_path(name).first().unwrap_or(mime::APPLICATION_OCTET_STREAM).to_string(); + + let mut res = data.into_response(); + + let mut headers = res.headers_mut(); + + headers.clear(); + headers.insert("content-type", HeaderValue::from(contentType)); + + return res; } /* pub async fn view(