initial prototype

This commit is contained in:
2025-07-19 15:35:04 +02:00
commit 673a0967b2
8 changed files with 1948 additions and 0 deletions

37
src/downloader.rs Normal file
View File

@@ -0,0 +1,37 @@
use crate::state::AppState;
use std::sync::Arc;
use tokio_stream::StreamExt;
pub fn spawn_download_task(url: &str, state: Arc<AppState>) {
let url = url.to_string();
tokio::spawn(async move {
if let Err(e) = download_stream(&url, state).await {
eprintln!("Download failed: {}", e);
}
});
}
async fn download_stream(url: &str, state: Arc<AppState>) -> Result<(), reqwest::Error> {
println!("Starting download from: {}", url);
let response = reqwest::Client::new().get(url).send().await?;
let mut stream = response.bytes_stream();
let mut total_size = 0;
while let Some(chunk) = stream.next().await {
let bytes = chunk?;
total_size += bytes.len();
state.add_chunk(bytes).await;
if total_size % (1024 * 1024) == 0 {
// Log every MB
println!("Downloaded: {} MB", total_size / (1024 * 1024));
}
}
state.mark_complete().await;
println!("Download complete! Total: {} bytes", total_size);
Ok(())
}