1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

update multipart examples to use derive macro

This commit is contained in:
Rob Ede
2023-02-26 21:56:55 +00:00
parent 9b77b033b0
commit 080db60175
10 changed files with 309 additions and 241 deletions

View File

@ -12,6 +12,8 @@ readme = "README.md"
actix-multipart.workspace = true
actix-web.workspace = true
env_logger.workspace = true
futures-util.workspace = true
log.workspace = true
sanitize-filename = "0.4"
uuid = { version = "1", features = ["v4"] }

View File

@ -1,11 +1,77 @@
use std::io::Write;
use actix_multipart::Multipart;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use actix_multipart::{
form::{
tempfile::{TempFile, TempFileConfig},
text::Text,
MultipartForm,
},
Multipart,
};
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer, Responder};
use futures_util::TryStreamExt as _;
use uuid::Uuid;
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
#[derive(Debug, MultipartForm)]
struct UploadForm {
#[multipart(rename = "file")]
files: Vec<TempFile>,
}
async fn save_files(
MultipartForm(form): MultipartForm<UploadForm>,
) -> Result<impl Responder, Error> {
for f in form.files {
let path = format!("./tmp/{}", f.file_name.unwrap());
log::info!("saving to {path}");
f.file.persist(path).unwrap();
}
Ok(HttpResponse::Ok())
}
async fn index() -> HttpResponse {
let html = r#"<html>
<head><title>Upload Test</title></head>
<body>
<form target="/" method="post" enctype="multipart/form-data">
<input type="file" multiple name="file"/>
<button type="submit">Submit</button>
</form>
</body>
</html>"#;
HttpResponse::Ok().body(html)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("creating temporary upload directory");
std::fs::create_dir_all("./tmp")?;
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.app_data(TempFileConfig::default().directory("./tmp"))
.service(
web::resource("/")
.route(web::get().to(index))
.route(web::post().to(save_files)),
)
})
.bind(("127.0.0.1", 8080))?
.workers(2)
.run()
.await
}
/// Example of the old manual way of processing multipart forms.
#[allow(unused)]
async fn save_file_manual(mut payload: Multipart) -> Result<HttpResponse, Error> {
// iterate over multipart stream
while let Some(mut field) = payload.try_next().await? {
// A multipart/form-data stream has to contain `content_disposition`
@ -28,34 +94,3 @@ async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Ok().into())
}
async fn index() -> HttpResponse {
let html = r#"<html>
<head><title>Upload Test</title></head>
<body>
<form target="/" method="post" enctype="multipart/form-data">
<input type="file" multiple name="file"/>
<button type="submit">Submit</button>
</form>
</body>
</html>"#;
HttpResponse::Ok().body(html)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "info");
std::fs::create_dir_all("./tmp")?;
HttpServer::new(|| {
App::new().wrap(middleware::Logger::default()).service(
web::resource("/")
.route(web::get().to(index))
.route(web::post().to(save_file)),
)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}