From 705f4c55d055cb605d2cf2a1e2864cf39ca61f19 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 22 Oct 2024 14:28:54 +0300 Subject: [PATCH] servefix --- src/handlers/serve_file.rs | 22 +++++++--------------- src/thumbnail.rs | 3 ++- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/handlers/serve_file.rs b/src/handlers/serve_file.rs index fa33cef..7b607d5 100644 --- a/src/handlers/serve_file.rs +++ b/src/handlers/serve_file.rs @@ -5,21 +5,13 @@ use crate::app_state::AppState; use crate::s3_utils::check_file_exists; /// Функция для обслуживания файла по заданному пути. -pub async fn serve_file(file_path: &str, state: &AppState) -> Result { - let filepath = match file_path.ends_with("/webp") { - true => &file_path.replace("/webp", ""), - false => file_path, - }; - let mut parts = filepath.split('/').collect::>(); // Explicit type annotation - let filename = parts.pop().unwrap_or(""); // Explicit type annotation - let file_fullpath = state.get_path(filename).await?.unwrap_or(String::new()); - - if file_fullpath.is_empty() { - return Err(ErrorInternalServerError("File not found".to_string())); +pub async fn serve_file(filepath: &str, state: &AppState) -> Result { + if filepath.is_empty() { + return Err(ErrorInternalServerError("Filename is empty".to_string())); } // Проверяем наличие файла в Storj S3 - let exists = check_file_exists(&state.storj_client, &state.storj_bucket, &file_fullpath).await?; + let exists = check_file_exists(&state.storj_client, &state.storj_bucket, &filepath).await?; if !exists { return Err(ErrorInternalServerError("File not found in Storj".to_string())); } @@ -29,10 +21,10 @@ pub async fn serve_file(file_path: &str, state: &AppState) -> Result Result Option<(String, u32, String)> { return Some((base_name.to_string(), width, ext_part.to_string())); } } + return Some((name_part.to_string(), 0, ext_part.to_string())) } None } @@ -25,7 +26,7 @@ pub fn find_closest_width(requested_width: u32) -> u32 { .unwrap_or(&THUMB_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло } -/// Генерирует миниатюры изображения для заданного набора ширин. +/// Генерирует миниатюры изображения. pub async fn generate_thumbnails(image: &DynamicImage) -> Result>, actix_web::Error> { let mut thumbnails = HashMap::new();