diff --git a/src/download.rs b/src/download.rs index 4197a78..3e6ecdb 100644 --- a/src/download.rs +++ b/src/download.rs @@ -207,6 +207,10 @@ pub async fn download_feedback_chunks(url: &str, into_file: &str, rep: DlReporte if buff.len() > 0 { ofile.write_all(&buff).await?; } + + if curr_progress != content_length { + return Err(DlError::HttpNoData.into()); + } // Ensure that IO is completed //ofile.flush().await?; @@ -291,7 +295,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter let mut t_last = t_start.clone(); - joiners.push(tokio::task::spawn(async move { + let manager_handle = tokio::task::spawn(async move { let rep = rep_task; //let mut dl_speeds = vec![0.0_f32; conn_count as usize]; @@ -357,8 +361,7 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter } } - Ok(()) - })); + }); let mut joiners: FuturesUnordered<_> = joiners.into_iter().collect(); @@ -374,12 +377,16 @@ pub async fn download_feedback_multi(url: &str, into_file: &str, rep: DlReporter handle.abort(); } + manager_handle.abort(); + tokio::fs::remove_file(&into_file).await?; return Err(e.into()); } } + manager_handle.await?; + // Remove the additional byte at the file end let ofile = tokio::fs::OpenOptions::new() .create(false) diff --git a/src/errors.rs b/src/errors.rs index 6de17f1..b9bb222 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -9,6 +9,7 @@ pub type ResBE = Result>; pub enum DlError { BadHttpStatus, ContentLengthUnknown, + HttpNoData, Other(String) } @@ -20,6 +21,7 @@ impl Display for DlError { match self { DlError::BadHttpStatus => write!(f, "Bad http response status"), DlError::ContentLengthUnknown => write!(f, "Content-Length is unknown"), + DlError::HttpNoData => write!(f, "Http server sent no more data"), DlError::Other(s) => write!(f, "Unknown download error: '{}'", s) } } diff --git a/src/main.rs b/src/main.rs index 53b8b9a..a8ee53e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -266,7 +266,7 @@ async fn download_multiple(urls: Vec, outdir: &str, file_count: i32, con if conn_count == 1 { if let Err(_e) = download::download_feedback(&url, &into_file, rep.clone(), Some(filesize)).await { rep.send(DlStatus::DoneErr { - filename: into_file.to_string() + filename: file_name.to_string() }); } } else { @@ -280,7 +280,7 @@ async fn download_multiple(urls: Vec, outdir: &str, file_count: i32, con if let Err(_e) = download::download_feedback_multi(&url, &into_file, rep.clone(), conn_count, Some(filesize)).await { rep.send(DlStatus::DoneErr { - filename: into_file.to_string() + filename: file_name.to_string() }); } };