paper/cli/
model_downloader.rs1use anyhow::{bail, Context, Result};
2use indicatif::{ProgressBar, ProgressStyle};
3use reqwest::Client;
4use sha2::{Digest, Sha256};
5use std::path::{Path, PathBuf};
6use tokio::io::AsyncWriteExt;
7
8pub struct ModelDownloader {
11 url: String,
12 dest: PathBuf,
13 expected_sha256: String,
14 total_size: u64,
15 client: Client,
16}
17
18impl ModelDownloader {
19 pub fn new(url: String, dest: PathBuf, expected_sha256: String, total_size: u64) -> Self {
21 Self {
22 url,
23 dest,
24 expected_sha256,
25 total_size,
26 client: Client::new(),
27 }
28 }
29
30 pub async fn download_with_progress(&self) -> Result<()> {
33 let offline = std::env::var("PAPER_OFFLINE")
35 .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
36 .unwrap_or(false);
37
38 let tmp = self.dest.with_extension("partial");
39 let mut downloaded = if tmp.exists() {
40 tmp.metadata().map(|m| m.len()).unwrap_or(0)
41 } else {
42 0
43 };
44
45 if offline {
47 if downloaded >= self.total_size && self.verify_sha256(&tmp).await? {
48 tokio::fs::rename(&tmp, &self.dest).await?;
49 return Ok(());
50 }
51 bail!(
52 "{} is missing or incomplete and PAPER_OFFLINE is set",
53 self.dest.display()
54 );
55 }
56
57 let pb = ProgressBar::new(self.total_size);
58 pb.set_style(
59 ProgressStyle::with_template(
60 "{msg} [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})",
61 )
62 .unwrap_or_else(|_| ProgressStyle::default_bar())
63 .progress_chars("##-"),
64 );
65 pb.set_message(format!(
66 "Downloading {}",
67 self.dest.file_name().unwrap().to_string_lossy()
68 ));
69 pb.set_position(downloaded);
70
71 let mut builder = self.client.get(&self.url);
72 if downloaded > 0 {
73 builder = builder.header(reqwest::header::RANGE, format!("bytes={downloaded}-"));
74 }
75 let mut resp = builder
76 .send()
77 .await
78 .with_context(|| format!("downloading {}", self.url))?
79 .error_for_status()
80 .with_context(|| format!("download failed for {}", self.url))?;
81
82 if downloaded > 0 && resp.status() != reqwest::StatusCode::PARTIAL_CONTENT {
86 tracing::debug!(
87 "server ignored Range for {}; restarting download",
88 self.dest.display()
89 );
90 tokio::fs::remove_file(&tmp).await.ok();
91 downloaded = 0;
92 pb.set_position(0);
93 }
94
95 let mut file = tokio::fs::OpenOptions::new()
97 .create(true)
98 .append(true)
99 .open(&tmp)
100 .await?;
101 while let Some(chunk) = resp.chunk().await? {
102 file.write_all(&chunk).await?;
103 downloaded += chunk.len() as u64;
104 pb.set_position(downloaded);
105 }
106 file.flush().await?;
107 drop(file);
108
109 if !self.verify_sha256(&tmp).await? {
111 let _ = tokio::fs::remove_file(&tmp).await;
112 bail!(
113 "checksum mismatch for {}; deleted the corrupt download",
114 self.dest.display()
115 );
116 }
117
118 tokio::fs::rename(&tmp, &self.dest).await?;
119 pb.finish_and_clear();
120 Ok(())
121 }
122
123 async fn verify_sha256(&self, path: &Path) -> Result<bool> {
124 let mut file = tokio::fs::File::open(path).await?;
125 let mut hasher = Sha256::new();
126 let mut buffer = vec![0u8; 8192];
127 loop {
128 let n = tokio::io::AsyncReadExt::read(&mut file, &mut buffer).await?;
129 if n == 0 {
130 break;
131 }
132 hasher.update(&buffer[..n]);
133 }
134 let actual = hex::encode(hasher.finalize());
135 Ok(actual.eq_ignore_ascii_case(&self.expected_sha256))
136 }
137}