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

100 lines
2.9 KiB
Rust
Raw Normal View History

2018-07-16 08:36:53 +02:00
use std::cell::Cell;
2018-06-08 06:34:14 +02:00
use std::fs;
use std::io::Write;
2019-03-29 21:43:03 +01:00
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer};
use futures::future::{err, Either};
2018-05-08 20:08:43 +02:00
use futures::{Future, Stream};
2018-06-20 17:03:56 +02:00
pub struct AppState {
pub counter: Cell<usize>,
}
2019-03-29 21:43:03 +01:00
pub fn save_file(field: web::MultipartField) -> impl Future<Item = i64, Error = Error> {
2018-06-08 06:34:14 +02:00
let file_path_string = "upload.png";
let mut file = match fs::File::create(file_path_string) {
Ok(file) => file,
2019-03-29 21:43:03 +01:00
Err(e) => return Either::A(err(error::ErrorInternalServerError(e))),
2018-06-08 06:34:14 +02:00
};
2019-03-29 21:43:03 +01:00
Either::B(
2018-06-08 06:34:14 +02:00
field
.fold(0i64, move |acc, bytes| {
2019-04-04 20:37:14 +02:00
println!("CHUNK: {:?}", bytes.len());
2019-03-29 21:43:03 +01:00
file.write_all(bytes.as_ref())
2018-06-08 06:34:14 +02:00
.map(|_| acc + bytes.len() as i64)
.map_err(|e| {
println!("file.write_all failed: {:?}", e);
error::MultipartError::Payload(error::PayloadError::Io(e))
2019-03-29 21:43:03 +01:00
})
2018-06-08 06:34:14 +02:00
})
.map_err(|e| {
println!("save_file failed, {:?}", e);
error::ErrorInternalServerError(e)
}),
)
}
2018-07-16 08:36:53 +02:00
pub fn handle_multipart_item(
2019-03-29 21:43:03 +01:00
item: web::MultipartItem,
2018-07-16 08:36:53 +02:00
) -> Box<Stream<Item = i64, Error = Error>> {
2018-06-08 06:34:14 +02:00
match item {
2019-03-29 21:43:03 +01:00
web::MultipartItem::Field(field) => Box::new(save_file(field).into_stream()),
web::MultipartItem::Nested(mp) => Box::new(
2018-06-08 06:34:14 +02:00
mp.map_err(error::ErrorInternalServerError)
.map(handle_multipart_item)
.flatten(),
),
}
}
2019-03-29 21:43:03 +01:00
pub fn upload(
multipart: web::Multipart,
counter: web::Data<Cell<usize>>,
) -> impl Future<Item = HttpResponse, Error = Error> {
counter.set(counter.get() + 1);
println!("{:?}", counter.get());
multipart
.map_err(error::ErrorInternalServerError)
.map(handle_multipart_item)
.flatten()
.collect()
.map(|sizes| HttpResponse::Ok().json(sizes))
.map_err(|e| {
println!("failed: {}", e);
e
})
2018-06-08 06:34:14 +02:00
}
2019-03-29 21:43:03 +01:00
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" name="file"/>
<input type="submit" value="Submit"></button>
</form>
</body>
</html>"#;
2019-03-29 21:43:03 +01:00
HttpResponse::Ok().body(html)
}
2019-03-29 21:43:03 +01:00
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
2018-06-08 06:34:14 +02:00
env_logger::init();
2019-03-29 21:43:03 +01:00
HttpServer::new(|| {
App::new()
.data(Cell::new(0usize))
.wrap(middleware::Logger::default())
.service(
web::resource("/")
.route(web::get().to(index))
.route(web::post().to_async(upload)),
)
2019-03-10 03:03:09 +01:00
})
2019-03-29 21:43:03 +01:00
.bind("127.0.0.1:8080")?
.run()
}