2018-04-13 03:18:42 +02:00
|
|
|
#![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-04-13 03:18:42 +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-04-13 03:18:42 +02:00
|
|
|
|
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))
|
2018-04-13 03:18:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-06-08 06:34:14 +02:00
|
|
|
env_logger::init();
|
2018-04-13 03:18:42 +02:00
|
|
|
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),
|
|
|
|
}).middleware(middleware::Logger::default())
|
2018-06-08 06:34:14 +02:00
|
|
|
.resource("/", |r| {
|
|
|
|
r.method(http::Method::GET).with(index);
|
|
|
|
r.method(http::Method::POST).with(upload);
|
|
|
|
})
|
2018-05-08 20:08:43 +02:00
|
|
|
}).bind("127.0.0.1:8080")
|
|
|
|
.unwrap()
|
2018-04-13 03:18:42 +02:00
|
|
|
.start();
|
|
|
|
|
|
|
|
println!("Starting http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|