use std::path::Path;
use tokio::io::AsyncWriteExt;
use crate::errors::DownloadResult;
use crate::hosts::HTTP_CLIENT;
use crate::trace_debug;
pub async fn download_file_untracked(url: &str, path: impl AsRef<Path>) -> DownloadResult<()> {
let mut response = HTTP_CLIENT.get(url).send().await?.error_for_status()?;
let mut file = tokio::fs::File::create(path.as_ref()).await?;
while let Some(chunk) = response.chunk().await? {
file.write_all(&chunk).await?;
}
file.flush().await?;
Ok(())
}
pub async fn download_file<F>(url: &str, on_progress: F) -> DownloadResult<Vec<u8>>
where
F: Fn(u64, u64),
{
let trimmed = url.trim();
trace_debug!("Downloading {trimmed}");
let mut response = HTTP_CLIENT.get(trimmed).send().await?.error_for_status()?;
let total = response.content_length().unwrap_or(0);
on_progress(0, total);
let mut buffer = Vec::with_capacity(total as usize);
let mut downloaded: u64 = 0;
while let Some(chunk) = response.chunk().await? {
buffer.extend_from_slice(&chunk);
downloaded += chunk.len() as u64;
on_progress(downloaded, total);
}
trace_debug!("Downloaded {} ({downloaded} bytes)", trimmed);
Ok(buffer)
}