docs
This commit is contained in:
@@ -4,9 +4,9 @@ use log::{error, warn};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::handlers::serve_file::serve_file;
|
||||
use crate::lookup::{find_file_by_pattern, get_mime_type};
|
||||
use crate::s3_utils::{check_file_exists, load_file_from_s3, upload_to_s3};
|
||||
use crate::thumbnail::{find_closest_width, parse_file_path, thumbdata_save};
|
||||
use crate::lookup::{find_file_by_pattern, get_mime_type};
|
||||
|
||||
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
|
||||
pub async fn proxy_handler(
|
||||
@@ -56,14 +56,22 @@ pub async fn proxy_handler(
|
||||
warn!("content_type: {}", content_type);
|
||||
|
||||
let shout_id = match req.query_string().contains("s=") {
|
||||
true => req.query_string().split("s=").collect::<Vec<&str>>().pop().unwrap_or(""),
|
||||
false => ""
|
||||
true => req
|
||||
.query_string()
|
||||
.split("s=")
|
||||
.collect::<Vec<&str>>()
|
||||
.pop()
|
||||
.unwrap_or(""),
|
||||
false => "",
|
||||
};
|
||||
|
||||
return match state.get_path(&filekey).await {
|
||||
Ok(Some(stored_path)) => {
|
||||
warn!("Found stored path in DB: {}", stored_path);
|
||||
warn!("Checking Storj path - bucket: {}, path: {}", state.bucket, stored_path);
|
||||
warn!(
|
||||
"Checking Storj path - bucket: {}, path: {}",
|
||||
state.bucket, stored_path
|
||||
);
|
||||
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") {
|
||||
@@ -73,20 +81,26 @@ pub async fn proxy_handler(
|
||||
serve_file(&stored_path, &state, shout_id).await
|
||||
} else {
|
||||
let closest: u32 = find_closest_width(requested_width as u32);
|
||||
warn!("Calculated closest width: {} for requested: {}", closest, requested_width);
|
||||
warn!(
|
||||
"Calculated closest width: {} for requested: {}",
|
||||
closest, requested_width
|
||||
);
|
||||
let thumb_filename = &format!("{}_{}.{}", base_filename, closest, ext);
|
||||
warn!("Generated thumbnail filename: {}", thumb_filename);
|
||||
|
||||
// Проверяем, существует ли уже миниатюра в 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
|
||||
{
|
||||
Ok(true) => {
|
||||
warn!("serve existed thumb file: {}", thumb_filename);
|
||||
serve_file(thumb_filename, &state, shout_id).await
|
||||
},
|
||||
}
|
||||
Ok(false) => {
|
||||
// Миниатюра не существует, возвращаем оригинал и запускаем генерацию миниатюры
|
||||
let original_file = serve_file(&stored_path, &state, shout_id).await?;
|
||||
|
||||
let original_file =
|
||||
serve_file(&stored_path, &state, shout_id).await?;
|
||||
|
||||
// Запускаем асинхронную задачу для генерации миниатюры
|
||||
let state_clone = state.clone();
|
||||
let stored_path_clone = stored_path.clone();
|
||||
@@ -94,9 +108,22 @@ pub async fn proxy_handler(
|
||||
let content_type_clone = content_type.to_string();
|
||||
|
||||
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 {
|
||||
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 {
|
||||
if let Err(e) = thumbdata_save(
|
||||
filedata,
|
||||
&state_clone,
|
||||
&filekey_clone,
|
||||
content_type_clone,
|
||||
)
|
||||
.await
|
||||
{
|
||||
error!("Failed to generate thumbnail: {}", e);
|
||||
}
|
||||
}
|
||||
@@ -115,7 +142,10 @@ pub async fn proxy_handler(
|
||||
serve_file(&stored_path, &state, shout_id).await
|
||||
}
|
||||
} else {
|
||||
warn!("Attempting to load from AWS - bucket: {}, path: {}", state.bucket, stored_path);
|
||||
warn!(
|
||||
"Attempting to load from AWS - bucket: {}, path: {}",
|
||||
state.bucket, stored_path
|
||||
);
|
||||
|
||||
// Определяем тип медиа из content_type
|
||||
let media_type = content_type.split("/").next().unwrap_or("image");
|
||||
@@ -124,7 +154,7 @@ pub async fn proxy_handler(
|
||||
let paths_lower = vec![
|
||||
stored_path.clone(),
|
||||
// format!("production/{}", stored_path),
|
||||
format!("production/{}/{}", media_type, stored_path)
|
||||
format!("production/{}/{}", media_type, stored_path),
|
||||
];
|
||||
|
||||
// Создаем те же пути, но с оригинальным регистром расширения
|
||||
@@ -133,7 +163,7 @@ pub async fn proxy_handler(
|
||||
let paths_orig = vec![
|
||||
orig_stored_path.clone(),
|
||||
// format!("production/{}", orig_stored_path),
|
||||
format!("production/{}/{}", media_type, orig_stored_path)
|
||||
format!("production/{}/{}", media_type, orig_stored_path),
|
||||
];
|
||||
|
||||
// Объединяем все пути для проверки
|
||||
@@ -143,22 +173,29 @@ pub async fn proxy_handler(
|
||||
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!(
|
||||
"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(),
|
||||
&content_type,
|
||||
).await {
|
||||
)
|
||||
.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));
|
||||
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type(content_type)
|
||||
.body(filedata));
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("Failed to load from AWS path {}: {:?}", path, err);
|
||||
@@ -174,18 +211,23 @@ pub async fn proxy_handler(
|
||||
Ok(None) => {
|
||||
warn!("No stored path found in DB for: {}", filekey);
|
||||
let ct_parts = content_type.split("/").collect::<Vec<&str>>();
|
||||
|
||||
|
||||
// Создаем два варианта пути - с оригинальным расширением и с нижним регистром
|
||||
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 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 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 {
|
||||
@@ -195,15 +237,25 @@ pub async fn proxy_handler(
|
||||
filepath_lower
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
if exists_in_storj {
|
||||
warn!("file {} exists in storj, try to generate thumbnails", filepath);
|
||||
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;
|
||||
let _ = thumbdata_save(
|
||||
filedata.clone(),
|
||||
&state,
|
||||
&filekey,
|
||||
content_type.to_string(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("cannot download {} from storj: {}", filekey, e);
|
||||
@@ -214,16 +266,25 @@ pub async fn proxy_handler(
|
||||
warn!("file {} does not exist in storj", filepath);
|
||||
}
|
||||
|
||||
let exists_in_aws = check_file_exists(&state.aws_client, &state.bucket, &filepath).await?;
|
||||
let exists_in_aws =
|
||||
check_file_exists(&state.aws_client, &state.bucket, &filepath).await?;
|
||||
warn!("Checking existence in AWS: {}", 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 {
|
||||
Ok(filedata) => {
|
||||
warn!("Successfully downloaded file from AWS, size: {} bytes", filedata.len());
|
||||
let _ = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string())
|
||||
.await;
|
||||
warn!(
|
||||
"Successfully downloaded file from AWS, size: {} bytes",
|
||||
filedata.len()
|
||||
);
|
||||
let _ = thumbdata_save(
|
||||
filedata.clone(),
|
||||
&state,
|
||||
&filekey,
|
||||
content_type.to_string(),
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = upload_to_s3(
|
||||
&state.storj_client,
|
||||
&state.bucket,
|
||||
@@ -231,27 +292,31 @@ pub async fn proxy_handler(
|
||||
filedata.clone(),
|
||||
&content_type,
|
||||
)
|
||||
.await {
|
||||
.await
|
||||
{
|
||||
warn!("cannot upload to storj: {}", e);
|
||||
} else {
|
||||
warn!("file {} uploaded to storj", filekey);
|
||||
state.set_path(&filekey, &filepath).await;
|
||||
}
|
||||
Ok(HttpResponse::Ok().content_type(content_type).body(filedata))
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to download from AWS: {} - Error: {}", filepath, e);
|
||||
Err(ErrorInternalServerError(e))
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!("File not found in either Storj or AWS: {}", filepath);
|
||||
Err(ErrorNotFound("file does not exist"))
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Database error while getting path: {} - Full error: {:?}", filekey, e);
|
||||
error!(
|
||||
"Database error while getting path: {} - Full error: {:?}",
|
||||
filekey, e
|
||||
);
|
||||
Err(ErrorInternalServerError(e))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user