thumbfix7

This commit is contained in:
Untone 2024-11-07 21:22:34 +03:00
parent 8cde63383e
commit 30f591a34e
2 changed files with 19 additions and 19 deletions

View File

@ -25,9 +25,7 @@ pub struct ShoutAuthor {
pub struct Shout { pub struct Shout {
pub title: String, pub title: String,
pub created_at: String, pub created_at: String,
pub created_by: i32, pub main_topic: ShoutTopic,
pub main_topic: String,
pub topics: Vec<ShoutTopic>,
pub authors: Vec<ShoutAuthor>, pub authors: Vec<ShoutAuthor>,
pub layout: String, pub layout: String,
} }
@ -42,7 +40,7 @@ pub async fn get_shout_by_id(shout_id: i32) -> Result<Shout, Box<dyn Error>> {
} }
let gql = json!({ let gql = json!({
"query": format!("query {}($slug: String, $shout_id: Int) {{ {}(slug: $slug, shout_id: $shout_id) {{ title created_at created_by topics {{ title slug }} authors {{ id name }} }} }}", operation, query_name), "query": format!("query {}($slug: String, $shout_id: Int) {{ {}(slug: $slug, shout_id: $shout_id) {{ title created_at main_topic {{ title slug }} authors {{ id name }} }} }}", operation, query_name),
"operationName": operation, "operationName": operation,
"variables": variables "variables": variables
}); });

View File

@ -73,23 +73,25 @@ pub async fn proxy_handler(
return 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, &state.bucket, &stored_path).await { let original_file = serve_file(&stored_path, &state, shout_id).await?;
warn!("generate new thumb files: {}", stored_path);
warn!("{} bytes", filedata.len());
// Генерируем миниатюру и сохраняем // Запускаем асинхронную задачу для генерации миниатюры
if let Ok(_) = thumbdata_save(filedata.clone(), &state, &filekey, content_type.to_string()).await { let state_clone = state.clone();
warn!("serve new thumb file: {}", thumb_filename); let stored_path_clone = stored_path.clone();
return serve_file(thumb_filename, &state, shout_id).await; let filekey_clone = filekey.clone();
} else { let content_type_clone = content_type.to_string();
error!("cannot generate thumbnail");
return Err(ErrorInternalServerError("cannot generate thumbnail")); 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 {
warn!("generate new thumb files: {}", stored_path_clone);
if let Err(e) = thumbdata_save(filedata, &state_clone, &filekey_clone, content_type_clone).await {
error!("Failed to generate thumbnail: {}", e);
} }
} else {
error!("cannot load file from Storj to generate thumbnail");
return Err(ErrorInternalServerError("cannot generate thumbnail"));
} }
});
return Ok(original_file);
} }
Err(e) => { Err(e) => {
error!("ошибка при проверке существования миниатюры: {}", e); error!("ошибка при проверке существования миниатюры: {}", e);