use crate::error::Result;
use std::path::Path;
pub struct Downloader {
client: reqwest::Client,
}
#[derive(Debug, Clone)]
pub struct DownloadProgress {
pub downloaded: u64,
pub total: u64,
pub speed: f64,
pub element: String,
}
impl Downloader {
pub fn new() -> Self {
Self {
client: reqwest::Client::new(),
}
}
pub async fn download_file(&self, url: &str, path: &Path) -> Result<()> {
let response = self.client.get(url).send().await?;
let bytes = response.bytes().await?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(path, bytes)?;
Ok(())
}
pub async fn download_files(&self, files: Vec<(String, String)>) -> Result<()> {
for (url, path) in files {
let path = Path::new(&path);
self.download_file(&url, path).await?;
}
Ok(())
}
}