diff --git a/app.js b/app.js index cc2e7b6..5bb2244 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ var bodyParser = require('body-parser'); var routes = require('./routes/index'); var avatars = require('./routes/avatars'); +var skins = require('./routes/skins') var app = express(); @@ -20,7 +21,8 @@ app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); -app.use('/', avatars); +app.use('/avatars', avatars); +app.use('/skins', skins) // catch 404 and forward to error handler diff --git a/modules/cache.js b/modules/cache.js index ed1420d..0f42f5a 100644 --- a/modules/cache.js +++ b/modules/cache.js @@ -16,7 +16,7 @@ function connect_redis() { } redis.on("ready", function() { logging.log("Redis connection established."); - if(process.env.HEROKU) { + if(process.env.HEROKU || true) { logging.log("Running on heroku, flushing redis"); redis.flushall(); } diff --git a/modules/helpers.js b/modules/helpers.js index 963042e..7ce43c9 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -36,7 +36,7 @@ function store_images(uuid, details, callback) { var facepath = __dirname + '/../' + config.faces_dir + hash + ".png"; var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png"; // download skin, extract face/helm - networking.skin_file(skin_url, facepath, helmpath, function(err, img) { + networking.skin_file(skin_url, facepath, function(err, img) { if (err) { callback(err, null); } else { @@ -143,4 +143,22 @@ exp.get_avatar = function(uuid, helm, size, callback) { }); }; +exp.get_skin = function(uuid, callback) { + logging.log("\nskin request: " + uuid); + exp.get_image_hash(uuid, function(err, status, hash) { + if (hash) { + var skinurl = "http://textures.minecraft.net/texture/" + hash; + networking.skin_file(skinurl, null, function(err, img) { + if (err) { + logging.log("\nerror while downloading skin"); + callback(err, hash, null); + } else { + logging.log("\nreturning skin"); + callback(null, hash, img); + } + }); + } + }); +}; + module.exports = exp; \ No newline at end of file diff --git a/modules/networking.js b/modules/networking.js index 243e039..39d02fa 100644 --- a/modules/networking.js +++ b/modules/networking.js @@ -102,10 +102,10 @@ exp.get_skin_url = function(uuid, callback) { // stores face image as +facename+ // stores helm image as +helmname+ // callback contains error -exp.skin_file = function(url, facename, helmname, callback) { - if (fs.existsSync(facename) && fs.existsSync(facename)) { +exp.skin_file = function(url, facename, callback) { + if (facename && fs.existsSync(facename)) { logging.log("Images already exist, not downloading."); - callback(null); + callback(null, null); return; } request.get({ @@ -116,7 +116,7 @@ exp.skin_file = function(url, facename, helmname, callback) { if (!error && response.statusCode == 200) { // skin downloaded successfully logging.log(url + " skin downloaded"); - callback(error, body); + callback(null, body); } else { if (error) { logging.error("Error downloading '" + url + "': " + error); diff --git a/routes/avatars.js b/routes/avatars.js index 24ae9bf..0d2f3af 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -1,7 +1,7 @@ +var router = require('express').Router(); 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'); var skins = require('../modules/skins'); @@ -13,48 +13,8 @@ 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, - 'Access-Control-Allow-Origin': '*', - '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, - 'Access-Control-Allow-Origin': '*', - '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('/avatars/:uuid.:ext?', function(req, res) { +router.get('/:uuid.:ext?', function(req, res) { var uuid = req.params.uuid; var size = req.query.size || config.default_size; var def = req.query.default;