diff --git a/routes/avatars.js b/routes/avatars.js index e457c83..c549398 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -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 - .send('404 Not found'); + 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; + } } }); } diff --git a/skins.js b/skins.js index 404d20d..40f2638 100644 --- a/skins.js +++ b/skins.js @@ -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) { diff --git a/views/index.jade b/views/index.jade index 1df4f84..3ea70c0 100644 --- a/views/index.jade +++ b/views/index.jade @@ -1,36 +1,38 @@ extends layout block content - .container(style= "margin-top: 30px;") - .row - .col-md-10 - h1 Crafatar - hr - p Welcome to Crafatar, a website for server Minecraft skins and heads! - h2 API - hr - p - | Replace - mark.green uuid - | with a Mojang UUID to get the related head. - .well - | <img src="https://skins.jake0oo0.me/avatars/ - mark.green uuid - | "> - p(style="margin-top: 10px;") Example: UUID: '2d5aa9cdaeb049189930461fc9b91cc5', Username: Jake0oo0 - .well <img src="https://skins.jake0oo0.me/avatars/2d5aa9cdaeb049189930461fc9b91cc5"> - p(style="margin-top: 10px;") There is also an option to include a size, default is 180x180px. The size may not be larger than 512px. - .well - | <img src="https://skins.jake0oo0.me/avatars/ - mark.green uuid - | / - mark.green size - | "> - p(style="margin-top: 10px;") Example: UUID: 'ae795aa86327408e92ab25c8a59f3ba1', Size: 250px, Username: redstone_sheep - .well <img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250"> - .col-md-2 - img(src= "/avatars/2d5aa9cdaeb049189930461fc9b91cc5") - img(src= "/avatars/ae795aa86327408e92ab25c8a59f3ba1") - img(src= "/avatars/dab0a06256934c28bf0211142b388c33") - img(src= "/avatars/3c7db14dac4b4e35b2c63b2237f382be") - img(src= "/avatars/62c41f0e133d4192ad33f0896f47d23a") + .container(style= "margin-top: 30px;") + .row + .col-md-10 + h1 Crafatar + hr + p Welcome to Crafatar, an API for Minecraft's faces! + h2 API + hr + p + | Replace + mark.green uuid + | with a Mojang UUID to get the related head. + .well + | <img src="https://skins.jake0oo0.me/avatars/ + mark.green uuid + | "> + p(style="margin-top: 10px;") Example: UUID: '2d5aa9cdaeb049189930461fc9b91cc5', Username: Jake0oo0 + .well <img src="https://skins.jake0oo0.me/avatars/2d5aa9cdaeb049189930461fc9b91cc5"> + p(style="margin-top: 10px;") There is also an option to include a size, default is 180x180px. The size may not be larger than 512px. + .well + | <img src="https://skins.jake0oo0.me/avatars/ + mark.green uuid + | / + mark.green size + | "> + p(style="margin-top: 10px;") Example: UUID: 'ae795aa86327408e92ab25c8a59f3ba1', Size: 250px, Username: redstone_sheep + .well <img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250"> + 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 <img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250?default=alex"> + .col-md-2 + img(src= "/avatars/2d5aa9cdaeb049189930461fc9b91cc5") + img(src= "/avatars/ae795aa86327408e92ab25c8a59f3ba1") + img(src= "/avatars/dab0a06256934c28bf0211142b388c33") + img(src= "/avatars/3c7db14dac4b4e35b2c63b2237f382be") + img(src= "/avatars/62c41f0e133d4192ad33f0896f47d23a")