diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-12 12:33:54 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-12 12:33:54 +0000 |
| commit | 4d5d6d664b59205770703d321ec78e85476f80bf (patch) | |
| tree | dc19f94b55fc0e17774e49b4a7a5013b7e4b9bec /src | |
| parent | ea16fc1b2d36b5ccafbd380612f8b1c7d575800b (diff) | |
Images are showed as images not binary
Diffstat (limited to 'src')
| -rw-r--r-- | src/files.rs | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/files.rs b/src/files.rs index 00d16b2..f232257 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,7 +2,7 @@ use crate::config::{SAVE_FILE_BASE_PATH, UPLOADS_ENDPOINT}; use crate::errors::AppError; use axum::{ extract::{Multipart, Path}, - http::header::HeaderMap, + http::header::{HeaderMap, HeaderName, HeaderValue}, }; use std::fs::read; @@ -52,13 +52,24 @@ pub async fn upload( /// Axum endpoint which shows uploaded file pub async fn show_uploads(Path(id): Path<String>) -> (HeaderMap, Vec<u8>) { - // let index = id.find(".").map(|i| i).unwrap_or(usize::max_value()); + let index = id.find(".").map(|i| i).unwrap_or(usize::max_value()); - // let mut ext_name = "xxx"; - // if index != usize::max_value() { - // ext_name = &id[index + 1..]; - // } - let headers = HeaderMap::new(); + let mut ext_name = "xxx"; + if index != usize::max_value() { + ext_name = &id[index + 1..]; + } + let mut headers = HeaderMap::new(); + + if vec!["jpg", "jpeg", "png", "gif", "webp"] + .iter() + .any(|&x| x == ext_name) + { + let content_type = format!("image/{}", ext_name); + headers.insert( + HeaderName::from_static("content-type"), + HeaderValue::from_str(&content_type).unwrap(), + ); + } let file_name = format!("{}/{}", SAVE_FILE_BASE_PATH, id); (headers, read(&file_name).unwrap()) } |
