extract create_download_response API helper
and put it into a new "api2::helpers" module. Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
This commit is contained in:
parent
c65bc99a41
commit
e22f4882e7
|
@ -9,6 +9,7 @@ pub mod status;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod version;
|
pub mod version;
|
||||||
pub mod pull;
|
pub mod pull;
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
use proxmox::api::router::SubdirMap;
|
use proxmox::api::router::SubdirMap;
|
||||||
use proxmox::api::Router;
|
use proxmox::api::Router;
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
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())
|
||||||
|
.map_err(move |err| http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path.clone(), err)))
|
||||||
|
.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())
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ use crate::server::{WorkerTask, H2Service};
|
||||||
use crate::tools;
|
use crate::tools;
|
||||||
use crate::config::acl::PRIV_DATASTORE_READ;
|
use crate::config::acl::PRIV_DATASTORE_READ;
|
||||||
use crate::config::cached_user_info::CachedUserInfo;
|
use crate::config::cached_user_info::CachedUserInfo;
|
||||||
|
use crate::api2::helpers;
|
||||||
|
|
||||||
mod environment;
|
mod environment;
|
||||||
use environment::*;
|
use environment::*;
|
||||||
|
@ -187,26 +188,9 @@ fn download_file(
|
||||||
path.push(env.backup_dir.relative_path());
|
path.push(env.backup_dir.relative_path());
|
||||||
path.push(&file_name);
|
path.push(&file_name);
|
||||||
|
|
||||||
let path2 = path.clone();
|
env.log(format!("download {:?}", path.clone()));
|
||||||
let path3 = path.clone();
|
|
||||||
|
|
||||||
let file = tokio::fs::File::open(path)
|
helpers::create_download_response(path).await
|
||||||
.map_err(move |err| http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path2, err)))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
env.log(format!("download {:?}", path3));
|
|
||||||
|
|
||||||
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())
|
|
||||||
}.boxed()
|
}.boxed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue