From 5df5bd10eab6216391e434384c38683c66c23c06 Mon Sep 17 00:00:00 2001 From: Christopher Gubbin <44929740+cgubbin@users.noreply.github.com> Date: Sat, 29 Jan 2022 16:40:50 +0000 Subject: [PATCH] Updated forms/multipart to v4. (#503) --- forms/multipart/Cargo.toml | 8 ++++---- forms/multipart/README.md | 2 +- forms/multipart/src/main.rs | 15 ++++++--------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/forms/multipart/Cargo.toml b/forms/multipart/Cargo.toml index 44448e06..a9f7008d 100644 --- a/forms/multipart/Cargo.toml +++ b/forms/multipart/Cargo.toml @@ -2,7 +2,7 @@ name = "multipart-example" version = "0.3.0" authors = ["Bevan Hunt "] -edition = "2018" +edition = "2021" license = "MIT" description = "Simple file uploader in Actix Web with Async/Await" keywords = ["actix", "actix-web", "multipart"] @@ -10,9 +10,9 @@ repository = "https://github.com/actix/examples" readme = "README.md" [dependencies] -actix-multipart = "0.3" -actix-web = "3" +actix-multipart = "0.4.0-beta.12" +actix-web = "4.0.0-beta.21" futures-util = "0.3" -sanitize-filename = "0.2" +sanitize-filename = "0.3" uuid = { version = "0.8", features = ["v4"] } diff --git a/forms/multipart/README.md b/forms/multipart/README.md index b685cd16..ffba927f 100644 --- a/forms/multipart/README.md +++ b/forms/multipart/README.md @@ -7,7 +7,7 @@ cd forms/multipart cargo run ``` -``` open web browser to localhost:3000 and upload file(s) ``` +``` open web browser to localhost:8080 and upload file(s) ``` ### Result diff --git a/forms/multipart/src/main.rs b/forms/multipart/src/main.rs index 62cf4d75..1fd65cfe 100644 --- a/forms/multipart/src/main.rs +++ b/forms/multipart/src/main.rs @@ -9,23 +9,20 @@ async fn save_file(mut payload: Multipart) -> Result { // iterate over multipart stream while let Some(mut field) = payload.try_next().await? { // A multipart/form-data stream has to contain `content_disposition` - let content_disposition = field - .content_disposition() - .ok_or_else(|| HttpResponse::BadRequest().finish())?; + let content_disposition = field.content_disposition(); - let filename = content_disposition.get_filename().map_or_else( - || Uuid::new_v4().to_string(), - |f| sanitize_filename::sanitize(f), - ); + let filename = content_disposition + .get_filename() + .map_or_else(|| Uuid::new_v4().to_string(), sanitize_filename::sanitize); let filepath = format!("./tmp/{}", filename); // File::create is blocking operation, use threadpool - let mut f = web::block(|| std::fs::File::create(filepath)).await?; + let mut f = web::block(|| std::fs::File::create(filepath)).await??; // Field in turn is stream of *Bytes* object while let Some(chunk) = field.try_next().await? { // filesystem operations are blocking, we have to use threadpool - f = web::block(move || f.write_all(&chunk).map(|_| f)).await?; + f = web::block(move || f.write_all(&chunk).map(|_| f)).await??; } }