minorfix4
All checks were successful
deploy / deploy (push) Successful in 1m4s

This commit is contained in:
2024-10-22 13:15:37 +03:00
parent a25e16132c
commit 90b0f0bc3a
5 changed files with 34 additions and 36 deletions

View File

@@ -13,11 +13,14 @@ pub async fn proxy_handler(
state: web::Data<AppState>,
) -> Result<HttpResponse, actix_web::Error> {
info!("req.path: {}", req.path());
let requested_path = requested_res.replace("/webp", "");
let parts = requested_path.split('/').collect::<Vec<&str>>(); // Explicit type annotation
let filename = parts[parts.len()-1];
let requested_path = match state.get_path(&requested_res).await {
let requested_path = match state.get_path(&filename).await {
Ok(Some(path)) => path,
Ok(None) => {
warn!("wrong request: {}", req.path());
warn!("wrong filename: {}", filename);
return Ok(HttpResponse::NotFound().finish());
}
Err(e) => {
@@ -35,12 +38,12 @@ pub async fn proxy_handler(
// Находим ближайший подходящий размер
let closest_width = find_closest_width(requested_width);
let thumb_filekey = format!("{}_{}", base_filename, closest_width);
info!("closest width: {}, thumb_filekey: {}", closest_width, thumb_filekey);
let thumb_filename = format!("{}_{}.jpg", base_filename, closest_width);
info!("closest width: {}, thumb_filename: {}", closest_width, thumb_filename);
// Проверяем наличие миниатюры в кэше
let cached_files = state.get_cached_file_list().await;
if !cached_files.contains(&thumb_filekey) {
if !cached_files.contains(&thumb_filename) {
info!("no thumb found");
if cached_files.contains(&base_filename) {
info!("no original file found");
@@ -60,7 +63,7 @@ pub async fn proxy_handler(
upload_to_s3(
&state.storj_client,
&state.storj_bucket,
&thumb_filekey,
&thumb_filename,
thumbnail_bytes.clone(),
"image/jpeg",
)
@@ -74,7 +77,7 @@ pub async fn proxy_handler(
}
} else {
info!("thumb was found");
return serve_file(&thumb_filekey, &state).await;
return serve_file(&thumb_filename, &state).await;
}
}

View File

@@ -11,16 +11,14 @@ pub async fn serve_file(file_path: &str, state: &AppState) -> Result<HttpRespons
false => file_path,
};
let mut parts = filepath.split('/').collect::<Vec<&str>>(); // Explicit type annotation
let filename = parts.pop().unwrap();
let mut filename_parts = filename.split('.').collect::<Vec<&str>>();
let _ext = filename_parts.pop().unwrap();
let filekey = filename_parts.pop().unwrap();
let file_path_in_storj = state.get_path(filekey).await.unwrap().unwrap();
let mut file_fullpath = String::new();
if let Some(filename) = parts.pop() {
file_fullpath = state.get_path(filename).await.unwrap().unwrap();
}
// Проверяем наличие файла в Storj S3
if !check_file_exists(&state.storj_client, &state.storj_bucket, &file_path_in_storj).await? {
return Err(ErrorInternalServerError(format!("File {} not found in Storj", file_path_in_storj)));
if !check_file_exists(&state.storj_client, &state.storj_bucket, &file_fullpath).await? {
return Err(ErrorInternalServerError(format!("File {} not found in Storj", file_fullpath)));
}
// Получаем объект из Storj S3
@@ -28,10 +26,10 @@ pub async fn serve_file(file_path: &str, state: &AppState) -> Result<HttpRespons
.storj_client
.get_object()
.bucket(&state.storj_bucket)
.key(file_path_in_storj.clone())
.key(file_fullpath.clone())
.send()
.await
.map_err(|_| ErrorInternalServerError(format!("Failed to get {} object from Storj", file_path_in_storj)))?;
.map_err(|_| ErrorInternalServerError(format!("Failed to get {} object from Storj", file_fullpath)))?;
let data: aws_sdk_s3::primitives::AggregatedBytes = get_object_output
.body
@@ -40,7 +38,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_path_in_storj).first_or_octet_stream();
let mime_type = MimeGuess::from_path(&file_fullpath).first_or_octet_stream();
Ok(HttpResponse::Ok()
.content_type(mime_type.as_ref())