use anyhow::{bail, Context, Result};
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::Client;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use tokio::io::AsyncWriteExt;
pub struct ModelDownloader {
url: String,
dest: PathBuf,
expected_sha256: String,
total_size: u64,
client: Client,
}
impl ModelDownloader {
pub fn new(url: String, dest: PathBuf, expected_sha256: String, total_size: u64) -> Self {
Self {
url,
dest,
expected_sha256,
total_size,
client: Client::new(),
}
}
pub async fn download_with_progress(&self) -> Result<()> {
let offline = std::env::var("PAPER_OFFLINE")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
let tmp = self.dest.with_extension("partial");
let mut downloaded = if tmp.exists() {
tmp.metadata().map(|m| m.len()).unwrap_or(0)
} else {
0
};
if offline {
if downloaded >= self.total_size && self.verify_sha256(&tmp).await? {
tokio::fs::rename(&tmp, &self.dest).await?;
return Ok(());
}
bail!(
"{} is missing or incomplete and PAPER_OFFLINE is set",
self.dest.display()
);
}
let pb = ProgressBar::new(self.total_size);
pb.set_style(
ProgressStyle::with_template(
"{msg} [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})",
)
.unwrap_or_else(|_| ProgressStyle::default_bar())
.progress_chars("##-"),
);
pb.set_message(format!(
"Downloading {}",
self.dest.file_name().unwrap().to_string_lossy()
));
pb.set_position(downloaded);
let mut builder = self.client.get(&self.url);
if downloaded > 0 {
builder = builder.header(reqwest::header::RANGE, format!("bytes={downloaded}-"));
}
let mut resp = builder
.send()
.await
.with_context(|| format!("downloading {}", self.url))?
.error_for_status()
.with_context(|| format!("download failed for {}", self.url))?;
if downloaded > 0 && resp.status() != reqwest::StatusCode::PARTIAL_CONTENT {
tracing::debug!(
"server ignored Range for {}; restarting download",
self.dest.display()
);
tokio::fs::remove_file(&tmp).await.ok();
downloaded = 0;
pb.set_position(0);
}
let mut file = tokio::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&tmp)
.await?;
while let Some(chunk) = resp.chunk().await? {
file.write_all(&chunk).await?;
downloaded += chunk.len() as u64;
pb.set_position(downloaded);
}
file.flush().await?;
drop(file);
if !self.verify_sha256(&tmp).await? {
let _ = tokio::fs::remove_file(&tmp).await;
bail!(
"checksum mismatch for {}; deleted the corrupt download",
self.dest.display()
);
}
tokio::fs::rename(&tmp, &self.dest).await?;
pb.finish_and_clear();
Ok(())
}
async fn verify_sha256(&self, path: &Path) -> Result<bool> {
let mut file = tokio::fs::File::open(path).await?;
let mut hasher = Sha256::new();
let mut buffer = vec![0u8; 8192];
loop {
let n = tokio::io::AsyncReadExt::read(&mut file, &mut buffer).await?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
}
let actual = hex::encode(hasher.finalize());
Ok(actual.eq_ignore_ascii_case(&self.expected_sha256))
}
}