better error handling

This commit is contained in:
jomo
2014-10-30 00:21:16 +01:00
parent a07ca210d6
commit d84d55f533
4 changed files with 60 additions and 34 deletions

View File

@@ -36,29 +36,38 @@ exp.uuid_valid = function(uuid) {
// error, status, image buffer
//
// the status gives information about how the image was received
// -1: profile requested, but it was not found
// -1: error
// 1: found on disk
// 2: profile requested/found, skin downloaded from mojang servers
// 3: profile requested/found, but it has no skin
exp.get_avatar = function(uuid, size, callback) {
var filepath = config.skins_dir + uuid + ".png";
if (fs.existsSync(filepath)) {
skins.resize_img(filepath, size, function(result) {
callback(null, 1, result);
skins.resize_img(filepath, size, function(err, result) {
callback(err, 1, result);
});
} else {
networking.get_profile(uuid, function(err, profile) {
if (err) {
callback(err, -1, profile);
return;
}
var skinurl = exp.skin_url(profile);
if (skinurl) {
networking.skin_file(skinurl, filepath, function() {
console.log('got skin');
skins.resize_img(filepath, size, function(result) {
callback(null, 2, result);
});
networking.skin_file(skinurl, filepath, function(err) {
if (err) {
callback(err, -1, null);
} else {
console.log('got skin');
skins.resize_img(filepath, size, function(err, result) {
if (err) {
callback(err, -1, null);
} else {
callback(null, 2, result);
}
});
}
});
} else {
// profile found, but has no skin

View File

@@ -40,12 +40,12 @@ exp.skin_file = function(url, outname, callback) {
timeout: config.http_timeout // ms
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
skins.extract_face(body, outname, function() {
callback();
skins.extract_face(body, outname, function(err) {
callback(err);
});
} else {
if (error) {
console.error(error);
console.error("Error downloading '" + url + "': " + error);
} else if (response.statusCode == 404) {
console.warn("Texture not found: " + url);
} else if (response.statusCode == 429) {

View File

@@ -4,15 +4,22 @@ var exp = {};
// extracts the face from an image +buffer+
// save it to a file called +outname+
// callback has an error parameter which can be null
exp.extract_face = function(buffer, outname, callback) {
lwip.open(buffer, "png", function(err, image) {
if (err) throw err;
image.batch()
.crop(8, 8, 15, 15)
.writeFile(outname, function(err) {
if (err) throw err;
callback();
});
if (err) {
callback(err);
} else {
image.batch()
.crop(8, 8, 15, 15)
.writeFile(outname, function(err) {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
});
};
@@ -20,12 +27,15 @@ exp.extract_face = function(buffer, outname, callback) {
// +callback+ is a buffer of the resized image
exp.resize_img = function(inname, size, callback) {
lwip.open(inname, function(err, image) {
if (err) throw err;
image.batch()
.resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
.toBuffer('png', function(err, buffer) {
callback(buffer);
});
if (err) {
callback(err, null);
} else {
image.batch()
.resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
.toBuffer('png', function(err, buffer) {
callback(null, buffer);
});
}
});
};