Update recipe image

This commit is contained in:
eraden 2024-11-16 05:25:39 +01:00
parent 193ef00b72
commit 57b1113cc3

View File

@ -380,7 +380,7 @@ async fn recipe_image_update(
MultipartForm(form): MultipartForm<ImageUpload>, MultipartForm(form): MultipartForm<ImageUpload>,
path: Path<i32>, path: Path<i32>,
db: Data<DatabaseConnection>, db: Data<DatabaseConnection>,
admin: Identity, _admin: Identity,
) -> HttpResponse { ) -> HttpResponse {
use sea_orm::ActiveValue::Set; use sea_orm::ActiveValue::Set;
@ -392,25 +392,35 @@ async fn recipe_image_update(
return HttpResponse::BadRequest().finish(); return HttpResponse::BadRequest().finish();
}; };
tracing::debug!("Ensure production assets directory exists"); 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"); 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"); tracing::debug!("Storing TempFile");
let Ok((_file, path)) = form.image.file.keep() else { let Ok((_file, path)) = form.image.file.keep() else {
tracing::warn!("Storing TempFile failed"); tracing::warn!("Storing TempFile failed");
return HttpResponse::InternalServerError().finish(); return HttpResponse::InternalServerError().body("Failed to keep tmp file");
}; };
let uid = uuid::Uuid::new_v4(); let uid = uuid::Uuid::new_v4();
tracing::debug!("Copying TempFile to tmp directory"); tracing::debug!("Copying TempFile to tmp directory");
let recipe_image_path = format!("/assets/recipies/images/{uid}"); let recipe_image_path = format!("/assets/recipies/images/{uid}");
let Ok(_) = tokio::fs::copy(&path, format!(".{}", recipe_image_path)).await else { let write_path = format!(".{}", recipe_image_path);
tracing::warn!("Copy TempFile to tmp directory failed"); let Ok(_) = tokio::fs::copy(&path, &write_path).await.inspect_err(|e| {
return HttpResponse::InternalServerError().finish(); 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(); let mut model = recipe.into_active_model();
model.image_url = Set(recipe_image_path); model.image_url = Set(recipe_image_path);
let Ok(_) = Recipies::update(model).exec(&**db).await else { let Ok(_) = Recipies::update(model)
return HttpResponse::InternalServerError().finish(); .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"); tracing::debug!("Copying TempFile to tmp directory");
let _ = tokio::fs::remove_file(&path).await; let _ = tokio::fs::remove_file(&path).await;