Bläddra i källkod

Rewrite skin support

Jake 10 år sedan
förälder
incheckning
a4c11f396f
5 ändrade filer med 29 tillägg och 49 borttagningar
  1. 3 1
      app.js
  2. 1 1
      modules/cache.js
  3. 19 1
      modules/helpers.js
  4. 4 4
      modules/networking.js
  5. 2 42
      routes/avatars.js

+ 3 - 1
app.js

@@ -6,6 +6,7 @@ var bodyParser = require('body-parser');
 
 
 var routes = require('./routes/index');
 var routes = require('./routes/index');
 var avatars = require('./routes/avatars');
 var avatars = require('./routes/avatars');
+var skins = require('./routes/skins')
 
 
 var app = express();
 var app = express();
 
 
@@ -20,7 +21,8 @@ app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));
 app.use(express.static(path.join(__dirname, 'public')));
 
 
 app.use('/', routes);
 app.use('/', routes);
-app.use('/', avatars);
+app.use('/avatars', avatars);
+app.use('/skins', skins)
 
 
 
 
 // catch 404 and forward to error handler
 // catch 404 and forward to error handler

+ 1 - 1
modules/cache.js

@@ -16,7 +16,7 @@ function connect_redis() {
   }
   }
   redis.on("ready", function() {
   redis.on("ready", function() {
     logging.log("Redis connection established.");
     logging.log("Redis connection established.");
-    if(process.env.HEROKU) {
+    if(process.env.HEROKU || true) {
       logging.log("Running on heroku, flushing redis");
       logging.log("Running on heroku, flushing redis");
       redis.flushall();
       redis.flushall();
     }
     }

+ 19 - 1
modules/helpers.js

@@ -36,7 +36,7 @@ function store_images(uuid, details, callback) {
           var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
           var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
           var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
           var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
           // download skin, extract face/helm
           // 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) {
             if (err) {
               callback(err, null);
               callback(err, null);
             } else {
             } 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;
 module.exports = exp;

+ 4 - 4
modules/networking.js

@@ -102,10 +102,10 @@ exp.get_skin_url = function(uuid, callback) {
 // stores face image as +facename+
 // stores face image as +facename+
 // stores helm image as +helmname+
 // stores helm image as +helmname+
 // callback contains error
 // 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.");
     logging.log("Images already exist, not downloading.");
-    callback(null);
+    callback(null, null);
     return;
     return;
   }
   }
   request.get({
   request.get({
@@ -116,7 +116,7 @@ exp.skin_file = function(url, facename, helmname, callback) {
     if (!error && response.statusCode == 200) {
     if (!error && response.statusCode == 200) {
       // skin downloaded successfully
       // skin downloaded successfully
       logging.log(url + " skin downloaded");
       logging.log(url + " skin downloaded");
-      callback(error, body);
+      callback(null, body);
     } else {
     } else {
       if (error) {
       if (error) {
         logging.error("Error downloading '" + url + "': " + error);
         logging.error("Error downloading '" + url + "': " + error);

+ 2 - 42
routes/avatars.js

@@ -1,7 +1,7 @@
+var router = require('express').Router();
 var networking = require('../modules/networking');
 var networking = require('../modules/networking');
 var logging = require('../modules/logging');
 var logging = require('../modules/logging');
 var helpers = require('../modules/helpers');
 var helpers = require('../modules/helpers');
-var router = require('express').Router();
 var config = require('../modules/config');
 var config = require('../modules/config');
 var skins = require('../modules/skins');
 var skins = require('../modules/skins');
 
 
@@ -13,48 +13,8 @@ var human_status = {
   "-1": "error"
   "-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. */
 /* GET avatar request. */
-router.get('/avatars/:uuid.:ext?', function(req, res) {
+router.get('/:uuid.:ext?', function(req, res) {
   var uuid = req.params.uuid;
   var uuid = req.params.uuid;
   var size = req.query.size || config.default_size;
   var size = req.query.size || config.default_size;
   var def = req.query.default;
   var def = req.query.default;