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

@ -11,7 +11,9 @@ 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) -> () {
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)),
@ -21,7 +23,7 @@ pub fn stream_file<F>(url : String, callback : F) -> Result<(), String>
let size: Option<&ContentLength> = client.headers().get();
match size {
Some(&ContentLength(v)) => v,
None => 0
None => 0,
}
};
@ -30,7 +32,7 @@ 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 {

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();
@ -167,8 +165,10 @@ impl InstallerFramework {
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;