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,
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 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![];
if fs::metadata(cache_path).is_ok() {
// We have a cached image
let mut file = fs::File::open(cache_path).unwrap();
file.read_to_end(&mut buf).unwrap();
let mut file = try!(fs::File::open(cache_path));
try!(file.read_to_end(&mut buf));
} else {
// Need to download it
let url = format!("http://textures.minecraft.net/texture/{}", hash);
let mut res = client.get(&url).send().unwrap();
res.read_to_end(&mut buf).unwrap();
let mut res = match client.get(&url).send() {
Ok(val) => val,
Err(err) => {
return Err(Error::new(ErrorKind::ConnectionAborted, err));
}
};
try!(res.read_to_end(&mut buf));
// Save to cache
let mut file = fs::File::create(cache_path).unwrap();
file.write_all(&buf).unwrap();
let mut file = try!(fs::File::create(cache_path));
try!(file.write_all(&buf));
}
let mut img = match image::load_from_memory(&buf) {
Ok(val) => val,
Err(_) => {
let _ = reply.send((hash, None));
continue;
Err(err) => {
return Err(Error::new(ErrorKind::InvalidData, err));
}
};
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) {