2020-06-24 13:27:40 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::Error;
|
|
|
|
use futures::*;
|
|
|
|
use hyper::{Body, Response, StatusCode, header};
|
|
|
|
use proxmox::http_err;
|
|
|
|
|
|
|
|
pub async fn create_download_response(path: PathBuf) -> Result<Response<Body>, Error> {
|
|
|
|
let file = tokio::fs::File::open(path.clone())
|
2020-07-21 13:03:34 +00:00
|
|
|
.map_err(move |err| {
|
|
|
|
match err.kind() {
|
|
|
|
std::io::ErrorKind::NotFound => http_err!(NOT_FOUND, format!("open file {:?} failed - not found", path.clone())),
|
|
|
|
_ => http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path.clone(), err)),
|
|
|
|
}
|
|
|
|
})
|
2020-06-24 13:27:40 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
|
|
|
|
.map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze()));
|
|
|
|
|
|
|
|
let body = Body::wrap_stream(payload);
|
|
|
|
|
|
|
|
// fixme: set other headers ?
|
|
|
|
Ok(Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header(header::CONTENT_TYPE, "application/octet-stream")
|
|
|
|
.body(body)
|
|
|
|
.unwrap())
|
|
|
|
}
|