feat: Added http server that serve the same files
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use axum::debug_handler;
|
||||
use axum::extract::Request;
|
||||
use axum::response::IntoResponse;
|
||||
use reqwest::{StatusCode, header};
|
||||
|
||||
use crate::file::get_file;
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn handler(req: Request) -> impl IntoResponse {
|
||||
let Some(path) = req.uri().path().strip_prefix("/") else {
|
||||
return StatusCode::BAD_REQUEST.into_response();
|
||||
};
|
||||
|
||||
let Some((file, content_type)) = get_file(path).await else {
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
|
||||
let path = PathBuf::from(path);
|
||||
let filename = path.file_name().unwrap();
|
||||
let filename = filename.to_str().unwrap();
|
||||
|
||||
let content_type = [(header::CONTENT_TYPE, content_type)];
|
||||
let disposition = [(
|
||||
header::CONTENT_DISPOSITION,
|
||||
format!("inline; filename=\"{filename}\""),
|
||||
)];
|
||||
|
||||
(content_type, disposition, file).into_response()
|
||||
}
|
||||
Reference in New Issue
Block a user