1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

remove unwraps in multipart examples

inspired by #406
This commit is contained in:
Rob Ede
2021-10-07 03:27:50 +01:00
parent 465b6a9c0a
commit 0c4ab86a9a
8 changed files with 61 additions and 43 deletions

View File

@ -12,6 +12,7 @@ readme = "README.md"
[dependencies]
actix-web = "3"
actix-multipart = "0.3"
futures = "0.3.5"
async-std = "1.8.0"
futures-util = "0.3"
async-std = "1.8"
sanitize-filename = "0.2"
uuid = { version = "0.8", features = ["v4"] }

View File

@ -1,24 +1,30 @@
use actix_multipart::Multipart;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use async_std::prelude::*;
use futures::{StreamExt, TryStreamExt};
use futures_util::TryStreamExt as _;
use uuid::Uuid;
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
// iterate over multipart stream
while let Ok(Some(mut field)) = payload.try_next().await {
let content_type = field
.content_disposition().ok_or(actix_web::error::ParseError::Incomplete)?;
let filename = content_type
.get_filename().ok_or(actix_web::error::ParseError::Incomplete)?;
let content_disposition = field
.content_disposition()
.ok_or_else(|| HttpResponse::BadRequest().finish())?;
let filename = content_disposition.get_filename().map_or_else(
|| Uuid::new_v4().to_string(),
|f| sanitize_filename::sanitize(f),
);
let filepath = format!("./tmp/{}", sanitize_filename::sanitize(&filename));
let mut f = async_std::fs::File::create(filepath).await?;
// Field in turn is stream of *Bytes* object
while let Some(chunk) = field.next().await {
let data = chunk.unwrap();
f.write_all(&data).await?;
while let Some(chunk) = field.try_next().await? {
f.write_all(&chunk).await?;
}
}
Ok(HttpResponse::Ok().into())
}
@ -40,11 +46,9 @@ fn index() -> HttpResponse {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
std::env::set_var("RUST_LOG", "info");
async_std::fs::create_dir_all("./tmp").await?;
let ip = "0.0.0.0:3000";
HttpServer::new(|| {
App::new().wrap(middleware::Logger::default()).service(
web::resource("/")
@ -52,7 +56,7 @@ async fn main() -> std::io::Result<()> {
.route(web::post().to(save_file)),
)
})
.bind(ip)?
.bind(("127.0.0.1", 8080))?
.run()
.await
}