diff --git a/modules/helpers.js b/modules/helpers.js index 9e12b21..c9d674c 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -100,7 +100,7 @@ exp.get_image_hash = function(uuid, callback) { // handles requests for +uuid+ images with +size+ -// callback contains error, status, image buffer +// callback contains error, status, image buffer, hash // 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) { @@ -115,16 +115,16 @@ exp.get_avatar = function(uuid, helm, size, callback) { } skins.resize_img(filepath, size, function(img_err, result) { if (img_err) { - callback(img_err, -1, null); + callback(img_err, -1, null, hash); } else { // we might have a hash although an error occured // (e.g. Mojang servers not reachable, using outdated hash) - callback(err, (err ? -1 : status), result); + callback(err, (err ? -1 : status), result, hash); } }); } else { // hash is null when uuid has no skin - callback(err, status, null); + callback(err, status, null, null); } }); }; diff --git a/routes/avatars.js b/routes/avatars.js index 3d30496..9565a17 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -58,6 +58,7 @@ router.get('/avatars/:uuid.:ext?', function(req, res) { var def = req.query.default; var helm = req.query.hasOwnProperty('helm'); var start = new Date(); + var etag = null; // Prevent app from crashing/freezing if (size < config.min_size || size > config.max_size) { @@ -74,13 +75,23 @@ router.get('/avatars/:uuid.:ext?', function(req, res) { uuid = uuid.replace(/-/g, ""); try { - helpers.get_avatar(uuid, helm, size, function(err, status, image) { + helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) { logging.log(uuid + " - " + human_status[status]); if (err) { logging.error(err); } + etag = hash && hash.substr(0, 32) + (helm ? "-helm-" : "-face-") + size || "none"; + var matches = req.get("If-None-Match") == '"' + etag + '"'; if (image) { - sendimage(err ? 503 : 200, status, image); + var http_status = 200; + if (matches) { + http_status = 304; + } else if (err) { + http_status = 503; + } + console.log("matches: " + matches); + console.log("status: " + http_status); + sendimage(http_status, status, image); } else { handle_default(404, status); } @@ -113,9 +124,10 @@ router.get('/avatars/:uuid.:ext?', function(req, res) { 'Content-Type': 'image/png', 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public', 'Response-Time': new Date() - start, - 'X-Storage-Type': human_status[img_status] + 'X-Storage-Type': human_status[img_status], + 'Etag': '"' + etag + '"' }); - res.end(image); + res.end(http_status == 304 ? null : image); } });