skin support, fix #15

This commit is contained in:
jomo 2014-11-24 00:53:39 +01:00
parent 796f410248
commit 55ed86a3f6
3 changed files with 59 additions and 19 deletions

2
app.js
View File

@ -20,7 +20,7 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/avatars', avatars);
app.use('/', avatars);
// catch 404 and forward to error handler

View File

@ -53,6 +53,16 @@ function store_images(uuid, details, callback) {
});
}
var exp = {};
// returns true if the +uuid+ is a valid uuid or username
// the uuid may be not exist, however
exp.uuid_valid = function(uuid) {
return valid_uuid.test(uuid);
};
// decides whether to get an image from disk or to download it
// callback contains error, status, hash
// the status gives information about how the image was received
@ -61,7 +71,7 @@ function store_images(uuid, details, callback) {
// 1: "cached" - found on disk
// 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
// 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
function get_image_hash(uuid, callback) {
exp.get_image_hash = function(uuid, callback) {
cache.get_details(uuid, function(err, details) {
if (err) {
callback(err, -1, null);
@ -72,9 +82,6 @@ function get_image_hash(uuid, callback) {
callback(null, (details.hash ? 1 : 0), details.hash);
} else {
logging.log(uuid + " uuid not known or too old");
logging.log("details:");
logging.log(details);
logging.log("/details");
store_images(uuid, details, function(err, hash) {
if (err) {
callback(err, -1, details && details.hash);
@ -88,23 +95,16 @@ function get_image_hash(uuid, callback) {
}
}
});
}
var exp = {};
// returns true if the +uuid+ is a valid uuid or username
// the uuid may be not exist, however
exp.uuid_valid = function(uuid) {
return valid_uuid.test(uuid);
};
// handles requests for +uuid+ images with +size+
// callback contains error, status, image buffer
// image is the user's face+helm when helm is true, or the face otherwise
// for status, see get_image_hash
exp.get_avatar = function(uuid, helm, size, callback) {
logging.log("\nrequest: " + uuid);
get_image_hash(uuid, function(err, status, hash) {
exp.get_image_hash(uuid, function(err, status, hash) {
if (hash) {
var filepath = __dirname + '/../' + (helm ? config.helms_dir : config.faces_dir) + hash + ".png";
skins.resize_img(filepath, size, function(img_err, result) {

View File

@ -1,3 +1,5 @@
var networking = require('../modules/networking');
var logging = require('../modules/logging');
var helpers = require('../modules/helpers');
var router = require('express').Router();
var config = require('../modules/config');
@ -11,8 +13,46 @@ var human_status = {
"-1": "error"
};
router.get('/skins/:uuid.:ext?', function(req, res) {
var uuid = req.params.uuid;
var start = new Date();
if (!helpers.uuid_valid(uuid)) {
res.status(422).send("422 Invalid UUID");
return;
}
// strip dashes
uuid = uuid.replace(/-/g, "");
try {
helpers.get_image_hash(uuid, function(err, status, hash) {
if (hash) {
res.writeHead(301, {
'Location': "http://textures.minecraft.net/texture/" + hash,
'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
'Response-Time': new Date() - start,
'X-Storage-Type': human_status[status]
});
res.end();
} else if (!err) {
res.writeHead(404, {
'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
'Response-Time': new Date() - start,
'X-Storage-Type': human_status[status]
});
res.end("404 Not found");
} else {
res.status(500).send("500 Internal server error");
}
});
} catch(e) {
logging.error("Error!");
logging.error(e);
res.status(500).send("500 Internal server error");
}
});
/* GET avatar request. */
router.get('/:uuid.:ext?', function(req, res) {
router.get('/avatars/:uuid.:ext?', function(req, res) {
var uuid = req.params.uuid;
var size = req.query.size || config.default_size;
var def = req.query.default;
@ -35,9 +75,9 @@ router.get('/:uuid.:ext?', function(req, res) {
try {
helpers.get_avatar(uuid, helm, size, function(err, status, image) {
console.log(uuid + " - " + human_status[status]);
logging.log(uuid + " - " + human_status[status]);
if (err) {
console.error(err);
logging.error(err);
}
if (image) {
sendimage(err ? 503 : 200, status, image);
@ -46,8 +86,8 @@ router.get('/:uuid.:ext?', function(req, res) {
}
});
} catch(e) {
console.error("Error!");
console.error(e);
logging.error("Error!");
logging.error(e);
handle_default(500, status);
}