From 0d4f21c79b7fccb1f6d54a63a729b5cd6c8ddae3 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 23 Oct 2024 17:46:30 +0300 Subject: [PATCH] handle-accurater --- src/handlers/proxy.rs | 81 +++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/handlers/proxy.rs b/src/handlers/proxy.rs index 9b1d248..16b3b04 100644 --- a/src/handlers/proxy.rs +++ b/src/handlers/proxy.rs @@ -1,3 +1,4 @@ +use actix_web::error::ErrorNotFound; use actix_web::{error::ErrorInternalServerError, web, HttpRequest, HttpResponse, Result}; use log::{error, warn}; @@ -142,46 +143,52 @@ pub async fn proxy_handler( let ct_parts = content_type.split("/").collect::>(); let filepath = format!("production/{}/{}.{}", ct_parts[0], base_filename, extension); // NOTE: original ext - match check_file_exists(&state.storj_client, &state.bucket, &filepath).await? { - true => { - warn!("file {} exists in storj", filepath); - } - false => { - warn!("file {} does not exist in storj", filepath); - } - } + let exists_in_storj = check_file_exists(&state.storj_client, &state.bucket, &filepath).await?; - match check_file_exists(&state.aws_client, &state.bucket, &filepath).await? { - true => { - warn!("file {} exists in aws", filepath); - } - false => { - warn!("file {} does not exist in aws", filepath); - } - } + if exists_in_storj { + warn!("file {} exists in storj, try to generate thumbnails", filepath); - match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await { - Ok(filedata) => { - let _ = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string()) - .await; - if let Err(e) = upload_to_s3( - &state.storj_client, - &state.bucket, - &filekey, - filedata.clone(), - content_type, - ) - .await { - warn!("cannot upload to storj: {}", e); - } else { - warn!("file {} uploaded to storj", filekey); + match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await { + Ok(filedata) => { + let _ = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string()).await; } - Ok(HttpResponse::Ok().content_type(content_type).body(filedata)) - }, - Err(e) => { - error!("cannot download {} from aws: {}", filepath, e); - Err(ErrorInternalServerError(e)) - }, + Err(e) => { + error!("cannot download {} from storj: {}", filekey, e); + return Err(ErrorInternalServerError(e)); + } + } + } else { + warn!("file {} does not exist in storj", filepath); + } + + let exists_in_aws = check_file_exists(&state.aws_client, &state.bucket, &filepath).await?; + if exists_in_aws { + match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await { + Ok(filedata) => { + let _ = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string()) + .await; + if let Err(e) = upload_to_s3( + &state.storj_client, + &state.bucket, + &filekey, + filedata.clone(), + content_type, + ) + .await { + warn!("cannot upload to storj: {}", e); + } else { + warn!("file {} uploaded to storj", filekey); + } + Ok(HttpResponse::Ok().content_type(content_type).body(filedata)) + }, + Err(e) => { + error!("cannot download {} from aws: {}", filepath, e); + Err(ErrorInternalServerError(e)) + }, + } + } else { + error!("file {} does not exist in aws", filepath); + Err(ErrorNotFound("file does not exist")) } }, Err(e) => {