-
-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor a method to asynchronously download a file on native, with a…
… simple progress bar. #523 Use it in the updater and all the importer tools. Only place it's not used yet is parking_mapper and the in-game updater. [rebuild] Also make the RunCommand state understand the control code for erasing the current line. It... sort of works.
- Loading branch information
1 parent
810c89e
commit 77709a2
Showing
10 changed files
with
83 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::io::Write; | ||
|
||
use anyhow::{Context, Result}; | ||
|
||
use abstutil::prettyprint_usize; | ||
|
||
/// Downloads bytes from a URL. If `quiet` is false, prints progress. This must be called with a | ||
/// tokio runtime somewhere. | ||
pub async fn download_bytes<I: AsRef<str>>(url: I, quiet: bool) -> Result<Vec<u8>> { | ||
let url = url.as_ref(); | ||
let mut resp = reqwest::get(url).await.unwrap(); | ||
resp.error_for_status_ref() | ||
.with_context(|| format!("downloading {}", url))?; | ||
|
||
let total_size = resp.content_length().map(|x| x as usize); | ||
let mut bytes = Vec::new(); | ||
while let Some(chunk) = resp.chunk().await.unwrap() { | ||
if let Some(n) = total_size { | ||
if !quiet { | ||
abstutil::clear_current_line(); | ||
print!( | ||
"{:.2}% ({} / {} bytes)", | ||
(bytes.len() as f64) / (n as f64) * 100.0, | ||
prettyprint_usize(bytes.len()), | ||
prettyprint_usize(n) | ||
); | ||
} | ||
} | ||
|
||
bytes.write_all(&chunk).unwrap(); | ||
} | ||
if !quiet { | ||
println!(); | ||
} | ||
Ok(bytes) | ||
} | ||
|
||
/// Downloads a file. If `quiet` is false, prints progress. This must be called with a tokio | ||
/// runtime somewhere. | ||
pub async fn download_to_file<I: AsRef<str>>(url: I, path: String, quiet: bool) -> Result<()> { | ||
let bytes = download_bytes(url, quiet).await?; | ||
std::fs::create_dir_all(std::path::Path::new(&path).parent().unwrap())?; | ||
let mut file = std::fs::File::create(&path)?; | ||
file.write_all(&bytes)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters