From 263e0aa8e7ac1d7d7e6f19ce34a1c9ae2a7c5316 Mon Sep 17 00:00:00 2001 From: eraden Date: Fri, 1 Nov 2024 17:49:18 +0100 Subject: [PATCH] Upload image --- Cargo.lock | 17 +++++++++++++++-- cooked/Cargo.toml | 1 + cooked/src/routes.rs | 14 +++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f5c795..ce013b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1185,6 +1185,7 @@ dependencies = [ "tantivy", "tempfile", "thiserror", + "tokio", "tracing", "tracing-subscriber", "tracing-test", @@ -4908,9 +4909,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.40.0" +version = "1.41.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +checksum = "145f3413504347a2be84393cc8a7d2fb4d863b375909ea59f2158261aa258bbb" dependencies = [ "backtrace", "bytes", @@ -4920,9 +4921,21 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "socket2 0.5.7", + "tokio-macros", "windows-sys 0.52.0", ] +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.82", +] + [[package]] name = "tokio-retry" version = "0.3.0" diff --git a/cooked/Cargo.toml b/cooked/Cargo.toml index 35492e4..7339341 100644 --- a/cooked/Cargo.toml +++ b/cooked/Cargo.toml @@ -31,6 +31,7 @@ tempfile = "3.13.0" pulldown-cmark = "0.12.2" thiserror = "1.0.65" actix-multipart = "0.7.2" +tokio = { version = "1.41.0", features = ["full"] } [build-dependencies] diff --git a/cooked/src/routes.rs b/cooked/src/routes.rs index 79c3767..0df08e3 100644 --- a/cooked/src/routes.rs +++ b/cooked/src/routes.rs @@ -59,7 +59,7 @@ struct SignInForm { #[derive(Debug, MultipartForm)] struct ImageUpload { - image: Option, + image: TempFile, } #[derive(Debug, Template, Deserialize, Clone, PartialEq)] @@ -92,6 +92,18 @@ async fn render_sign_in() -> SignInForm { } } +#[post("/recipe-image")] +async fn recipe_image_upload(MultipartForm(form): MultipartForm) -> HttpResponse { + let _ = tokio::fs::create_dir_all("./assets/recipies/images").await; + let Ok((_file, path)) = form.image.file.keep() else { + return HttpResponse::InternalServerError().finish(); + }; + let uid = uuid::Uuid::new_v4(); + tokio::fs::copy(path, format!("./assets/recipies/images/{}", uid)).await; + + HttpResponse::Ok().body(format!("{{\"url\":\"/assets/recipies/images/{uid}\"}}")) +} + #[post("/sign-in")] async fn sign_in( req: HttpRequest,