1 use std
::path
::PathBuf
;
4 use hyper
::{Body, Response, StatusCode, header}
;
7 pub async
fn create_download_response(path
: PathBuf
) -> Result
<Response
<Body
>, Error
> {
8 let file
= tokio
::fs
::File
::open(path
.clone())
11 std
::io
::ErrorKind
::NotFound
=> http_err
!(NOT_FOUND
, format
!("open file {:?} failed - not found", path
.clone())),
12 _
=> http_err
!(BAD_REQUEST
, format
!("open file {:?} failed: {}", path
.clone(), err
)),
17 let payload
= tokio_util
::codec
::FramedRead
::new(file
, tokio_util
::codec
::BytesCodec
::new())
18 .map_ok(|bytes
| hyper
::body
::Bytes
::from(bytes
.freeze()));
20 let body
= Body
::wrap_stream(payload
);
22 // fixme: set other headers ?
23 Ok(Response
::builder()
24 .status(StatusCode
::OK
)
25 .header(header
::CONTENT_TYPE
, "application/octet-stream")