diff --git a/src/handlers/proxy.rs b/src/handlers/proxy.rs index 512091a..0df5176 100644 --- a/src/handlers/proxy.rs +++ b/src/handlers/proxy.rs @@ -13,9 +13,10 @@ pub async fn proxy_handler( state: web::Data, ) -> Result { warn!("\t>>>\tGET {}", requested_res); - let normalized_path = match requested_res.ends_with("/webp") { - true => requested_res.replace("/webp", ""), - false => requested_res.to_string(), + let normalized_path = if requested_res.ends_with("/webp") { + requested_res.replace("/webp", "") + } else { + requested_res.to_string() }; // парсим GET запрос @@ -123,15 +124,36 @@ pub async fn proxy_handler( Err(err) => Err(ErrorInternalServerError(err)), }; } - Ok(None) => Err(ErrorInternalServerError( - "requested file path was not found", - )), - Err(e) => Err(ErrorInternalServerError(e)), + Ok(None) => { + // download from aws to storj + match load_file_from_s3(&state.aws_client, &state.bucket, &requested_res).await { + Ok(filedata) => { + 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) => Err(ErrorInternalServerError(e)), + } + }, + Err(e) => Err(ErrorInternalServerError(e)) }; } Err(ErrorInternalServerError("invalid file key")) } + async fn thumbdata_save( original_data: Vec, state: &AppState,