Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
#[derive(Default)]
pub struct DownloadManagerState {
pub cancel_tokens: HashMap<String, CancellationToken>,
// Paused tasks keep their partial .tmp/.url instead of being deleted on cancel.
pub paused_tasks: HashSet<String>,
}
#[derive(serde::Deserialize, Clone, Debug)]
pub struct ProxyConfig {
pub url: String,
pub username: Option<String>,
pub password: Option<String>,
pub no_proxy: Option<Vec<String>>, // List of domains to bypass proxy
pub ignore_ssl: Option<bool>, // Ignore SSL certificate verification
}
#[derive(serde::Deserialize, Clone, Debug)]
pub struct DownloadItem {
pub url: String,
pub save_path: String,
pub proxy: Option<ProxyConfig>,
pub sha256: Option<String>,
pub size: Option<u64>,
pub model_id: Option<String>,
}
#[derive(serde::Serialize, Clone, Debug)]
pub struct DownloadEvent {
pub transferred: u64,
pub total: u64,
}
/// Structure to track progress for each file in parallel downloads
#[derive(Clone)]
pub struct ProgressTracker {
file_progress: Arc<Mutex<HashMap<String, u64>>>,
total_size: Arc<AtomicU64>,
}
impl ProgressTracker {
pub fn new(_items: &[DownloadItem], sizes: HashMap<String, u64>) -> Self {
let total_size: u64 = sizes.values().sum();
ProgressTracker {
file_progress: Arc::new(Mutex::new(HashMap::new())),
total_size: Arc::new(AtomicU64::new(total_size)),
}
}
pub async fn update_progress(&self, file_id: &str, transferred: u64) {
let mut progress = self.file_progress.lock().await;
progress.insert(file_id.to_string(), transferred);
}
/// Add `additional` bytes to the running total. Used when the true size is
/// only known after the GET response (HEAD may have reported 0 or been
/// blocked).
pub fn add_to_total(&self, additional: u64) {
self.total_size.fetch_add(additional, Ordering::Relaxed);
}
pub async fn get_total_progress(&self) -> (u64, u64) {
let progress = self.file_progress.lock().await;
let total_transferred: u64 = progress.values().sum();
(total_transferred, self.total_size.load(Ordering::Relaxed))
}
}