less-upload
All checks were successful
deploy / deploy (push) Successful in 4m32s

This commit is contained in:
Untone 2024-10-23 20:32:07 +03:00
parent 6e90529420
commit 4db1ac6ff7

View File

@ -56,62 +56,49 @@ pub async fn proxy_handler(
return match state.get_path(&filekey).await { return match state.get_path(&filekey).await {
Ok(Some(stored_path)) => { Ok(Some(stored_path)) => {
warn!("stored_path: {}", stored_path); warn!("stored_path: {}", stored_path);
// we have stored file path in storj // Проверяем, существует ли файл в Storj
if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? { if check_file_exists(&state.storj_client, &state.bucket, &stored_path).await? {
if content_type.starts_with("image") { if content_type.starts_with("image") {
return match requested_width == 0 { if requested_width == 0 {
true => serve_file(&stored_path, &state, shout_id).await, return serve_file(&stored_path, &state, shout_id).await;
false => { } else {
// find closest thumb width // Находим ближайшую ширину для миниатюры
let closest: u32 = find_closest_width(requested_width as u32); let closest: u32 = find_closest_width(requested_width as u32);
let thumb_filename = let thumb_filename = &format!("{}_{}.{}", base_filename, closest, ext);
&format!("{}_{}.{}", base_filename, closest, ext);
return match check_file_exists( // Проверяем, существует ли уже миниатюра в Storj
&state.storj_client, match check_file_exists(&state.storj_client, &state.bucket, thumb_filename).await {
&state.bucket,
thumb_filename,
)
.await
{
Ok(true) => { Ok(true) => {
warn!("serve existed thumb file: {}", thumb_filename); warn!("serve existed thumb file: {}", thumb_filename);
serve_file(thumb_filename, &state, shout_id).await return serve_file(thumb_filename, &state, shout_id).await;
}, },
Ok(false) => { Ok(false) => {
if let Ok(filedata) = load_file_from_s3( // Миниатюра не существует, создаем и загружаем её
&state.storj_client, if let Ok(filedata) = load_file_from_s3(&state.storj_client, &state.bucket, &stored_path).await {
&state.bucket,
&stored_path,
)
.await
{
warn!("generate new thumb files: {}", stored_path); warn!("generate new thumb files: {}", stored_path);
warn!("{} bytes", filedata.len()); warn!("{} bytes", filedata.len());
let _ = thumbdata_save(
filedata.clone(), // Генерируем миниатюру и сохраняем
&state, if let Ok(_) = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string()).await {
&filekey,
content_type.to_string(),
)
.await;
warn!("serve new thumb file: {}", thumb_filename); warn!("serve new thumb file: {}", thumb_filename);
serve_file(thumb_filename, &state, shout_id).await return serve_file(thumb_filename, &state, shout_id).await;
} else { } else {
error!("cannot generate thumbnail"); error!("cannot generate thumbnail");
Err(ErrorInternalServerError( return Err(ErrorInternalServerError("cannot generate thumbnail"));
"cannot generate thumbnail", }
)) } else {
error!("cannot load file from Storj to generate thumbnail");
return Err(ErrorInternalServerError("cannot generate thumbnail"));
} }
} }
Err(_) => { Err(e) => {
Err(ErrorInternalServerError("failed to load thumbnail")) error!("ошибка при проверке существования миниатюры: {}", e);
return Err(ErrorInternalServerError("failed to load thumbnail"));
} }
};
} }
};
} }
// not image passing thumb generation }
// Если файл не изображение, продолжаем обработку
warn!("file is not an image"); warn!("file is not an image");
} }