2024-10-23 17:46:30 +03:00
|
|
|
|
use actix_web::error::ErrorNotFound;
|
2025-09-01 22:58:03 +03:00
|
|
|
|
use actix_web::{HttpRequest, HttpResponse, Result, error::ErrorInternalServerError, web};
|
|
|
|
|
|
use log::{error, info, warn};
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2025-09-02 14:00:54 +03:00
|
|
|
|
use super::common::create_file_response_with_analytics;
|
2024-10-22 00:36:42 +03:00
|
|
|
|
use crate::app_state::AppState;
|
|
|
|
|
|
use crate::handlers::serve_file::serve_file;
|
2025-08-02 00:18:09 +03:00
|
|
|
|
use crate::lookup::{find_file_by_pattern, get_mime_type};
|
2024-10-22 20:35:51 +03:00
|
|
|
|
use crate::s3_utils::{check_file_exists, load_file_from_s3, upload_to_s3};
|
2025-09-02 14:00:54 +03:00
|
|
|
|
use crate::thumbnail::parse_file_path;
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2025-09-02 11:40:43 +03:00
|
|
|
|
// Удалена дублирующая функция, используется из common модуля
|
2025-09-01 20:36:15 +03:00
|
|
|
|
|
2025-09-02 14:00:54 +03:00
|
|
|
|
/// Обработчик для скачивания файла
|
|
|
|
|
|
/// без генерации миниатюр - это делает Vercel Edge API
|
2025-09-01 23:33:27 +03:00
|
|
|
|
#[allow(clippy::collapsible_if)]
|
2024-10-22 00:36:42 +03:00
|
|
|
|
pub async fn proxy_handler(
|
2024-10-23 20:06:34 +03:00
|
|
|
|
req: HttpRequest,
|
2024-10-22 09:38:30 +03:00
|
|
|
|
requested_res: web::Path<String>,
|
2024-10-22 00:36:42 +03:00
|
|
|
|
state: web::Data<AppState>,
|
|
|
|
|
|
) -> Result<HttpResponse, actix_web::Error> {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
|
info!("GET {} [START]", requested_res);
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2024-10-22 22:56:12 +03:00
|
|
|
|
let normalized_path = if requested_res.ends_with("/webp") {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
info!("Converting to WebP format: {}", requested_res);
|
2024-10-22 22:56:12 +03:00
|
|
|
|
requested_res.replace("/webp", "")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
requested_res.to_string()
|
2024-10-22 00:36:42 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-02 11:40:43 +03:00
|
|
|
|
// Парсим GET запрос
|
2024-10-23 15:31:33 +03:00
|
|
|
|
let (base_filename, requested_width, extension) = parse_file_path(&normalized_path);
|
2024-10-23 15:02:33 +03:00
|
|
|
|
let ext = extension.as_str().to_lowercase();
|
2024-10-23 14:45:23 +03:00
|
|
|
|
let filekey = format!("{}.{}", base_filename, &ext);
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
|
"Parsed request - base: {}, width: {}, ext: {}",
|
|
|
|
|
|
base_filename, requested_width, ext
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-09-02 14:00:54 +03:00
|
|
|
|
// Caching handled by Vercel Edge - focus on fast file serving
|
2024-11-13 11:14:53 +03: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)) => {
|
2025-08-12 14:13:35 +03:00
|
|
|
|
if let Some(found_ext) = found_file.split('.').next_back() {
|
2024-11-13 11:14:53 +03:00
|
|
|
|
get_mime_type(found_ext)
|
|
|
|
|
|
.unwrap_or("application/octet-stream")
|
|
|
|
|
|
.to_string()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"application/octet-stream".to_string()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
error!("Unsupported file format for: {}", base_filename);
|
|
|
|
|
|
return Err(ErrorInternalServerError("Unsupported file format"));
|
2024-11-13 11:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-23 13:31:05 +03:00
|
|
|
|
};
|
2024-10-22 20:42:45 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
info!("Content-Type: {}", content_type);
|
2024-10-22 20:35:51 +03:00
|
|
|
|
|
2024-10-23 13:31:05 +03:00
|
|
|
|
return match state.get_path(&filekey).await {
|
|
|
|
|
|
Ok(Some(stored_path)) => {
|
2024-11-11 14:03:20 +03:00
|
|
|
|
warn!("Found stored path in DB: {}", stored_path);
|
2025-08-02 00:18:09 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"Checking Storj path - bucket: {}, path: {}",
|
|
|
|
|
|
state.bucket, stored_path
|
|
|
|
|
|
);
|
2024-10-23 13:31:05 +03:00
|
|
|
|
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
|
2024-11-11 14:03:20 +03:00
|
|
|
|
warn!("File exists in Storj: {}", stored_path);
|
2025-09-02 14:00:54 +03:00
|
|
|
|
// Просто отдаем файл, миниатюры генерирует Vercel Edge API
|
|
|
|
|
|
serve_file(&stored_path, &state, &req).await
|
2024-11-11 14:03:20 +03:00
|
|
|
|
} else {
|
2025-08-02 00:18:09 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"Attempting to load from AWS - bucket: {}, path: {}",
|
|
|
|
|
|
state.bucket, stored_path
|
|
|
|
|
|
);
|
2024-11-11 14:15:45 +03:00
|
|
|
|
|
|
|
|
|
|
// Определяем тип медиа из content_type
|
|
|
|
|
|
let media_type = content_type.split("/").next().unwrap_or("image");
|
|
|
|
|
|
|
2024-11-12 12:13:15 +03:00
|
|
|
|
// Создаем варианты путей с обоими регистрами расширения
|
|
|
|
|
|
let paths_lower = vec![
|
2024-11-11 14:11:11 +03:00
|
|
|
|
stored_path.clone(),
|
2024-11-13 12:03:32 +03:00
|
|
|
|
// format!("production/{}", stored_path),
|
2025-08-02 00:18:09 +03:00
|
|
|
|
format!("production/{}/{}", media_type, stored_path),
|
2024-11-11 14:11:11 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
2024-11-12 12:13:15 +03: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 12:03:32 +03:00
|
|
|
|
// format!("production/{}", orig_stored_path),
|
2025-08-02 00:18:09 +03:00
|
|
|
|
format!("production/{}/{}", media_type, orig_stored_path),
|
2024-11-12 12:13:15 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// Объединяем все пути для проверки
|
|
|
|
|
|
let all_paths = paths_lower.into_iter().chain(paths_orig.into_iter());
|
|
|
|
|
|
|
|
|
|
|
|
for path in all_paths {
|
2024-11-11 14:11:11 +03:00
|
|
|
|
warn!("Trying AWS path: {}", path);
|
|
|
|
|
|
match load_file_from_s3(&state.aws_client, &state.bucket, &path).await {
|
|
|
|
|
|
Ok(filedata) => {
|
2025-08-02 00:18:09 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"Successfully loaded file from AWS, size: {} bytes",
|
|
|
|
|
|
filedata.len()
|
|
|
|
|
|
);
|
2024-11-11 14:11:11 +03:00
|
|
|
|
warn!("Attempting to upload to Storj with key: {}", filekey);
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2024-11-11 14:11:11 +03:00
|
|
|
|
if let Err(e) = upload_to_s3(
|
|
|
|
|
|
&state.storj_client,
|
|
|
|
|
|
&state.bucket,
|
|
|
|
|
|
&filekey,
|
|
|
|
|
|
filedata.clone(),
|
2024-11-13 11:14:53 +03:00
|
|
|
|
&content_type,
|
2025-08-02 00:18:09 +03:00
|
|
|
|
)
|
|
|
|
|
|
.await
|
|
|
|
|
|
{
|
2024-11-11 14:11:11 +03:00
|
|
|
|
error!("Failed to upload to Storj: {} - Error: {}", filekey, e);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
warn!("Successfully uploaded to Storj: {}", filekey);
|
|
|
|
|
|
}
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
let elapsed = start_time.elapsed();
|
|
|
|
|
|
info!("File served from AWS in {:?}: {}", elapsed, path);
|
2025-09-02 14:00:54 +03:00
|
|
|
|
return Ok(create_file_response_with_analytics(
|
|
|
|
|
|
&content_type,
|
|
|
|
|
|
filedata,
|
|
|
|
|
|
&req,
|
|
|
|
|
|
&path,
|
|
|
|
|
|
));
|
2024-11-11 14:11:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
warn!("Failed to load from AWS path {}: {:?}", path, err);
|
|
|
|
|
|
continue;
|
2024-11-11 14:05:21 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-11 14:11:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
error!("Failed to load from any AWS path for: {}", stored_path);
|
|
|
|
|
|
Err(ErrorInternalServerError("Failed to load file from AWS"))
|
2024-11-11 14:05:21 +03:00
|
|
|
|
}
|
2024-10-23 13:31:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
Ok(None) => {
|
2024-11-11 14:03:20 +03:00
|
|
|
|
warn!("No stored path found in DB for: {}", filekey);
|
2024-10-23 16:38:34 +03:00
|
|
|
|
let ct_parts = content_type.split("/").collect::<Vec<&str>>();
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2024-11-12 12:10:14 +03:00
|
|
|
|
// Создаем два варианта пути - с оригинальным расширением и с нижним регистром
|
|
|
|
|
|
let filepath_lower = format!("production/{}/{}.{}", ct_parts[0], base_filename, ext);
|
2025-08-02 00:18:09 +03:00
|
|
|
|
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
|
|
|
|
|
|
);
|
2024-11-12 12:10:14 +03:00
|
|
|
|
|
|
|
|
|
|
// Проверяем существование файла с обоими вариантами расширения
|
2025-08-02 00:18:09 +03:00
|
|
|
|
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?;
|
|
|
|
|
|
|
2024-11-12 12:10:14 +03:00
|
|
|
|
let filepath = if exists_in_aws_orig {
|
|
|
|
|
|
filepath_orig
|
|
|
|
|
|
} else if exists_in_aws_lower {
|
|
|
|
|
|
filepath_lower
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Если файл не найден ни с одним из расширений, используем нижний регистр по умолчанию
|
|
|
|
|
|
filepath_lower
|
|
|
|
|
|
};
|
2024-10-23 16:38:34 +03:00
|
|
|
|
|
2025-08-02 00:18:09 +03:00
|
|
|
|
let exists_in_storj =
|
|
|
|
|
|
check_file_exists(&state.storj_client, &state.bucket, &filepath).await?;
|
2024-11-07 21:30:53 +03:00
|
|
|
|
warn!("Checking existence in Storj: {}", exists_in_storj);
|
2024-10-23 16:38:34 +03:00
|
|
|
|
|
2024-10-23 17:46:30 +03:00
|
|
|
|
if exists_in_storj {
|
2025-09-02 14:00:54 +03:00
|
|
|
|
warn!("file {} exists in storj, serving directly", filepath);
|
|
|
|
|
|
// Файл существует в Storj, отдаем его напрямую
|
|
|
|
|
|
return serve_file(&filepath, &state, &req).await;
|
2024-10-23 17:46:30 +03:00
|
|
|
|
} else {
|
|
|
|
|
|
warn!("file {} does not exist in storj", filepath);
|
2024-10-23 16:38:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-02 00:18:09 +03:00
|
|
|
|
let exists_in_aws =
|
|
|
|
|
|
check_file_exists(&state.aws_client, &state.bucket, &filepath).await?;
|
2024-11-07 21:30:53 +03:00
|
|
|
|
warn!("Checking existence in AWS: {}", exists_in_aws);
|
|
|
|
|
|
|
2024-10-23 17:46:30 +03:00
|
|
|
|
if exists_in_aws {
|
2024-11-11 14:03:20 +03:00
|
|
|
|
warn!("File found in AWS, attempting to download: {}", filepath);
|
2024-10-23 17:46:30 +03:00
|
|
|
|
match load_file_from_s3(&state.aws_client, &state.bucket, &filepath).await {
|
|
|
|
|
|
Ok(filedata) => {
|
2025-08-02 00:18:09 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"Successfully downloaded file from AWS, size: {} bytes",
|
|
|
|
|
|
filedata.len()
|
|
|
|
|
|
);
|
2024-10-23 17:46:30 +03:00
|
|
|
|
if let Err(e) = upload_to_s3(
|
|
|
|
|
|
&state.storj_client,
|
|
|
|
|
|
&state.bucket,
|
|
|
|
|
|
&filekey,
|
|
|
|
|
|
filedata.clone(),
|
2024-11-13 11:14:53 +03:00
|
|
|
|
&content_type,
|
2024-10-23 17:46:30 +03:00
|
|
|
|
)
|
2025-08-02 00:18:09 +03:00
|
|
|
|
.await
|
|
|
|
|
|
{
|
2024-10-23 17:46:30 +03:00
|
|
|
|
warn!("cannot upload to storj: {}", e);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
warn!("file {} uploaded to storj", filekey);
|
2024-10-23 21:41:29 +03:00
|
|
|
|
state.set_path(&filekey, &filepath).await;
|
2024-10-23 17:46:30 +03:00
|
|
|
|
}
|
2025-09-01 20:36:15 +03:00
|
|
|
|
let elapsed = start_time.elapsed();
|
|
|
|
|
|
info!("File served from AWS in {:?}: {}", elapsed, filepath);
|
2025-09-02 14:00:54 +03:00
|
|
|
|
Ok(create_file_response_with_analytics(
|
|
|
|
|
|
&content_type,
|
|
|
|
|
|
filedata,
|
|
|
|
|
|
&req,
|
|
|
|
|
|
&filepath,
|
|
|
|
|
|
))
|
2025-08-02 00:18:09 +03:00
|
|
|
|
}
|
2024-10-23 17:46:30 +03:00
|
|
|
|
Err(e) => {
|
2024-11-11 14:03:20 +03:00
|
|
|
|
error!("Failed to download from AWS: {} - Error: {}", filepath, e);
|
2024-10-23 17:46:30 +03:00
|
|
|
|
Err(ErrorInternalServerError(e))
|
2025-08-02 00:18:09 +03:00
|
|
|
|
}
|
2024-10-23 17:46:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-11-11 14:03:20 +03:00
|
|
|
|
error!("File not found in either Storj or AWS: {}", filepath);
|
2024-10-23 17:46:30 +03:00
|
|
|
|
Err(ErrorNotFound("file does not exist"))
|
2024-10-22 20:35:51 +03:00
|
|
|
|
}
|
2025-08-02 00:18:09 +03:00
|
|
|
|
}
|
2024-10-23 15:45:05 +03:00
|
|
|
|
Err(e) => {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
let elapsed = start_time.elapsed();
|
2025-08-02 00:18:09 +03:00
|
|
|
|
error!(
|
2025-09-01 20:36:15 +03:00
|
|
|
|
"Database error while getting path: {} in {:?} - Full error: {:?}",
|
|
|
|
|
|
filekey, elapsed, e
|
2025-08-02 00:18:09 +03:00
|
|
|
|
);
|
2024-10-23 15:45:05 +03:00
|
|
|
|
Err(ErrorInternalServerError(e))
|
|
|
|
|
|
}
|
2025-08-02 00:18:09 +03:00
|
|
|
|
};
|
2024-10-22 19:34:08 +03:00
|
|
|
|
}
|