Skip to main content

paper/cli/
model_downloader.rs

1use 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
8/// Downloads a single model file with progress, resumable support, SHA256 verification,
9/// and atomic rename.
10pub struct ModelDownloader {
11    url: String,
12    dest: PathBuf,
13    expected_sha256: String,
14    total_size: u64,
15    client: Client,
16}
17
18impl ModelDownloader {
19    /// Create a new ModelDownloader.
20    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    /// Download the file with progress bar, resume support, SHA256 verification,
31    /// and atomic rename. Respects PAPER_OFFLINE=1.
32    pub async fn download_with_progress(&self) -> Result<()> {
33        // Check PAPER_OFFLINE
34        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 and file not complete, error
46        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        // A resumed request must be answered with 206 Partial Content.
83        // If the server ignored the Range header and sent the whole file (200),
84        // the existing `.partial` prefix is stale: start it over.
85        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        // `.partial` first: an interrupted download is never mistaken for a model.
96        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        // Verify SHA256 before atomic rename
110        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}