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;
/// Streams a file from a HTTP server.
pub fn stream_file<F>(url : String, callback : F) -> Result<(), String>
// |data : Vec<u8>, total : u64|
where F: Fn(Vec<u8>, u64) -> () {
pub fn stream_file<F>(url: String, callback: F) -> Result<(), String>
// |data : Vec<u8>, total : u64|
where
F: Fn(Vec<u8>, u64) -> (),
{
let mut client = match reqwest::get(&url) {
Ok(v) => v,
Err(v) => return Err(format!("Failed to GET resource: {:?}", v)),
};
let size = {
let size : Option<&ContentLength> = client.headers().get();
let size: Option<&ContentLength> = client.headers().get();
match size {
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 = match len {
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 {
break;
}
let buf_copy = &buf[0 .. len];
let buf_copy = &buf[0..len];
let buf_copy = buf_copy.to_vec();
callback(buf_copy, size);

View File

@ -18,7 +18,7 @@ use config::Config;
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.
#[derive(Serialize)]
@ -35,7 +35,7 @@ pub struct InstallerFramework {
}
struct DownloadProgress {
downloaded: usize
downloaded: usize,
}
impl InstallerFramework {
@ -135,9 +135,7 @@ impl InstallerFramework {
println!("{:?}", latest_file);
// Download this file
let lock = Arc::new(Mutex::new(DownloadProgress {
downloaded: 0
}));
let lock = Arc::new(Mutex::new(DownloadProgress { downloaded: 0 }));
stream_file(latest_file.url, |data, size| {
let mut reference = lock.lock().unwrap();
@ -157,18 +155,20 @@ impl InstallerFramework {
// Pretty print data volumes
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),
};
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),
};
messages
.send(InstallMessage::Status(
format!("Downloading {} ({} of {})", package.name, pretty_current,
pretty_total),
format!(
"Downloading {} ({} of {})",
package.name, pretty_current, pretty_total
),
global_percentage,
))
.unwrap();

View File

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