]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/helpers.rs
update to first proxmox crate split
[proxmox-backup.git] / src / api2 / helpers.rs
CommitLineData
e22f4882 1use std::path::PathBuf;
8aa67ee7 2
e22f4882 3use anyhow::Error;
8aa67ee7 4use futures::stream::TryStreamExt;
e22f4882 5use hyper::{Body, Response, StatusCode, header};
8aa67ee7 6
6ef1b649 7use proxmox_router::http_bail;
e22f4882
SR
8
9pub async fn create_download_response(path: PathBuf) -> Result<Response<Body>, Error> {
8aa67ee7
WB
10 let file = match tokio::fs::File::open(path.clone()).await {
11 Ok(file) => file,
12 Err(ref err) if err.kind() == std::io::ErrorKind::NotFound => {
13 http_bail!(NOT_FOUND, "open file {:?} failed - not found", path);
14 }
15 Err(err) => http_bail!(BAD_REQUEST, "open file {:?} failed: {}", path, err),
16 };
e22f4882
SR
17
18 let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
44288184 19 .map_ok(|bytes| bytes.freeze());
e22f4882
SR
20
21 let body = Body::wrap_stream(payload);
22
23 // fixme: set other headers ?
24 Ok(Response::builder()
25 .status(StatusCode::OK)
26 .header(header::CONTENT_TYPE, "application/octet-stream")
27 .body(body)
28 .unwrap())
29}