forked from cetra3/mpart-async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarp.rs
44 lines (36 loc) · 1.2 KB
/
warp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use warp::Filter;
use bytes::Buf;
use futures_core::Stream;
use futures_util::TryStreamExt;
use mime::Mime;
use mpart_async::server::MultipartStream;
use std::convert::Infallible;
#[tokio::main]
async fn main() {
// Match any request and return hello world!
let routes = warp::any()
.and(warp::header::<Mime>("content-type"))
.and(warp::body::stream())
.and_then(mpart);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
async fn mpart(
mime: Mime,
body: impl Stream<Item = Result<impl Buf, warp::Error>> + Unpin,
) -> Result<impl warp::Reply, Infallible> {
let boundary = mime.get_param("boundary").map(|v| v.to_string()).unwrap();
let mut stream = MultipartStream::new(
boundary,
body.map_ok(|mut buf| buf.copy_to_bytes(buf.remaining())),
);
while let Ok(Some(mut field)) = stream.try_next().await {
println!("Field received:{}", field.name().unwrap());
if let Ok(filename) = field.filename() {
println!("Field filename:{}", filename);
}
while let Ok(Some(bytes)) = field.try_next().await {
println!("Bytes received:{}", bytes.len());
}
}
Ok(format!("Thanks!\n"))
}