quoter/src/handlers/proxy.rs

184 lines
8.1 KiB
Rust
Raw Normal View History

2024-10-21 21:36:42 +00:00
use actix_web::{error::ErrorInternalServerError, web, HttpRequest, HttpResponse, Result};
2024-10-22 17:42:45 +00:00
use log::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};
use crate::thumbnail::{find_closest_width, generate_thumbnails, parse_image_request};
2024-10-21 21:36:42 +00:00
/// Обработчик для скачивания файла и генерации миниатюры, если она недоступна.
pub async fn proxy_handler(
2024-10-22 10:18:57 +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-10-22 18:19:40 +00:00
warn!("\t>>>\tGET {}", requested_res);
2024-10-22 16:34:08 +00:00
let normalized_path = match requested_res.ends_with("/webp") {
true => requested_res.replace("/webp", ""),
false => requested_res.to_string(),
2024-10-21 21:36:42 +00:00
};
2024-10-22 16:34:08 +00:00
// парсим GET запрос
2024-10-22 17:35:51 +00:00
if let Some((base_filename, requested_width, extension)) = parse_image_request(&normalized_path)
{
2024-10-22 16:34:08 +00:00
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",
2024-10-22 17:35:51 +00:00
_ => return Err(ErrorInternalServerError("unsupported file format")),
2024-10-22 16:34:08 +00:00
};
2024-10-21 21:36:42 +00:00
2024-10-22 17:42:45 +00:00
warn!("content_type: {}", content_type);
2024-10-22 16:34:08 +00:00
return match state.get_path(&filekey).await {
Ok(Some(stored_path)) => {
2024-10-22 17:42:45 +00:00
warn!("stored_path: {}", stored_path);
2024-10-22 16:34:08 +00:00
// 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);
2024-10-22 17:35:51 +00:00
let thumb_filename =
&format!("{}_{}.{}", base_filename, closest, extension);
return match check_file_exists(
&state.storj_client,
&state.bucket,
thumb_filename,
)
.await
{
2024-10-22 17:50:06 +00:00
Ok(true) => {
warn!("serve existed thumb file: {}", thumb_filename);
serve_file(thumb_filename, &state).await
},
2024-10-22 16:34:08 +00:00
Ok(false) => {
if let Ok(filedata) = load_file_from_s3(
2024-10-22 17:35:51 +00:00
&state.storj_client,
&state.bucket,
&stored_path,
)
.await
{
2024-10-22 18:31:38 +00:00
warn!("generate new thumb files: {}", stored_path);
warn!("{} bytes", filedata.len());
2024-10-22 17:35:51 +00:00
thumbdata_save(
filedata.clone(),
&state,
2024-10-22 18:15:29 +00:00
&filekey,
2024-10-22 17:35:51 +00:00
content_type.to_string(),
)
.await;
2024-10-22 17:50:06 +00:00
warn!("serve new thumb file: {}", thumb_filename);
2024-10-22 17:35:51 +00:00
serve_file(thumb_filename, &state).await
} else {
Err(ErrorInternalServerError(
"cannot generate thumbnail",
))
}
2024-10-22 16:34:08 +00:00
}
2024-10-22 17:35:51 +00:00
Err(_) => {
Err(ErrorInternalServerError("failed to load thumbnail"))
}
};
2024-10-22 16:34:08 +00:00
}
2024-10-22 17:35:51 +00:00
};
2024-10-22 16:34:08 +00:00
}
// not image passing thumb generation
}
2024-10-21 21:36:42 +00:00
2024-10-22 16:34:08 +00:00
// we need to download what stored_path keeping in aws
2024-10-22 17:35:51 +00:00
return match load_file_from_s3(&state.aws_client, &state.bucket, &stored_path).await
{
Ok(filedata) => {
2024-10-22 17:42:45 +00:00
warn!("download stored_path from aws: {:?}", stored_path);
2024-10-22 18:23:34 +00:00
if let Err(e) = upload_to_s3(
2024-10-22 17:35:51 +00:00
&state.storj_client,
&state.bucket,
&filekey,
filedata.clone(),
content_type,
)
2024-10-22 18:23:34 +00:00
.await {
warn!("cannot upload to storj: {}", e);
} else {
warn!("file {} uploaded to storj", filekey);
}
2024-10-22 18:15:29 +00:00
thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string())
2024-10-22 17:35:51 +00:00
.await;
Ok(HttpResponse::Ok().content_type(content_type).body(filedata))
2024-10-22 16:34:08 +00:00
}
2024-10-22 17:35:51 +00:00
Err(err) => Err(ErrorInternalServerError(err)),
};
}
Ok(None) => Err(ErrorInternalServerError(
"requested file path was not found",
)),
Err(e) => Err(ErrorInternalServerError(e)),
};
2024-10-22 16:34:08 +00:00
}
2024-10-21 21:36:42 +00:00
2024-10-22 16:34:08 +00:00
Err(ErrorInternalServerError("invalid file key"))
}
2024-10-22 17:35:51 +00:00
async fn thumbdata_save(
original_data: Vec<u8>,
state: &AppState,
original_filename: &str,
content_type: String,
) {
let state = state.clone();
2024-10-22 16:34:08 +00:00
if content_type.starts_with("image") {
2024-10-22 18:22:07 +00:00
warn!("original file name: {}", original_filename);
2024-10-22 18:31:38 +00:00
let (base_filename, _, extension) = parse_image_request(&original_filename).unwrap();
let filename = format!("{}.{}", base_filename, extension);
2024-10-22 18:11:35 +00:00
2024-10-22 18:31:38 +00:00
warn!("file extension: {}", extension);
2024-10-22 18:11:35 +00:00
let img = match image::load_from_memory(&original_data) {
Ok(img) => img,
Err(e) => {
warn!("cannot load image from memory: {}", e);
return;
}
};
2024-10-22 18:33:42 +00:00
//actix::spawn(async move {
2024-10-22 18:11:35 +00:00
match generate_thumbnails(&img).await {
Ok(thumbnails_bytes) => {
for (thumb_width, thumbnail) in thumbnails_bytes {
2024-10-22 18:31:38 +00:00
let thumb_filename = format!("{}_{}.{}", base_filename, thumb_width, extension);
2024-10-22 18:11:35 +00:00
// Загружаем миниатюру в S3
if let Err(e) = upload_to_s3(
&state.storj_client,
&state.bucket,
&thumb_filename,
thumbnail,
&content_type,
)
.await
{
warn!("cannot load thumb {}: {}", thumb_filename, e);
2024-10-22 18:22:07 +00:00
} else {
warn!("thumb {} uploaded to storj", thumb_filename);
2024-10-22 18:11:35 +00:00
}
}
}
Err(e) => {
warn!("cannot generate thumbnails for {}: {}", filename, e);
2024-10-22 17:35:51 +00:00
}
2024-10-21 21:36:42 +00:00
}
2024-10-22 18:33:42 +00:00
//});
2024-10-21 21:36:42 +00:00
}
2024-10-22 18:11:35 +00:00
}