use std::{path::PathBuf, pin::pin, str::FromStr};
use bytes::Bytes;
use flowly::{Frame, Service, ServiceExt, flow, io::file::FileReader};
use futures::TryStreamExt;
use tokio::io::AsyncReadExt;
pub struct FileReader1;
impl<E: std::error::Error + Send + Sync + 'static> Service<Result<PathBuf, E>> for FileReader1 {
type Out = std::io::Result<Bytes>;
fn handle(
self,
input: impl futures::Stream<Item = Result<PathBuf, E>> + Send,
) -> impl futures::Stream<Item = Self::Out> + Send {
async_stream::try_stream! {
let mut input = pin!(input);
let mut buf = vec![0u8; 188 * 1024];
while let Some(path) = input.try_next().await.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, Box::new(err)))? {
let mut file = tokio::fs::File::open(path).await?;
loop {
yield match file.read(&mut buf[..]).await? {
0 => break,
n => buf[0..n].to_vec().into()
};
}
}
}
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
Ok(())
}