Update recipe image
This commit is contained in:
parent
193ef00b72
commit
57b1113cc3
@ -380,7 +380,7 @@ async fn recipe_image_update(
|
||||
MultipartForm(form): MultipartForm<ImageUpload>,
|
||||
path: Path<i32>,
|
||||
db: Data<DatabaseConnection>,
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user