1
0
mirror of https://github.com/actix/examples synced 2024-12-02 18:02:22 +01:00
examples/forms/multipart/src/main.rs

96 lines
2.8 KiB
Rust
Raw Normal View History

2019-12-09 01:28:09 +01:00
use std::io::Write;
use actix_multipart::{
form::{
tempfile::{TempFile, TempFileConfig},
MultipartForm,
},
Multipart,
};
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer, Responder};
use futures_util::TryStreamExt as _;
use uuid::Uuid;
#[derive(Debug, MultipartForm)]
struct UploadForm {
#[multipart(rename = "file")]
files: Vec<TempFile>,
}
2020-09-12 17:49:45 +02:00
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())
2018-06-08 06:34:14 +02:00
}
2022-02-02 01:44:58 +01:00
async fn index() -> HttpResponse {
2018-06-08 06:34:14 +02:00
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>
2018-06-08 06:34:14 +02:00
</form>
</body>
</html>"#;
HttpResponse::Ok().body(html)
}
2020-09-12 17:49:45 +02:00
#[actix_web::main]
2019-12-07 18:59:24 +01:00
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")?;
2019-12-07 18:59:24 +01:00
log::info!("starting HTTP server at http://localhost:8080");
2019-03-29 21:43:03 +01:00
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)),
)
2019-03-10 03:03:09 +01:00
})
.bind(("127.0.0.1", 8080))?
.workers(2)
2019-12-25 17:48:33 +01:00
.run()
2019-12-07 18:59:24 +01:00
.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`
let content_disposition = field.content_disposition();
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??;
// 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??;
}
}
Ok(HttpResponse::Ok().into())
}