Skip to content

Commit 9adb239

Browse files
downloader: Send initial progress update and return error for response
1 parent 1bf7bfd commit 9adb239

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/downloader.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ async fn dispatcher_thread(
172172
}
173173

174174
async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<File, Error> {
175-
let mut response = client.get(req.url.as_ref()).send().await?;
175+
let mut response = client
176+
.get(req.url.as_ref())
177+
.send()
178+
.await?
179+
.error_for_status()?;
176180
let total_bytes = response.content_length();
177181
let mut bytes_downloaded = 0u64;
178182

@@ -181,7 +185,12 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
181185
tokio::fs::create_dir_all(parent).await?;
182186
}
183187
let mut file = File::create(&req.destination).await?;
184-
188+
req.status
189+
.send(Status::InProgress(DownloadProgress {
190+
bytes_downloaded,
191+
total_bytes,
192+
}))
193+
.ok();
185194
loop {
186195
tokio::select! {
187196
_ = &mut req.cancel => {
@@ -196,10 +205,10 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
196205
Ok(Some(chunk)) => {
197206
file.write_all(&chunk).await?;
198207
bytes_downloaded += chunk.len() as u64;
199-
let _ = req.status.send(Status::InProgress(DownloadProgress {
208+
req.status.send(Status::InProgress(DownloadProgress {
200209
bytes_downloaded,
201210
total_bytes,
202-
}));
211+
})).ok();
203212
}
204213
Ok(None) => break,
205214
Err(e) => return Err(Error::Reqwest(e)),

0 commit comments

Comments
 (0)