quoter/src/handlers/proxy.rs

258 lines
13 KiB
Rust
Raw Normal View History

2024-10-23 14:46:30 +00:00
use actix_web::error::ErrorNotFound;
2024-10-21 21:36:42 +00:00
use actix_web::{error::ErrorInternalServerError, web, HttpRequest, HttpResponse, Result};
2024-10-23 08:55:21 +00:00
use log::{error, warn};
2024-10-21 21:36:42 +00:00
use crate::app_state::AppState;
use crate::handlers::serve_file::serve_file;
2024-10-22 17:35:51 +00:00
use crate::s3_utils::{check_file_exists, load_file_from_s3, upload_to_s3};
2024-10-23 14:29:03 +00:00
use crate::thumbnail::{find_closest_width, parse_file_path, thumbdata_save};
2024-11-13 08:14:53 +00:00
use crate::lookup::{find_file_by_pattern, get_mime_type};
2024-10-21 21:36:42 +00:00
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
pub async fn proxy_handler(
2024-10-23 17:06:34 +00:00
req: HttpRequest,
2024-10-22 06:38:30 +00:00
requested_res: web::Path<String>,
2024-10-21 21:36:42 +00:00
state: web::Data<AppState>,
) -> Result<HttpResponse, actix_web::Error> {
2024-11-11 11:03:20 +00:00
warn!("\t>>>\tGET {} [START]", requested_res);
2024-10-22 19:56:12 +00:00
let normalized_path = if requested_res.ends_with("/webp") {
2024-11-11 11:03:20 +00:00
warn!("Removing /webp suffix from path");
2024-10-22 19:56:12 +00:00
requested_res.replace("/webp", "")
} else {
requested_res.to_string()
2024-10-21 21:36:42 +00:00
};
2024-10-22 16:34:08 +00:00
// парсим GET запрос
2024-10-23 12:31:33 +00:00
let (base_filename, requested_width, extension) = parse_file_path(&normalized_path);
2024-10-23 12:02:33 +00:00
warn!("detected file extension: {}", extension);
2024-10-23 13:21:18 +00:00
warn!("base_filename: {}", base_filename);
warn!("requested width: {}", requested_width);
2024-10-23 12:02:33 +00:00
let ext = extension.as_str().to_lowercase();
warn!("normalized to lowercase: {}", ext);
2024-10-23 11:45:23 +00:00
let filekey = format!("{}.{}", base_filename, &ext);
2024-10-23 12:02:33 +00:00
warn!("filekey: {}", filekey);
2024-11-13 08:14:53 +00:00
let content_type = match get_mime_type(&ext) {
Some(mime) => mime.to_string(),
None => {
let mut redis = state.redis.clone();
match find_file_by_pattern(&mut redis, &base_filename).await {
Ok(Some(found_file)) => {
if let Some(found_ext) = found_file.split('.').last() {
get_mime_type(found_ext)
.unwrap_or("application/octet-stream")
.to_string()
} else {
"application/octet-stream".to_string()
}
}
_ => {
error!("unsupported file format");
return Err(ErrorInternalServerError("unsupported file format"));
}
}
}
2024-10-23 10:31:05 +00:00
};
2024-10-22 17:42:45 +00:00
2024-10-23 10:31:05 +00:00
warn!("content_type: {}", content_type);
2024-10-22 17:35:51 +00:00
2024-10-23 17:06:34 +00:00
let shout_id = match req.query_string().contains("s=") {
true => req.query_string().split("s=").collect::<Vec<&str>>().pop().unwrap_or(""),
false => ""
};
2024-10-23 10:31:05 +00:00
return match state.get_path(&filekey).await {
Ok(Some(stored_path)) => {
2024-11-11 11:03:20 +00:00
warn!("Found stored path in DB: {}", stored_path);
2024-11-11 11:05:21 +00:00
warn!("Checking Storj path - bucket: {}, path: {}", state.bucket, stored_path);
2024-10-23 10:31:05 +00:00
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
2024-11-11 11:03:20 +00:00
warn!("File exists in Storj: {}", stored_path);
2024-10-23 10:31:05 +00:00
if content_type.starts_with("image") {
2024-11-11 11:03:20 +00:00
warn!("Processing image file with width: {}", requested_width);
2024-10-23 17:32:07 +00:00
if requested_width == 0 {
2024-11-11 11:03:20 +00:00
warn!("Serving original file without resizing");
2024-11-11 11:07:25 +00:00
serve_file(&stored_path, &state, shout_id).await
2024-10-23 17:32:07 +00:00
} else {
let closest: u32 = find_closest_width(requested_width as u32);
2024-11-11 11:03:20 +00:00
warn!("Calculated closest width: {} for requested: {}", closest, requested_width);
2024-10-23 17:32:07 +00:00
let thumb_filename = &format!("{}_{}.{}", base_filename, closest, ext);
2024-11-11 11:03:20 +00:00
warn!("Generated thumbnail filename: {}", thumb_filename);
2024-10-23 17:32:07 +00:00
// Проверяем, существует ли уже миниатюра в Storj
match check_file_exists(&state.storj_client, &state.bucket, thumb_filename).await {
Ok(true) => {
warn!("serve existed thumb file: {}", thumb_filename);
2024-11-11 11:07:25 +00:00
serve_file(thumb_filename, &state, shout_id).await
2024-10-23 17:32:07 +00:00
},
Ok(false) => {
2024-11-07 18:22:34 +00:00
// Миниатюра не существует, возвращаем оригинал и запускаем генерацию миниатюры
let original_file = serve_file(&stored_path, &state, shout_id).await?;
// Запускаем асинхронную задачу для генерации миниатюры
let state_clone = state.clone();
let stored_path_clone = stored_path.clone();
let filekey_clone = filekey.clone();
let content_type_clone = content_type.to_string();
2024-10-23 10:31:05 +00:00
2024-11-07 18:22:34 +00:00
actix_web::rt::spawn(async move {
if let Ok(filedata) = load_file_from_s3(&state_clone.storj_client, &state_clone.bucket, &stored_path_clone).await {
warn!("generate new thumb files: {}", stored_path_clone);
if let Err(e) = thumbdata_save(filedata, &state_clone, &filekey_clone, content_type_clone).await {
error!("Failed to generate thumbnail: {}", e);
}
2024-10-22 17:35:51 +00:00
}
2024-11-07 18:22:34 +00:00
});
2024-11-11 11:07:25 +00:00
Ok(original_file)
2024-10-23 17:32:07 +00:00
}
Err(e) => {
error!("ошибка при проверке существования миниатюры: {}", e);
2024-11-11 11:07:25 +00:00
Err(ErrorInternalServerError("failed to load thumbnail"))
2024-10-23 17:32:07 +00:00
}
2024-10-23 10:31:05 +00:00
}
2024-10-23 17:32:07 +00:00
}
2024-11-11 11:07:25 +00:00
} else {
warn!("File is not an image, proceeding with normal serving");
serve_file(&stored_path, &state, shout_id).await
2024-10-22 16:34:08 +00:00
}
2024-11-11 11:03:20 +00:00
} else {
2024-11-11 11:05:21 +00:00
warn!("Attempting to load from AWS - bucket: {}, path: {}", state.bucket, stored_path);
2024-11-11 11:15:45 +00:00
// Определяем тип медиа из content_type
let media_type = content_type.split("/").next().unwrap_or("image");
2024-11-12 09:13:15 +00:00
// Создаем варианты путей с обоими регистрами расширения
let paths_lower = vec![
2024-11-11 11:11:11 +00:00
stored_path.clone(),
2024-11-13 09:03:32 +00:00
// format!("production/{}", stored_path),
2024-11-11 11:15:45 +00:00
format!("production/{}/{}", media_type, stored_path)
2024-11-11 11:11:11 +00:00
];
2024-11-12 09:13:15 +00:00
// Создаем те же пути, но с оригинальным регистром расширения
let orig_ext = extension.as_str(); // оригинальное расширение
let orig_stored_path = format!("{}.{}", base_filename, orig_ext);
let paths_orig = vec![
orig_stored_path.clone(),
2024-11-13 09:03:32 +00:00
// format!("production/{}", orig_stored_path),
2024-11-12 09:13:15 +00:00
format!("production/{}/{}", media_type, orig_stored_path)
];
// Объединяем все пути для проверки
let all_paths = paths_lower.into_iter().chain(paths_orig.into_iter());
for path in all_paths {
2024-11-11 11:11:11 +00:00
warn!("Trying AWS path: {}", path);
match load_file_from_s3(&state.aws_client, &state.bucket, &path).await {
Ok(filedata) => {
warn!("Successfully loaded file from AWS, size: {} bytes", filedata.len());
warn!("Attempting to upload to Storj with key: {}", filekey);
if let Err(e) = upload_to_s3(
&state.storj_client,
&state.bucket,
&filekey,
filedata.clone(),
2024-11-13 08:14:53 +00:00
&content_type,
2024-11-11 11:11:11 +00:00
).await {
error!("Failed to upload to Storj: {} - Error: {}", filekey, e);
} else {
warn!("Successfully uploaded to Storj: {}", filekey);
}
return Ok(HttpResponse::Ok().content_type(content_type).body(filedata));
}
Err(err) => {
warn!("Failed to load from AWS path {}: {:?}", path, err);
continue;
2024-11-11 11:05:21 +00:00
}
}
2024-11-11 11:11:11 +00:00
}
error!("Failed to load from any AWS path for: {}", stored_path);
Err(ErrorInternalServerError("Failed to load file from AWS"))
2024-11-11 11:05:21 +00:00
}
2024-10-23 10:31:05 +00:00
}
Ok(None) => {
2024-11-11 11:03:20 +00:00
warn!("No stored path found in DB for: {}", filekey);
2024-10-23 13:38:34 +00:00
let ct_parts = content_type.split("/").collect::<Vec<&str>>();
2024-11-12 09:10:14 +00:00
// Создаем два варианта пути - с оригинальным расширением и с нижним регистром
let filepath_lower = format!("production/{}/{}.{}", ct_parts[0], base_filename, ext);
let filepath_orig = format!("production/{}/{}.{}", ct_parts[0], base_filename, extension);
warn!("Looking up files with paths: {} or {} in bucket: {}",
filepath_lower, filepath_orig, state.bucket);
// Проверяем существование файла с обоими вариантами расширения
let exists_in_aws_lower = check_file_exists(&state.aws_client, &state.bucket, &filepath_lower).await?;
let exists_in_aws_orig = check_file_exists(&state.aws_client, &state.bucket, &filepath_orig).await?;
let filepath = if exists_in_aws_orig {
filepath_orig
} else if exists_in_aws_lower {
filepath_lower
} else {
// Если файл не найден ни с одним из расширений, используем нижний регистр по умолчанию
filepath_lower
};
2024-10-23 13:38:34 +00:00
2024-10-23 14:46:30 +00:00
let exists_in_storj = check_file_exists(&state.storj_client, &state.bucket, &filepath).await?;
2024-11-07 18:30:53 +00:00
warn!("Checking existence in Storj: {}", exists_in_storj);
2024-10-23 13:38:34 +00:00
2024-10-23 14:46:30 +00:00
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;
}
Err(e) => {
error!("cannot download {} from storj: {}", filekey, e);
return Err(ErrorInternalServerError(e));
}
2024-10-23 13:38:34 +00:00
}
2024-10-23 14:46:30 +00:00
} else {
warn!("file {} does not exist in storj", filepath);
2024-10-23 13:38:34 +00:00
}
2024-10-23 14:46:30 +00:00
let exists_in_aws = check_file_exists(&state.aws_client, &state.bucket, &filepath).await?;
2024-11-07 18:30:53 +00:00
warn!("Checking existence in AWS: {}", exists_in_aws);
2024-10-23 14:46:30 +00:00
if exists_in_aws {
2024-11-11 11:03:20 +00:00
warn!("File found in AWS, attempting to download: {}", filepath);
2024-10-23 14:46:30 +00:00
match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await {
Ok(filedata) => {
2024-11-11 11:03:20 +00:00
warn!("Successfully downloaded file from AWS, size: {} bytes", filedata.len());
2024-10-23 14:46:30 +00:00
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(),
2024-11-13 08:14:53 +00:00
&content_type,
2024-10-23 14:46:30 +00:00
)
.await {
warn!("cannot upload to storj: {}", e);
} else {
warn!("file {} uploaded to storj", filekey);
2024-10-23 18:41:29 +00:00
state.set_path(&filekey, &filepath).await;
2024-10-23 14:46:30 +00:00
}
Ok(HttpResponse::Ok().content_type(content_type).body(filedata))
},
Err(e) => {
2024-11-11 11:03:20 +00:00
error!("Failed to download from AWS: {} - Error: {}", filepath, e);
2024-10-23 14:46:30 +00:00
Err(ErrorInternalServerError(e))
},
}
} else {
2024-11-11 11:03:20 +00:00
error!("File not found in either Storj or AWS: {}", filepath);
2024-10-23 14:46:30 +00:00
Err(ErrorNotFound("file does not exist"))
2024-10-22 17:35:51 +00:00
}
2024-10-23 10:31:05 +00:00
},
2024-10-23 12:45:05 +00:00
Err(e) => {
2024-11-11 11:05:21 +00:00
error!("Database error while getting path: {} - Full error: {:?}", filekey, e);
2024-10-23 12:45:05 +00:00
Err(ErrorInternalServerError(e))
}
2024-10-22 16:34:08 +00:00
}
}