Skip to content

Commit 9f25867

Browse files
downloader: End progress updates at completion too
1 parent b9911b8 commit 9f25867

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/downloader.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ async fn dispatcher_thread(
182182
}
183183

184184
async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<File, Error> {
185+
let update_progress = |bytes_downloaded: u64, total_bytes: Option<u64>| {
186+
req.status
187+
.send(Status::InProgress(DownloadProgress {
188+
bytes_downloaded,
189+
total_bytes,
190+
}))
191+
.ok();
192+
};
193+
185194
let mut response = client
186195
.get(req.url.as_ref())
187196
.send()
@@ -195,12 +204,8 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
195204
tokio::fs::create_dir_all(parent).await?;
196205
}
197206
let mut file = File::create(&req.destination).await?;
198-
req.status
199-
.send(Status::InProgress(DownloadProgress {
200-
bytes_downloaded,
201-
total_bytes,
202-
}))
203-
.ok();
207+
208+
update_progress(bytes_downloaded, total_bytes);
204209
loop {
205210
tokio::select! {
206211
_ = &mut req.cancel => {
@@ -215,10 +220,7 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
215220
Ok(Some(chunk)) => {
216221
file.write_all(&chunk).await?;
217222
bytes_downloaded += chunk.len() as u64;
218-
req.status.send(Status::InProgress(DownloadProgress {
219-
bytes_downloaded,
220-
total_bytes,
221-
})).ok();
223+
update_progress(bytes_downloaded, total_bytes);
222224
}
223225
Ok(None) => break,
224226
Err(e) => return Err(Error::Reqwest(e)),
@@ -227,6 +229,7 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
227229
}
228230
}
229231
}
232+
update_progress(bytes_downloaded, total_bytes);
230233

231234
// Ensure the data is written to disk
232235
file.sync_all().await?;

0 commit comments

Comments
 (0)