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