@@ -22,18 +22,11 @@ use crate::{
2222
2323#[ derive( Debug , Clone ) ]
2424pub enum DownloadState {
25- Progress ( DownloadProgress ) ,
25+ Preparing ( u64 ) ,
26+ Progress ( u64 ) ,
2627 Complete ,
2728}
2829
29- #[ derive( Debug , Clone ) ]
30- pub struct DownloadProgress {
31- pub bytes_downloaded : u64 ,
32- pub total_bytes : Option < u64 > ,
33- pub url : String ,
34- pub file_path : String ,
35- }
36-
3730pub struct DownloadOptions {
3831 pub url : String ,
3932 pub output_path : Option < String > ,
@@ -67,7 +60,7 @@ impl Downloader {
6760 } ) ;
6861 }
6962
70- let content_length = response. content_length ( ) ;
63+ let content_length = response. content_length ( ) . unwrap_or ( 0 ) ;
7164
7265 let filename = options
7366 . output_path
@@ -98,20 +91,19 @@ impl Downloader {
9891 . await ?;
9992
10093 let mut downloaded_bytes = 0u64 ;
101- let mut progress_callback = options. progress_callback ;
94+ let progress_callback = options. progress_callback ;
95+
96+ if let Some ( ref callback) = progress_callback {
97+ callback ( DownloadState :: Preparing ( content_length) ) ;
98+ }
10299
103100 while let Some ( chunk) = stream. next ( ) . await {
104101 let chunk = chunk. unwrap ( ) ;
105102 file. write_all ( & chunk) . await . unwrap ( ) ;
106103 downloaded_bytes = downloaded_bytes. saturating_add ( chunk. len ( ) as u64 ) ;
107104
108- if let Some ( ref mut callback) = progress_callback {
109- callback ( DownloadState :: Progress ( DownloadProgress {
110- bytes_downloaded : downloaded_bytes,
111- total_bytes : content_length,
112- url : options. url . clone ( ) ,
113- file_path : filename. clone ( ) ,
114- } ) ) ;
105+ if let Some ( ref callback) = progress_callback {
106+ callback ( DownloadState :: Progress ( downloaded_bytes) ) ;
115107 }
116108 }
117109
@@ -121,8 +113,8 @@ impl Downloader {
121113 fs:: set_permissions ( & output_path, Permissions :: from_mode ( 0o755 ) ) . await ?;
122114 }
123115
124- if let Some ( ref cb ) = progress_callback {
125- cb ( DownloadState :: Complete ) ;
116+ if let Some ( ref callback ) = progress_callback {
117+ callback ( DownloadState :: Complete ) ;
126118 }
127119
128120 Ok ( filename)
@@ -139,12 +131,7 @@ impl Downloader {
139131 let total_bytes: u64 = manifest. layers . iter ( ) . map ( |layer| layer. size ) . sum ( ) ;
140132
141133 if let Some ( ref callback) = options. progress_callback {
142- callback ( DownloadState :: Progress ( DownloadProgress {
143- bytes_downloaded : 0 ,
144- total_bytes : Some ( total_bytes) ,
145- url : options. url . clone ( ) ,
146- file_path : String :: new ( ) ,
147- } ) ) ;
134+ callback ( DownloadState :: Preparing ( total_bytes) ) ;
148135 }
149136
150137 let downloaded_bytes = Arc :: new ( Mutex :: new ( 0u64 ) ) ;
@@ -154,26 +141,20 @@ impl Downloader {
154141 let client_clone = oci_client. clone ( ) ;
155142 let cb_clone = options. progress_callback . clone ( ) ;
156143 let downloaded_bytes = downloaded_bytes. clone ( ) ;
157- let url = options. url . clone ( ) ;
158144 let outdir = outdir. clone ( ) ;
159145
160146 let task = task:: spawn ( async move {
161- let chunk_size = client_clone
147+ client_clone
162148 . pull_layer ( & layer, outdir, move |bytes| {
163149 if let Some ( ref callback) = cb_clone {
164150 let mut current = downloaded_bytes. lock ( ) . unwrap ( ) ;
165151 * current = bytes;
166- callback ( DownloadState :: Progress ( DownloadProgress {
167- bytes_downloaded : * current,
168- total_bytes : Some ( total_bytes) ,
169- url : url. clone ( ) ,
170- file_path : String :: new ( ) ,
171- } ) ) ;
152+ callback ( DownloadState :: Progress ( * current) ) ;
172153 }
173154 } )
174155 . await ?;
175156
176- Ok :: < u64 , DownloadError > ( chunk_size )
157+ Ok :: < ( ) , DownloadError > ( ( ) )
177158 } ) ;
178159 tasks. push ( task) ;
179160 }
0 commit comments