Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloads: Handle an updated content length and add better error checking #405

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions examples/scenes/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,46 @@ impl Download {
to_download = downloads;
}
}
let mut completed_count = 0;
let mut failed_count = 0;
for (index, download) in to_download.iter().enumerate() {
println!(
"{index}: Downloading {} from {}",
download.name, download.url
);
download.fetch(&self.directory, self.size_limit)?;
match download.fetch(&self.directory, self.size_limit) {
Ok(()) => completed_count += 1,
Err(e) => {
failed_count += 1;
eprintln!("Download failed with error: {e}");
let cont = if self.auto {
false
} else {
dialoguer::Confirm::new()
.with_prompt("Would you like to try other downloads?")
.wait_for_newline(true)
.default(false)
.interact()?
};
if !cont {
println!("{} downloads complete", completed_count);
if failed_count > 0 {
println!("{} downloads failed", failed_count);
}
let remaining = to_download.len() - (completed_count + failed_count);
if remaining > 0 {
println!("{} downloads skipped", remaining);
}
return Err(e);
}
}
}
}
println!("{} downloads complete", completed_count);
if failed_count > 0 {
println!("{} downloads failed", failed_count);
}
println!("{} downloads complete", to_download.len());
debug_assert!(completed_count + failed_count == to_download.len());
Ok(())
}

Expand Down Expand Up @@ -137,6 +169,21 @@ impl SVGDownload {
size_limit = builtin.expected_size;
limit_exact = true;
}
// If we're expecting an exact version of the file, it's worth not fetching
// the file if we know it will fail
if limit_exact {
let head_response = ureq::head(&self.url).call()?;
let content_length = head_response.header("content-length");
if let Some(Ok(content_length)) = content_length.map(|it| it.parse::<u64>()) {
if content_length != size_limit {
bail!(
"Size is not as expected for download. Expected {}, server reported {}",
Byte::from_bytes(size_limit.into()).get_appropriate_unit(true),
Byte::from_bytes(content_length.into()).get_appropriate_unit(true)
)
}
}
}
let mut file = std::fs::OpenOptions::new()
.create_new(true)
.write(true)
Expand Down