use axum::{
body::{Body, Bytes},
extract::Request,
};
use futures_util::{Stream, TryStreamExt};
use std::io;
async fn stream_handler(request: Request) -> Result<(), (StatusCode, String)> {
// Convert body to a stream of Bytes
let stream = request.into_body().into_data_stream();
// Process the stream
stream_to_file("output.txt", stream).await
}
async fn stream_to_file<S, E>(path: &str, stream: S) -> Result<(), (StatusCode, String)>
where
S: Stream<Item = Result<Bytes, E>>,
E: Into<BoxError>,
{
// Convert stream to AsyncRead and write to file
let body_with_io_error = stream.map_err(io::Error::other);
let body_reader = StreamReader::new(body_with_io_error);
let mut file = File::create(path).await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
tokio::io::copy(&mut body_reader, &mut file).await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
Ok(())
}