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

118 lines
3.3 KiB
Rust
Raw Normal View History

#![allow(unused_variables)]
extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;
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;
2018-05-21 06:03:29 +02:00
use actix_web::{
2018-07-16 08:36:53 +02:00
dev, error, http, middleware, multipart, server, App, Error, FutureResponse,
HttpMessage, HttpRequest, HttpResponse,
2018-05-21 06:03:29 +02:00
};
2018-06-08 06:34:14 +02:00
use futures::future;
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>,
}
2018-07-16 08:36:53 +02:00
pub fn save_file(
field: multipart::Field<dev::Payload>,
) -> Box<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,
Err(e) => return Box::new(future::err(error::ErrorInternalServerError(e))),
};
Box::new(
field
.fold(0i64, move |acc, bytes| {
let rt = file
.write_all(bytes.as_ref())
.map(|_| acc + bytes.len() as i64)
.map_err(|e| {
println!("file.write_all failed: {:?}", e);
error::MultipartError::Payload(error::PayloadError::Io(e))
});
future::result(rt)
})
.map_err(|e| {
println!("save_file failed, {:?}", e);
error::ErrorInternalServerError(e)
}),
)
}
2018-07-16 08:36:53 +02:00
pub fn handle_multipart_item(
item: multipart::MultipartItem<dev::Payload>,
) -> Box<Stream<Item = i64, Error = Error>> {
2018-06-08 06:34:14 +02:00
match item {
multipart::MultipartItem::Field(field) => {
Box::new(save_file(field).into_stream())
}
multipart::MultipartItem::Nested(mp) => Box::new(
mp.map_err(error::ErrorInternalServerError)
.map(handle_multipart_item)
.flatten(),
),
}
}
2018-06-20 17:03:56 +02:00
pub fn upload(req: HttpRequest<AppState>) -> FutureResponse<HttpResponse> {
req.state().counter.set(req.state().counter.get() + 1);
2018-07-16 08:36:53 +02:00
println!("{:?}", req.state().counter.get());
2018-06-08 06:34:14 +02:00
Box::new(
2018-07-16 08:36:53 +02:00
req.multipart()
2018-06-08 06:34:14 +02:00
.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-20 17:03:56 +02:00
fn index(_req: HttpRequest<AppState>) -> Result<HttpResponse, error::Error> {
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>"#;
Ok(HttpResponse::Ok().body(html))
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
2018-06-08 06:34:14 +02:00
env_logger::init();
let sys = actix::System::new("multipart-example");
2018-05-08 20:08:43 +02:00
server::new(|| {
2018-07-16 08:36:53 +02:00
App::with_state(AppState {
counter: Cell::new(0),
2019-03-10 03:03:09 +01:00
})
.middleware(middleware::Logger::default())
.resource("/", |r| {
r.method(http::Method::GET).with(index);
r.method(http::Method::POST).with(upload);
})
})
.bind("127.0.0.1:8080")
.unwrap()
.start();
println!("Starting http server: 127.0.0.1:8080");
let _ = sys.run();
}