Handle errors when fetching skins better

This commit is contained in:
Thinkofname 2016-04-21 13:09:20 +01:00
parent feb7ea1630
commit df37ec283d
1 changed files with 83 additions and 63 deletions

View File

@ -848,28 +848,49 @@ impl TextureManager {
Ok(val) => val, Ok(val) => val,
Err(_) => return, // Most likely shutting down Err(_) => return, // Most likely shutting down
}; };
match Self::obtain_skin(&client, &hash) {
Ok(img) => {
let _ = reply.send((hash, Some(img)));
},
Err(err) => {
error!("Failed to get skin {:?}: {}", hash, err);
let _ = reply.send((hash, None));
},
}
}
}
fn obtain_skin(client: &::hyper::Client, hash: &str) -> Result<image::DynamicImage, ::std::io::Error> {
use std::io::Read;
use std::fs;
use std::path::Path;
use std::io::{Error, ErrorKind};
let path = format!("skin-cache/{}/{}.png", &hash[..2], hash); let path = format!("skin-cache/{}/{}.png", &hash[..2], hash);
let cache_path = Path::new(&path); let cache_path = Path::new(&path);
fs::create_dir_all(cache_path.parent().unwrap()).unwrap(); try!(fs::create_dir_all(cache_path.parent().unwrap()));
let mut buf = vec![]; let mut buf = vec![];
if fs::metadata(cache_path).is_ok() { if fs::metadata(cache_path).is_ok() {
// We have a cached image // We have a cached image
let mut file = fs::File::open(cache_path).unwrap(); let mut file = try!(fs::File::open(cache_path));
file.read_to_end(&mut buf).unwrap(); try!(file.read_to_end(&mut buf));
} else { } else {
// Need to download it // Need to download it
let url = format!("http://textures.minecraft.net/texture/{}", hash); let url = format!("http://textures.minecraft.net/texture/{}", hash);
let mut res = client.get(&url).send().unwrap(); let mut res = match client.get(&url).send() {
res.read_to_end(&mut buf).unwrap(); Ok(val) => val,
Err(err) => {
return Err(Error::new(ErrorKind::ConnectionAborted, err));
}
};
try!(res.read_to_end(&mut buf));
// Save to cache // Save to cache
let mut file = fs::File::create(cache_path).unwrap(); let mut file = try!(fs::File::create(cache_path));
file.write_all(&buf).unwrap(); try!(file.write_all(&buf));
} }
let mut img = match image::load_from_memory(&buf) { let mut img = match image::load_from_memory(&buf) {
Ok(val) => val, Ok(val) => val,
Err(_) => { Err(err) => {
let _ = reply.send((hash, None)); return Err(Error::new(ErrorKind::InvalidData, err));
continue;
} }
}; };
let (_, height) = img.dimensions(); let (_, height) = img.dimensions();
@ -913,8 +934,7 @@ impl TextureManager {
} }
} }
} }
let _ = reply.send((hash, Some(img))); Ok(img)
}
} }
fn update_textures(&mut self, version: usize) { fn update_textures(&mut self, version: usize) {