aboutsummaryrefslogtreecommitdiffstats
path: root/fatcat-cli/src
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2021-02-10 18:24:10 -0800
committerBryan Newbold <bnewbold@archive.org>2021-02-10 18:24:10 -0800
commit41e56409b89bcb7918221d088c685776884f3983 (patch)
treea20d061a3f8c1bef7c6bb8ba0c068c0814a0845a /fatcat-cli/src
parentb85353d4b6c8ff6fd71c31b3bd618c76afcd6a39 (diff)
downloadfatcat-cli-41e56409b89bcb7918221d088c685776884f3983.tar.gz
fatcat-cli-41e56409b89bcb7918221d088c685776884f3983.zip
download: restyle progress bar; check HTTP content length
Diffstat (limited to 'fatcat-cli/src')
-rw-r--r--fatcat-cli/src/download.rs15
1 files changed, 11 insertions, 4 deletions
diff --git a/fatcat-cli/src/download.rs b/fatcat-cli/src/download.rs
index cf520fe..2297652 100644
--- a/fatcat-cli/src/download.rs
+++ b/fatcat-cli/src/download.rs
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Context, Result};
use fatcat_openapi::models::{FileEntity, ReleaseEntity};
-use indicatif::ProgressBar;
+use indicatif::{ProgressBar, ProgressStyle};
use log::info;
use reqwest::header::USER_AGENT;
use std::fmt;
@@ -155,15 +155,22 @@ pub fn download_file(fe: &FileEntity, specifier: &Specifier, output_path: Option
}
};
- // TODO: parse headers, eg size (?)
if !resp.status().is_success() {
std::fs::remove_file(download_path)?;
return Ok(DownloadStatus::HttpError(resp.status().as_u16()));
}
- // TODO: what if no filesize?
- // TODO: compare with resp.content_length(() -> Option<u64>
+ if let Some(content_bytes) = resp.content_length() {
+ if content_bytes != expected_size {
+ std::fs::remove_file(download_path)?;
+ return Ok(DownloadStatus::WrongSize);
+ }
+ }
+
let pb = ProgressBar::new(fe.size.unwrap() as u64);
+ pb.set_style(ProgressStyle::default_bar()
+ .template("{spinner:.green} [{elapsed_precise}] [{bar:40}] {bytes}/{total_bytes} ({eta})")
+ .progress_chars("#>-"));
let out_size = match resp.copy_to(&mut pb.wrap_write(download_file)) {
Ok(r) => r,
Err(e) => {