From 57b1113cc34b27d44609895de3ef2cc2b632508a Mon Sep 17 00:00:00 2001 From: eraden Date: Sat, 16 Nov 2024 05:25:39 +0100 Subject: [PATCH] Update recipe image --- cooked/src/routes.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/cooked/src/routes.rs b/cooked/src/routes.rs index 515f9bf..01b09f6 100644 --- a/cooked/src/routes.rs +++ b/cooked/src/routes.rs @@ -380,7 +380,7 @@ async fn recipe_image_update( MultipartForm(form): MultipartForm, path: Path, db: Data, - admin: Identity, + _admin: Identity, ) -> HttpResponse { use sea_orm::ActiveValue::Set; @@ -392,25 +392,35 @@ async fn recipe_image_update( return HttpResponse::BadRequest().finish(); }; tracing::debug!("Ensure production assets directory exists"); - let _ = tokio::fs::create_dir_all("./assets/recipies/images").await; + let _ = tokio::fs::create_dir_all("./assets/recipies/images") + .await + .inspect_err(|e| tracing::error!("Failed to create recipies image dir: {e}")); tracing::debug!("Ensure tmp assets directory exists"); - let _ = tokio::fs::create_dir_all("/tmp/assets/recipies/images").await; + let _ = tokio::fs::create_dir_all("/tmp/assets/recipies/images") + .await + .inspect_err(|e| tracing::error!("Failed to create recipies tmp image dir: {e}")); tracing::debug!("Storing TempFile"); let Ok((_file, path)) = form.image.file.keep() else { tracing::warn!("Storing TempFile failed"); - return HttpResponse::InternalServerError().finish(); + return HttpResponse::InternalServerError().body("Failed to keep tmp file"); }; let uid = uuid::Uuid::new_v4(); tracing::debug!("Copying TempFile to tmp directory"); let recipe_image_path = format!("/assets/recipies/images/{uid}"); - let Ok(_) = tokio::fs::copy(&path, format!(".{}", recipe_image_path)).await else { - tracing::warn!("Copy TempFile to tmp directory failed"); - return HttpResponse::InternalServerError().finish(); + let write_path = format!(".{}", recipe_image_path); + let Ok(_) = tokio::fs::copy(&path, &write_path).await.inspect_err(|e| { + tracing::error!("Failed to store recipe image ({path:?} => {write_path}): {e}") + }) else { + return HttpResponse::InternalServerError().body("Failed to write recipe image"); }; let mut model = recipe.into_active_model(); model.image_url = Set(recipe_image_path); - let Ok(_) = Recipies::update(model).exec(&**db).await else { - return HttpResponse::InternalServerError().finish(); + let Ok(_) = Recipies::update(model) + .exec(&**db) + .await + .inspect_err(|e| tracing::error!("Failed to update recipe image_url: {e}")) + else { + return HttpResponse::InternalServerError().body("Failed to update recipe image url"); }; tracing::debug!("Copying TempFile to tmp directory"); let _ = tokio::fs::remove_file(&path).await;