This commit is contained in:
parent
74a1a4b242
commit
705f4c55d0
|
@ -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<HttpResponse, actix_web::Error> {
|
||||
let filepath = match file_path.ends_with("/webp") {
|
||||
true => &file_path.replace("/webp", ""),
|
||||
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()));
|
||||
pub async fn serve_file(filepath: &str, state: &AppState) -> Result<HttpResponse, actix_web::Error> {
|
||||
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<HttpRespons
|
|||
.storj_client
|
||||
.get_object()
|
||||
.bucket(&state.storj_bucket)
|
||||
.key(file_fullpath.clone())
|
||||
.key(filepath)
|
||||
.send()
|
||||
.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
|
||||
.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"))?;
|
||||
|
||||
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()
|
||||
.content_type(mime_type.as_ref())
|
||||
|
|
|
@ -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((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<HashMap<u32, Vec<u8>>, actix_web::Error> {
|
||||
let mut thumbnails = HashMap::new();
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user