servefix
All checks were successful
deploy / deploy (push) Successful in 1m2s

This commit is contained in:
Untone 2024-10-22 14:28:54 +03:00
parent 74a1a4b242
commit 705f4c55d0
2 changed files with 9 additions and 16 deletions

View File

@ -5,21 +5,13 @@ use crate::app_state::AppState;
use crate::s3_utils::check_file_exists; use crate::s3_utils::check_file_exists;
/// Функция для обслуживания файла по заданному пути. /// Функция для обслуживания файла по заданному пути.
pub async fn serve_file(file_path: &str, state: &AppState) -> Result<HttpResponse, actix_web::Error> { pub async fn serve_file(filepath: &str, state: &AppState) -> Result<HttpResponse, actix_web::Error> {
let filepath = match file_path.ends_with("/webp") { if filepath.is_empty() {
true => &file_path.replace("/webp", ""), return Err(ErrorInternalServerError("Filename is empty".to_string()));
false => file_path,
};
let mut parts = filepath.split('/').collect::<Vec<&str>>(); // 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()));
} }
// Проверяем наличие файла в Storj S3 // Проверяем наличие файла в 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 { if !exists {
return Err(ErrorInternalServerError("File not found in Storj".to_string())); 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<HttpRespons
.storj_client .storj_client
.get_object() .get_object()
.bucket(&state.storj_bucket) .bucket(&state.storj_bucket)
.key(file_fullpath.clone()) .key(filepath)
.send() .send()
.await .await
.map_err(|_| ErrorInternalServerError(format!("Failed to get {} object from Storj", file_fullpath)))?; .map_err(|_| ErrorInternalServerError(format!("Failed to get {} object from Storj", filepath)))?;
let data: aws_sdk_s3::primitives::AggregatedBytes = get_object_output let data: aws_sdk_s3::primitives::AggregatedBytes = get_object_output
.body .body
@ -41,7 +33,7 @@ pub async fn serve_file(file_path: &str, state: &AppState) -> Result<HttpRespons
.map_err(|_| ErrorInternalServerError("Failed to read object body"))?; .map_err(|_| ErrorInternalServerError("Failed to read object body"))?;
let data_bytes = data.into_bytes(); let data_bytes = data.into_bytes();
let mime_type = MimeGuess::from_path(&file_fullpath).first_or_octet_stream(); let mime_type = MimeGuess::from_path(&filepath).first_or_octet_stream();
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type(mime_type.as_ref()) .content_type(mime_type.as_ref())

View File

@ -13,6 +13,7 @@ pub fn parse_thumbnail_request(path: &str) -> Option<(String, u32, String)> {
return Some((base_name.to_string(), width, ext_part.to_string())); return Some((base_name.to_string(), width, ext_part.to_string()));
} }
} }
return Some((name_part.to_string(), 0, ext_part.to_string()))
} }
None None
} }
@ -25,7 +26,7 @@ pub fn find_closest_width(requested_width: u32) -> u32 {
.unwrap_or(&THUMB_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло .unwrap_or(&THUMB_WIDTHS[0]) // Возвращаем самый маленький размер, если ничего не подошло
} }
/// Генерирует миниатюры изображения для заданного набора ширин. /// Генерирует миниатюры изображения.
pub async fn generate_thumbnails(image: &DynamicImage) -> Result<HashMap<u32, Vec<u8>>, actix_web::Error> { pub async fn generate_thumbnails(image: &DynamicImage) -> Result<HashMap<u32, Vec<u8>>, actix_web::Error> {
let mut thumbnails = HashMap::new(); let mut thumbnails = HashMap::new();