2021-02-16 17:06:52 +00:00
|
|
|
use std::io::{Read, Seek};
|
2020-06-24 13:27:40 +00:00
|
|
|
use std::path::PathBuf;
|
2020-07-29 07:38:11 +00:00
|
|
|
|
2020-06-24 13:27:40 +00:00
|
|
|
use anyhow::Error;
|
2020-07-29 07:38:11 +00:00
|
|
|
use futures::stream::TryStreamExt;
|
2020-06-24 13:27:40 +00:00
|
|
|
use hyper::{Body, Response, StatusCode, header};
|
2020-07-29 07:38:11 +00:00
|
|
|
|
|
|
|
use proxmox::http_bail;
|
2020-06-24 13:27:40 +00:00
|
|
|
|
2021-02-16 17:06:52 +00:00
|
|
|
use crate::api2::types::ArchiveEntry;
|
|
|
|
use crate::backup::{CatalogReader, DirEntryAttribute};
|
|
|
|
|
2020-06-24 13:27:40 +00:00
|
|
|
pub async fn create_download_response(path: PathBuf) -> Result<Response<Body>, Error> {
|
2020-07-29 07:38:11 +00:00
|
|
|
let file = match tokio::fs::File::open(path.clone()).await {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(ref err) if err.kind() == std::io::ErrorKind::NotFound => {
|
|
|
|
http_bail!(NOT_FOUND, "open file {:?} failed - not found", path);
|
|
|
|
}
|
|
|
|
Err(err) => http_bail!(BAD_REQUEST, "open file {:?} failed: {}", path, err),
|
|
|
|
};
|
2020-06-24 13:27:40 +00:00
|
|
|
|
|
|
|
let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new())
|
2021-01-15 13:38:27 +00:00
|
|
|
.map_ok(|bytes| bytes.freeze());
|
2020-06-24 13:27:40 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
2021-02-16 17:06:52 +00:00
|
|
|
|
|
|
|
/// Returns the list of content of the given path
|
|
|
|
pub fn list_dir_content<R: Read + Seek>(
|
|
|
|
reader: &mut CatalogReader<R>,
|
|
|
|
path: &[u8],
|
|
|
|
) -> Result<Vec<ArchiveEntry>, Error> {
|
|
|
|
let dir = reader.lookup_recursive(path)?;
|
|
|
|
let mut res = vec![];
|
|
|
|
let mut path = path.to_vec();
|
|
|
|
if !path.is_empty() && path[0] == b'/' {
|
|
|
|
path.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for direntry in reader.read_dir(&dir)? {
|
|
|
|
let mut components = path.clone();
|
|
|
|
components.push(b'/');
|
|
|
|
components.extend(&direntry.name);
|
2021-04-21 13:18:08 +00:00
|
|
|
let mut entry = ArchiveEntry::new(&components, Some(&direntry.attr));
|
2021-02-16 17:06:52 +00:00
|
|
|
if let DirEntryAttribute::File { size, mtime } = direntry.attr {
|
|
|
|
entry.size = size.into();
|
|
|
|
entry.mtime = mtime.into();
|
|
|
|
}
|
|
|
|
res.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|