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
45
46
47
use futures_util::stream::Stream;
use hyper::body::{Body, Bytes};
use std::io::Error as IoError;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::fs::File;
use tokio::prelude::AsyncRead;
const BUF_SIZE: usize = 8 * 1024;
pub struct FileBytesStream {
file: File,
buf: Box<[u8; BUF_SIZE]>,
}
impl FileBytesStream {
pub fn new(file: File) -> FileBytesStream {
let buf = Box::new([0; BUF_SIZE]);
FileBytesStream { file, buf }
}
}
impl Stream for FileBytesStream {
type Item = Result<Bytes, IoError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let Self {
ref mut file,
ref mut buf,
} = *self;
match Pin::new(file).poll_read(cx, &mut buf[..]) {
Poll::Ready(Ok(0)) => Poll::Ready(None),
Poll::Ready(Ok(size)) => Poll::Ready(Some(Ok(self.buf[..size].to_owned().into()))),
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Pending => Poll::Pending,
}
}
}
impl FileBytesStream {
pub fn into_body(self) -> Body {
Body::wrap_stream(self)
}
}