add ?default=alex|steve; more error handling

This commit is contained in:
jomo 2014-10-20 01:22:59 +02:00
parent 77bcf9dbe7
commit 507e0e9220
3 changed files with 74 additions and 44 deletions

View File

@ -9,29 +9,54 @@ var valid_uuid = /^[0-9a-f]{32}$/;
router.get('/:uuid/:size?', function(req, res) {
var uuid = req.param('uuid');
var size = req.param('size') || 180;
var def = req.query.default;
// Prevent app from crashing/freezing
if (size <= 0 || size > 512) size = 180;
console.log(uuid);
if (valid_uuid.test(uuid)) {
var filename = uuid + ".png";
if (fs.existsSync("skins/" + filename)) {
skins.resize_img(filename, size, function(data) {
res.writeHead(200, {'Content-Type': 'image/png'});
console.log('found ' + filename);
skins.resize_img("skins/" + filename, size, function(data) {
// tell browser to cache image locally for 10 minutes
res.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
res.end(data);
});
} else {
console.log(filename + ' not found, downloading profile..');
skins.get_profile(uuid, function(profile) {
var skinurl = skins.skin_url(profile);
if (skinurl) {
console.log('got profile, skin url is "' + skinurl + '" downloading..');
skins.skin_file(skinurl, filename, function() {
skins.resize_img(filename, size, function(data) {
res.writeHead(200, {'Content-Type': 'image/png'});
console.log('got skin');
skins.resize_img("skins/" + filename, size, function(data) {
// tell browser to cache image locally for 10 minutes
res.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
res.end(data);
});
});
} else {
res.status(404) // HTTP status 404: NotFound
console.log('no skin url found');
switch (def) {
case "alex":
skins.resize_img("public/images/alex.png", size, function(data) {
// tell browser to cache image locally for 10 minutes
res.writeHead(404, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
res.end(data);
});
break;
case "steve":
skins.resize_img("public/images/steve.png", size, function(data) {
// tell browser to cache image locally for 10 minutes
res.writeHead(404, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
res.end(data);
});
break;
default:
res.status(404)
.send('404 Not found');
break;
}
}
});
}

View File

@ -37,8 +37,8 @@ module.exports = {
}
});
}).on('error', function(e) {
console.error(e);
}).on('error', function(err) {
throw err;
});
},
@ -49,7 +49,7 @@ module.exports = {
if (prop.name == 'textures') {
var json = Buffer(prop.value, 'base64').toString();
var props = JSON.parse(json);
url = props.textures.SKIN.url;
url = props && props.textures && props.textures.SKIN && props.textures.SKIN.url;
}
});
}
@ -72,11 +72,14 @@ module.exports = {
callback(); // outside unlink callback cause we don't have to wait until it's deleted
});
});
}).on('error', function(err) {
throw err;
});
},
resize_img: function(inname, size, callback) {
lwip.open("skins/" + inname, function(err, image) {
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) {

View File

@ -6,7 +6,7 @@ block content
.col-md-10
h1 Crafatar
hr
p Welcome to Crafatar, a website for server Minecraft skins and heads!
p Welcome to Crafatar, an API for Minecraft's faces!
h2 API
hr
p
@ -28,6 +28,8 @@ block content
| "&gt;
p(style="margin-top: 10px;") Example: UUID: 'ae795aa86327408e92ab25c8a59f3ba1', Size: 250px, Username: redstone_sheep
.well &lt;img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250"&gt;
p(style="margin-top: 10px;") By default, a 404 text is returned when the avatar was not found. You can change that to the avatar of steve or alex:
.well &lt;img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250?default=alex"&gt;
.col-md-2
img(src= "/avatars/2d5aa9cdaeb049189930461fc9b91cc5")
img(src= "/avatars/ae795aa86327408e92ab25c8a59f3ba1")