Rewrite skin support

This commit is contained in:
Jake 2014-11-29 12:21:35 -06:00
parent caf7c731da
commit a4c11f396f
5 changed files with 29 additions and 49 deletions

4
app.js
View File

@ -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

View File

@ -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();
}

View File

@ -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;

View File

@ -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);

View File

@ -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;