32 lines
877 B
Rust
32 lines
877 B
Rust
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()
|
|
}
|