Upload image

This commit is contained in:
eraden 2024-11-01 17:49:18 +01:00
parent 9d5dc91a22
commit 263e0aa8e7
3 changed files with 29 additions and 3 deletions

17
Cargo.lock generated
View File

@ -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"

View File

@ -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]

View File

@ -59,7 +59,7 @@ struct SignInForm {
#[derive(Debug, MultipartForm)]
struct ImageUpload {
image: Option<TempFile>,
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<ImageUpload>) -> 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,