/
vol4oks
/
LANIPTracker
Обзор
Документация
Войти
/
vol4oks
/
LANIPTracker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/static_serv.rs
39 строк
1 KB
Vol4ok
version = "0.1.1"
03 июл 2025, 21:16
03 июл 2025, 21:16
949338a
Код
Авторство
О чём код?
//TODO: переписать файл сервер на жеские пути use axum::{extract::Path, http::StatusCode, response::Response}; pub async fn serve_static(Path(name_file): Path<String>) -> Result<Response, (StatusCode, String)>{ let base_dir = std::path::Path::new("templates").join("static"); //tracing::debug!("Static path: {:?}", base_dir); let file_path = base_dir.join(name_file); tracing::debug!("File path: {:?}", file_path); // Check if the file path is within the base directory if !file_path.starts_with(&base_dir) { tracing::error!("Invalid file path: {:?}", file_path); return Err((StatusCode::FORBIDDEN, "Invalid file path".to_string())); } // Check if the file exists if !file_path.exists() { tracing::error!("File not found: {:?}", file_path); return Err((StatusCode::NOT_FOUND, "File not found".to_string())); } // Read the file contents let file = tokio::fs::File::open(&file_path).await .map_err(|e| { tracing::error!("Failed to open file: {}", e); (StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to open file: {}", e)) })?; // Create a stream from the file let stream = tokio_util::io::ReaderStream::new(file); let body = axum::body::Body::from_stream(stream); // Create a response with the file contents let resp = Response::new(body); Ok(resp) }