proxy-reworked
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
use actix_web::{error::ErrorInternalServerError, web, HttpRequest, HttpResponse, Result};
|
||||
use log::{info, warn, error};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::thumbnail::{parse_thumbnail_request, find_closest_width, generate_thumbnails};
|
||||
use crate::s3_utils::{load_file_from_s3, upload_to_s3};
|
||||
use crate::thumbnail::{find_closest_width, generate_thumbnails, parse_image_request};
|
||||
use crate::s3_utils::{check_file_exists, load_file_from_s3, upload_to_s3};
|
||||
use crate::handlers::serve_file::serve_file;
|
||||
|
||||
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
|
||||
@@ -12,82 +11,105 @@ pub async fn proxy_handler(
|
||||
requested_res: web::Path<String>,
|
||||
state: web::Data<AppState>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
info!("requested_path: {}", requested_res);
|
||||
let requested_path = requested_res.replace("/webp", "");
|
||||
let parts = requested_path.split('/').collect::<Vec<&str>>(); // Explicit type annotation
|
||||
let filename = parts[parts.len()-1];
|
||||
|
||||
let stored_path = match state.get_path(&filename).await {
|
||||
Ok(Some(path)) => path,
|
||||
Ok(None) => {
|
||||
warn!("wrong filename: {}", filename);
|
||||
return Ok(HttpResponse::NotFound().finish());
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("error: {}", e);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
let normalized_path = match requested_res.ends_with("/webp") {
|
||||
true => requested_res.replace("/webp", ""),
|
||||
false => requested_res.to_string(),
|
||||
};
|
||||
info!("stored path: {}", stored_path);
|
||||
|
||||
// Проверяем, запрошена ли миниатюра
|
||||
if let Some((base_filename, requested_width, extension)) =
|
||||
parse_thumbnail_request(&requested_res)
|
||||
{
|
||||
info!("thumbnail requested: {} width: {} ext: {}", base_filename, requested_width, extension);
|
||||
// парсим GET запрос
|
||||
if let Some((base_filename, requested_width, extension)) = parse_image_request(&normalized_path) {
|
||||
let filekey = format!("{}.{}", base_filename, extension);
|
||||
let content_type = match extension.as_str() {
|
||||
"jpg" | "jpeg" => "image/jpeg",
|
||||
"png" => "image/png",
|
||||
"webp" => "image/webp",
|
||||
"gif" => "image/gif",
|
||||
"mp3" => "audio/mpeg",
|
||||
"wav" => "audio/x-wav",
|
||||
"ogg" => "audio/ogg",
|
||||
"aac" => "audio/aac",
|
||||
"m4a" => "audio/m4a",
|
||||
"flac" => "audio/flac",
|
||||
_ => return Err(ErrorInternalServerError("unsupported file format"))
|
||||
};
|
||||
|
||||
// Находим ближайший подходящий размер
|
||||
let closest_width = find_closest_width(requested_width);
|
||||
let thumb_filename = format!("{}_{}.jpg", base_filename, closest_width);
|
||||
info!("closest width: {}, thumb_filename: {}", closest_width, thumb_filename);
|
||||
return match state.get_path(&filekey).await {
|
||||
Ok(Some(stored_path)) => {
|
||||
|
||||
// Проверяем наличие миниатюры в кэше
|
||||
let cached_files = state.get_cached_file_list().await;
|
||||
if !cached_files.contains(&thumb_filename) {
|
||||
info!("no thumb found");
|
||||
if cached_files.contains(&base_filename) {
|
||||
info!("no original file found");
|
||||
// Загружаем оригинальный файл из S3
|
||||
let original_data: Vec<u8> =
|
||||
load_file_from_s3(&state.storj_client, &state.storj_bucket, &base_filename).await?;
|
||||
// we have stored file path in storj
|
||||
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
|
||||
if content_type.starts_with("image") {
|
||||
return match requested_width == 0 {
|
||||
true => serve_file(&stored_path, &state).await,
|
||||
false => {
|
||||
// find closest thumb width
|
||||
let closest: u32 = find_closest_width(requested_width as u32);
|
||||
let thumb_filename = &format!("{}_{}.{}", base_filename, closest, extension);
|
||||
|
||||
return match check_file_exists(&state.storj_client, &state.bucket, thumb_filename).await {
|
||||
Ok(true) => serve_file(thumb_filename, &state).await,
|
||||
Ok(false) => {
|
||||
if let Ok(filedata) = load_file_from_s3(
|
||||
&state.storj_client, &state.bucket, &stored_path).await {
|
||||
thumbdata_save(&filedata, &state, &base_filename, content_type).await;
|
||||
serve_file(thumb_filename, &state).await
|
||||
} else {
|
||||
Err(ErrorInternalServerError("cannot generate thumbnail"))
|
||||
}
|
||||
}
|
||||
Err(_) => Err(ErrorInternalServerError("failed to load thumbnail"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// not image passing thumb generation
|
||||
}
|
||||
|
||||
// Генерируем миниатюру для ближайшего подходящего размера
|
||||
let image = image::load_from_memory(&original_data).map_err(|_| {
|
||||
ErrorInternalServerError("Failed to load image for thumbnail generation")
|
||||
})?;
|
||||
let thumbnails_bytes =
|
||||
generate_thumbnails(&image).await?;
|
||||
let thumbnail_bytes = thumbnails_bytes[&closest_width].clone();
|
||||
|
||||
// Загружаем миниатюру в S3
|
||||
upload_to_s3(
|
||||
&state.storj_client,
|
||||
&state.storj_bucket,
|
||||
&thumb_filename,
|
||||
thumbnail_bytes.clone(),
|
||||
"image/jpeg",
|
||||
)
|
||||
.await?;
|
||||
info!("thumb was saved in storj");
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("image/jpeg")
|
||||
.body(thumbnail_bytes));
|
||||
} else {
|
||||
warn!("original was not found");
|
||||
}
|
||||
} else {
|
||||
info!("thumb was found");
|
||||
return serve_file(&thumb_filename, &state).await;
|
||||
// we need to download what stored_path keeping in aws
|
||||
return match load_file_from_s3(
|
||||
&state.aws_client,
|
||||
&state.bucket,
|
||||
&stored_path).await {
|
||||
Ok(filedata) => {
|
||||
let _ = upload_to_s3(&state.storj_client, &state.bucket, &filekey, filedata.clone(), content_type).await;
|
||||
thumbdata_save(&filedata, &state, &base_filename, content_type).await;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(content_type)
|
||||
.body(filedata)
|
||||
)
|
||||
},
|
||||
Err(err) => Err(ErrorInternalServerError(err)),
|
||||
}
|
||||
},
|
||||
Ok(None) => Err(ErrorInternalServerError("requested file path was not found")),
|
||||
Err(e) => Err(ErrorInternalServerError(e))
|
||||
}
|
||||
}
|
||||
|
||||
// Если запрошен целый файл
|
||||
info!("serving full file: {}", requested_path);
|
||||
match serve_file(&requested_path, &state).await {
|
||||
Ok(response) => Ok(response),
|
||||
Err(e) => {
|
||||
error!("error: {}", e);
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
Err(ErrorInternalServerError("invalid file key"))
|
||||
|
||||
}
|
||||
|
||||
async fn thumbdata_save(original_data: &[u8], state: &AppState, original_filename: &str, content_type: &str) {
|
||||
if content_type.starts_with("image") {
|
||||
let ext = original_filename.split('.').last().unwrap();
|
||||
let img = image::load_from_memory(&original_data).unwrap();
|
||||
|
||||
if let Ok(thumbnails_bytes) = generate_thumbnails(&img).await {
|
||||
for (thumb_width, thumbnail) in thumbnails_bytes {
|
||||
let thumb_filename = &format!("{}_{}.{}", original_filename, thumb_width, ext);
|
||||
let thumbnail_bytes = thumbnail.clone();
|
||||
// Загружаем миниатюру в S3
|
||||
let _ = upload_to_s3(
|
||||
&state.storj_client,
|
||||
&state.bucket,
|
||||
thumb_filename,
|
||||
thumbnail_bytes,
|
||||
content_type,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user