use crate::error::Error;
use crate::file::File;
use crate::package::Package;
use reqwest::Client;
use sha2::{Digest, Sha256};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use tokio::fs::{create_dir_all, OpenOptions};
use tokio::io::{AsyncSeekExt, AsyncWriteExt};
pub struct Downloader {
client: Client,
}
impl Default for Downloader {
fn default() -> Self {
Self::new()
}
}
impl Downloader {
pub fn new() -> Self {
Self {
client: Client::builder()
.timeout(std::time::Duration::from_secs(1))
.build()
.unwrap_or_else(|_| Client::new()),
}
}
pub async fn download_package<F>(
&self,
package: &Package,
output_dir: &Path,
progress_cb: F,
) -> Result<(), Error>
where
F: Fn(u64, u64) + Send + Sync + 'static,
{
let all_files = package.all_files();
let total_bytes = package.total_size();
let cumulative_downloaded = Arc::new(AtomicU64::new(0));
let progress_fn = Arc::new(progress_cb);
for file_entry in &all_files {
let target_path = output_dir.join(&file_entry.name);
if let Some(parent) = target_path.parent() {
create_dir_all(parent).await?;
}
let cumulative_ref = Arc::clone(&cumulative_downloaded);
let progress_fn_ref = Arc::clone(&progress_fn);
self.download_file(file_entry, None, &target_path, move |curr_file, _total_file| {
let current_total = cumulative_ref.load(Ordering::SeqCst) + curr_file;
progress_fn_ref(current_total, total_bytes);
})
.await?;
cumulative_downloaded.fetch_add(file_entry.file_size, Ordering::SeqCst);
}
Ok(())
}
pub async fn download_file<F>(
&self,
file_entry: &File,
url_override: Option<&str>,
output_path: &Path,
progress_cb: F,
) -> Result<(), Error>
where
F: Fn(u64, u64) + Send + Sync + 'static,
{
let hashes = file_entry.decode_hashes()?;
let total_chunks = hashes.len();
let chunk_size = file_entry.chunk_size as u64;
let mirrors: Vec<String> = if let Some(override_url) = url_override {
vec![override_url.to_string()]
} else {
file_entry.mirror_urls()
};
if mirrors.is_empty() {
return Err(Error::NoMirrorsAvailable);
}
let active_mirrors: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(mirrors));
let progress_fn = Arc::new(progress_cb);
if let Some(parent) = output_path.parent() {
create_dir_all(parent).await?;
}
let mut file = OpenOptions::new()
.create(true)
.write(true)
.open(output_path)
.await?;
file.set_len(file_entry.file_size).await?;
let downloaded_bytes = Arc::new(AtomicU64::new(0));
for i in 0..total_chunks {
let offset = i as u64 * chunk_size;
let expected_size = if i == total_chunks - 1 {
file_entry.last_chunk_size() as u64
} else {
chunk_size
};
let expected_hash = hashes[i];
let mut success = false;
let mirrors_clone: Arc<Mutex<Vec<String>>> = Arc::clone(&active_mirrors);
let current_mirrors = {
let guard = mirrors_clone.lock().unwrap();
guard.clone()
};
for mirror_url in ¤t_mirrors {
let range_header = format!(
"bytes={}-{}",
offset,
offset + expected_size - 1
);
let response = self
.client
.get(mirror_url)
.header("Range", range_header)
.send()
.await;
if let Ok(res) = response {
if res.status().is_success() {
if let Ok(bytes) = res.bytes().await {
let chunk_bytes = if bytes.len() as u64 == expected_size {
bytes.to_vec()
} else if (bytes.len() as u64) >= offset + expected_size {
bytes[offset as usize..(offset + expected_size) as usize].to_vec()
} else {
continue;
};
let mut hasher = Sha256::new();
hasher.update(&chunk_bytes);
let calculated_hash: [u8; 32] = hasher.finalize().into();
if calculated_hash == expected_hash {
file.seek(std::io::SeekFrom::Start(offset)).await?;
file.write_all(&chunk_bytes).await?;
let current_total = downloaded_bytes
.fetch_add(expected_size, Ordering::SeqCst)
+ expected_size;
progress_fn(current_total, file_entry.file_size);
success = true;
break;
}
}
}
}
}
if !success {
return Err(Error::ChunkDownloadFailed(i));
}
}
file.flush().await?;
Ok(())
}
}