@@ -182,6 +182,15 @@ async fn dispatcher_thread(
182
182
}
183
183
184
184
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
+
185
194
let mut response = client
186
195
. get ( req. url . as_ref ( ) )
187
196
. send ( )
@@ -195,12 +204,8 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
195
204
tokio:: fs:: create_dir_all ( parent) . await ?;
196
205
}
197
206
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) ;
204
209
loop {
205
210
tokio:: select! {
206
211
_ = & mut req. cancel => {
@@ -215,10 +220,7 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
215
220
Ok ( Some ( chunk) ) => {
216
221
file. write_all( & chunk) . await ?;
217
222
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) ;
222
224
}
223
225
Ok ( None ) => break ,
224
226
Err ( e) => return Err ( Error :: Reqwest ( e) ) ,
@@ -227,6 +229,7 @@ async fn download_thread(client: Client, req: &mut DownloadRequest) -> Result<Fi
227
229
}
228
230
}
229
231
}
232
+ update_progress ( bytes_downloaded, total_bytes) ;
230
233
231
234
// Ensure the data is written to disk
232
235
file. sync_all ( ) . await ?;
0 commit comments