parent
367484ccd7
commit
5e47423eaf
|
@ -13,8 +13,9 @@ pub async fn proxy_handler(
|
||||||
requested_res: web::Path<String>,
|
requested_res: web::Path<String>,
|
||||||
state: web::Data<AppState>,
|
state: web::Data<AppState>,
|
||||||
) -> Result<HttpResponse, actix_web::Error> {
|
) -> Result<HttpResponse, actix_web::Error> {
|
||||||
warn!("\t>>>\tGET {}", requested_res);
|
warn!("\t>>>\tGET {} [START]", requested_res);
|
||||||
let normalized_path = if requested_res.ends_with("/webp") {
|
let normalized_path = if requested_res.ends_with("/webp") {
|
||||||
|
warn!("Removing /webp suffix from path");
|
||||||
requested_res.replace("/webp", "")
|
requested_res.replace("/webp", "")
|
||||||
} else {
|
} else {
|
||||||
requested_res.to_string()
|
requested_res.to_string()
|
||||||
|
@ -56,16 +57,21 @@ pub async fn proxy_handler(
|
||||||
|
|
||||||
return match state.get_path(&filekey).await {
|
return match state.get_path(&filekey).await {
|
||||||
Ok(Some(stored_path)) => {
|
Ok(Some(stored_path)) => {
|
||||||
warn!("stored_path: {}", stored_path);
|
warn!("Found stored path in DB: {}", stored_path);
|
||||||
// Проверяем, существует ли файл в Storj
|
// Проверяем, существует ли файл в Storj
|
||||||
|
warn!("Checking if file exists in Storj: {}", stored_path);
|
||||||
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
|
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
|
||||||
|
warn!("File exists in Storj: {}", stored_path);
|
||||||
if content_type.starts_with("image") {
|
if content_type.starts_with("image") {
|
||||||
|
warn!("Processing image file with width: {}", requested_width);
|
||||||
if requested_width == 0 {
|
if requested_width == 0 {
|
||||||
|
warn!("Serving original file without resizing");
|
||||||
return serve_file(&stored_path, &state, shout_id).await;
|
return serve_file(&stored_path, &state, shout_id).await;
|
||||||
} else {
|
} else {
|
||||||
// Находим ближайшую ширину для миниатюры
|
|
||||||
let closest: u32 = find_closest_width(requested_width as u32);
|
let closest: u32 = find_closest_width(requested_width as u32);
|
||||||
|
warn!("Calculated closest width: {} for requested: {}", closest, requested_width);
|
||||||
let thumb_filename = &format!("{}_{}.{}", base_filename, closest, ext);
|
let thumb_filename = &format!("{}_{}.{}", base_filename, closest, ext);
|
||||||
|
warn!("Generated thumbnail filename: {}", thumb_filename);
|
||||||
|
|
||||||
// Проверяем, существует ли уже миниатюра в Storj
|
// Проверяем, существует ли уже миниатюра в Storj
|
||||||
match check_file_exists(&state.storj_client, &state.bucket, thumb_filename).await {
|
match check_file_exists(&state.storj_client, &state.bucket, thumb_filename).await {
|
||||||
|
@ -101,8 +107,9 @@ pub async fn proxy_handler(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Если файл не изображение, продолжаем обработку
|
warn!("File is not an image, proceeding with normal serving");
|
||||||
warn!("file is not an image");
|
} else {
|
||||||
|
warn!("File does not exist in Storj: {}", stored_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// we need to download what stored_path keeping in aws
|
// we need to download what stored_path keeping in aws
|
||||||
|
@ -135,9 +142,10 @@ pub async fn proxy_handler(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
warn!("cannot find stored path for: {}", filekey);
|
warn!("No stored path found in DB for: {}", filekey);
|
||||||
let ct_parts = content_type.split("/").collect::<Vec<&str>>();
|
let ct_parts = content_type.split("/").collect::<Vec<&str>>();
|
||||||
let filepath = format!("production/{}/{}.{}", ct_parts[0], base_filename, extension); // NOTE: original ext
|
let filepath = format!("production/{}/{}.{}", ct_parts[0], base_filename, extension);
|
||||||
|
warn!("Generated filepath for lookup: {}", filepath);
|
||||||
|
|
||||||
let exists_in_storj = check_file_exists(&state.storj_client, &state.bucket, &filepath).await?;
|
let exists_in_storj = check_file_exists(&state.storj_client, &state.bucket, &filepath).await?;
|
||||||
warn!("Checking existence in Storj: {}", exists_in_storj);
|
warn!("Checking existence in Storj: {}", exists_in_storj);
|
||||||
|
@ -162,8 +170,10 @@ pub async fn proxy_handler(
|
||||||
warn!("Checking existence in AWS: {}", exists_in_aws);
|
warn!("Checking existence in AWS: {}", exists_in_aws);
|
||||||
|
|
||||||
if exists_in_aws {
|
if exists_in_aws {
|
||||||
|
warn!("File found in AWS, attempting to download: {}", filepath);
|
||||||
match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await {
|
match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await {
|
||||||
Ok(filedata) => {
|
Ok(filedata) => {
|
||||||
|
warn!("Successfully downloaded file from AWS, size: {} bytes", filedata.len());
|
||||||
let _ = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string())
|
let _ = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string())
|
||||||
.await;
|
.await;
|
||||||
if let Err(e) = upload_to_s3(
|
if let Err(e) = upload_to_s3(
|
||||||
|
@ -182,17 +192,17 @@ pub async fn proxy_handler(
|
||||||
Ok(HttpResponse::Ok().content_type(content_type).body(filedata))
|
Ok(HttpResponse::Ok().content_type(content_type).body(filedata))
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("cannot download {} from aws: {}", filepath, e);
|
error!("Failed to download from AWS: {} - Error: {}", filepath, e);
|
||||||
Err(ErrorInternalServerError(e))
|
Err(ErrorInternalServerError(e))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error!("file {} does not exist in aws", filepath);
|
error!("File not found in either Storj or AWS: {}", filepath);
|
||||||
Err(ErrorNotFound("file does not exist"))
|
Err(ErrorNotFound("file does not exist"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("cannot get path from aws: {}", e);
|
error!("Database error while getting path: {}", e);
|
||||||
Err(ErrorInternalServerError(e))
|
Err(ErrorInternalServerError(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user