use std::io::Write;
use std::path::PathBuf;
use libpijul::pristine::Base32;
pub struct Http {
pub url: String,
pub channel: String,
pub client: reqwest::Client,
pub name: String,
}
impl Http {
pub async fn download_changes(&mut self, hashes: &[libpijul::pristine::Hash], send: &mut tokio::sync::mpsc::Sender<libpijul::pristine::Hash>, path: &mut PathBuf, _full: bool) -> Result<(), anyhow::Error> {
for c in hashes {
libpijul::changestore::filesystem::push_filename(
path,
c,
);
std::fs::create_dir_all(&path.parent().unwrap())?;
let mut f = std::fs::File::create(&path)?;
libpijul::changestore::filesystem::pop_filename(path);
let c32 = c.to_base32();
let url = format!("{}/{}", self.url, super::DOT_DIR);
let mut res = self.client.get(&url).query(&[("change", c32)]).send().await?;
if !res.status().is_success() {
return Err((crate::Error::Http {
status: res.status(),
})
.into());
}
while let Some(chunk) = res.chunk().await? {
f.write_all(&chunk)?;
}
if send.send(*c).await.is_err() {
break
}
}
Ok(())
}
}