From 41e56409b89bcb7918221d088c685776884f3983 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 10 Feb 2021 18:24:10 -0800 Subject: download: restyle progress bar; check HTTP content length --- fatcat-cli/src/download.rs | 15 +++++++++++---- 1 file 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 + 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) => { -- cgit v1.2.3