Clean up archive downloading

This commit is contained in:
James 2018-01-30 15:54:44 +11:00
parent b8851f158a
commit c8af1009a2
3 changed files with 19 additions and 17 deletions

View File

@ -9,19 +9,21 @@ use reqwest;
use std::io::Read; use std::io::Read;
/// Streams a file from a HTTP server. /// Streams a file from a HTTP server.
pub fn stream_file<F>(url : String, callback : F) -> Result<(), String> pub fn stream_file<F>(url: String, callback: F) -> Result<(), String>
// |data : Vec<u8>, total : u64| // |data : Vec<u8>, total : u64|
where F: Fn(Vec<u8>, u64) -> () { where
F: Fn(Vec<u8>, u64) -> (),
{
let mut client = match reqwest::get(&url) { let mut client = match reqwest::get(&url) {
Ok(v) => v, Ok(v) => v,
Err(v) => return Err(format!("Failed to GET resource: {:?}", v)), Err(v) => return Err(format!("Failed to GET resource: {:?}", v)),
}; };
let size = { let size = {
let size : Option<&ContentLength> = client.headers().get(); let size: Option<&ContentLength> = client.headers().get();
match size { match size {
Some(&ContentLength(v)) => v, Some(&ContentLength(v)) => v,
None => 0 None => 0,
} }
}; };
@ -30,14 +32,14 @@ pub fn stream_file<F>(url : String, callback : F) -> Result<(), String>
let len = client.read(&mut buf); let len = client.read(&mut buf);
let len = match len { let len = match len {
Ok(v) => v, Ok(v) => v,
Err(v) => return Err(format!("Failed to read resource: {:?}", v)) Err(v) => return Err(format!("Failed to read resource: {:?}", v)),
}; };
if len == 0 { if len == 0 {
break; break;
} }
let buf_copy = &buf[0 .. len]; let buf_copy = &buf[0..len];
let buf_copy = buf_copy.to_vec(); let buf_copy = buf_copy.to_vec();
callback(buf_copy, size); callback(buf_copy, size);

View File

@ -18,7 +18,7 @@ use config::Config;
use http::stream_file; use http::stream_file;
use number_prefix::{decimal_prefix, Standalone, Prefixed}; use number_prefix::{decimal_prefix, Prefixed, Standalone};
/// A message thrown during the installation of packages. /// A message thrown during the installation of packages.
#[derive(Serialize)] #[derive(Serialize)]
@ -35,7 +35,7 @@ pub struct InstallerFramework {
} }
struct DownloadProgress { struct DownloadProgress {
downloaded: usize downloaded: usize,
} }
impl InstallerFramework { impl InstallerFramework {
@ -135,9 +135,7 @@ impl InstallerFramework {
println!("{:?}", latest_file); println!("{:?}", latest_file);
// Download this file // Download this file
let lock = Arc::new(Mutex::new(DownloadProgress { let lock = Arc::new(Mutex::new(DownloadProgress { downloaded: 0 }));
downloaded: 0
}));
stream_file(latest_file.url, |data, size| { stream_file(latest_file.url, |data, size| {
let mut reference = lock.lock().unwrap(); let mut reference = lock.lock().unwrap();
@ -157,18 +155,20 @@ impl InstallerFramework {
// Pretty print data volumes // Pretty print data volumes
let pretty_current = match decimal_prefix(reference.downloaded as f64) { let pretty_current = match decimal_prefix(reference.downloaded as f64) {
Standalone(bytes) => format!("{} bytes", bytes), Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix), Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
}; };
let pretty_total = match decimal_prefix(size as f64) { let pretty_total = match decimal_prefix(size as f64) {
Standalone(bytes) => format!("{} bytes", bytes), Standalone(bytes) => format!("{} bytes", bytes),
Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix), Prefixed(prefix, n) => format!("{:.0} {}B", n, prefix),
}; };
messages messages
.send(InstallMessage::Status( .send(InstallMessage::Status(
format!("Downloading {} ({} of {})", package.name, pretty_current, format!(
pretty_total), "Downloading {} ({} of {})",
package.name, pretty_current, pretty_total
),
global_percentage, global_percentage,
)) ))
.unwrap(); .unwrap();

View File

@ -7,8 +7,8 @@ extern crate hyper;
extern crate hyper_tls; extern crate hyper_tls;
extern crate tokio_core; extern crate tokio_core;
extern crate reqwest;
extern crate number_prefix; extern crate number_prefix;
extern crate reqwest;
extern crate includedir; extern crate includedir;
extern crate phf; extern crate phf;