38 lines
1.0 KiB
Rust
38 lines
1.0 KiB
Rust
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(())
|
|
}
|