Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 8f4ec9d

Browse files
committed
simplify download state
1 parent baf4ea7 commit 8f4ec9d

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

src/bin/soar-dl/progress.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ fn calculate_speed(pos: u64, elapsed: f64) -> u64 {
3838

3939
pub fn handle_progress(state: DownloadState, progress_bar: &ProgressBar) {
4040
match state {
41+
DownloadState::Preparing(total_size) => {
42+
progress_bar.set_length(total_size);
43+
}
4144
DownloadState::Progress(progress) => {
42-
if let Some(total) = progress.total_bytes {
43-
progress_bar.set_length(total);
44-
}
45-
progress_bar.set_position(progress.bytes_downloaded);
45+
progress_bar.set_position(progress);
4646
}
4747
DownloadState::Complete => progress_bar.finish(),
4848
}

src/downloader.rs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,11 @@ use crate::{
2222

2323
#[derive(Debug, Clone)]
2424
pub 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-
3730
pub 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
}

src/oci.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ impl OciClient {
142142
});
143143
}
144144

145-
let filename = layer.get_title().unwrap();
145+
let Some(filename) = layer.get_title() else {
146+
// skip if layer doesn't contain title
147+
return Ok(0);
148+
};
149+
146150
let (temp_path, final_path) = if let Some(output_dir) = output_dir {
147151
let output_dir = output_dir.as_ref();
148152
fs::create_dir_all(output_dir).await?;

0 commit comments

Comments
 (0)