Răsfoiți Sursa

:heart: eslint

jomo 10 ani în urmă
părinte
comite
af03fb63f8
16 a modificat fișierele cu 394 adăugiri și 373 ștergeri
  1. 7 7
      lib/cache.js
  2. 27 21
      lib/cleaner.js
  3. 1 1
      lib/config.example.js
  4. 55 50
      lib/helpers.js
  5. 1 1
      lib/logging.js
  6. 26 22
      lib/networking.js
  7. 116 115
      lib/renders.js
  8. 26 22
      lib/skins.js
  9. 1 1
      lib/www.js
  10. 15 15
      routes/avatars.js
  11. 14 14
      routes/capes.js
  12. 2 1
      routes/index.js
  13. 46 47
      routes/renders.js
  14. 38 36
      routes/skins.js
  15. 3 3
      server.js
  16. 16 17
      test/test.js

+ 7 - 7
lib/cache.js

@@ -22,7 +22,7 @@ function connect_redis() {
   }
   redis.on("ready", function() {
     logging.log("Redis connection established.");
-    if(process.env.HEROKU) {
+    if (process.env.HEROKU) {
       logging.log("Running on heroku, flushing redis");
       redis.flushall();
     }
@@ -43,7 +43,7 @@ function update_file_date(rid, skin_hash) {
     fs.exists(path, function(exists) {
       if (exists) {
         var date = new Date();
-        fs.utimes(path, date, date, function(err){
+        fs.utimes(path, date, date, function(err) {
           if (err) {
             logging.error(rid, "Error:", err.stack);
           }
@@ -81,7 +81,7 @@ exp.info = function(callback) {
       }
     });
     obj.versions = [];
-    if( obj.redis_version ){
+    if (obj.redis_version) {
       obj.redis_version.split(".").forEach(function(num) {
         obj.versions.push(+num);
       });
@@ -98,7 +98,7 @@ exp.info = function(callback) {
 // callback: error
 exp.update_timestamp = function(rid, userId, hash, temp, callback) {
   logging.log(rid, "cache: updating timestamp");
-  sub = temp ? (config.local_cache_time - 60) : 0;
+  var sub = temp ? (config.local_cache_time - 60) : 0;
   var time = new Date().getTime() - sub;
   // store userId in lower case if not null
   userId = userId && userId.toLowerCase();
@@ -121,15 +121,15 @@ exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) {
   // store userId in lower case if not null
   userId = userId && userId.toLowerCase();
   if (skin_hash === undefined) {
-    redis.hmset(userId, "c", cape_hash, "t", time, function(err){
+    redis.hmset(userId, "c", cape_hash, "t", time, function(err) {
       callback(err);
     });
   } else if (cape_hash === undefined) {
-    redis.hmset(userId, "s", skin_hash, "t", time, function(err){
+    redis.hmset(userId, "s", skin_hash, "t", time, function(err) {
       callback(err);
     });
   } else {
-    redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err){
+    redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err) {
       callback(err);
     });
   }

+ 27 - 21
lib/cleaner.js

@@ -1,12 +1,16 @@
 var logging = require("./logging");
 var config = require("./config");
 var cache = require("./cache");
+var path = require("path");
 var df = require("node-df");
 var fs = require("fs");
 
 var redis = cache.get_redis();
 var exp = {};
 
+// does nothing
+function nil() {}
+
 // compares redis' used_memory with cleaning_redis_limit
 // callback: error, true|false
 function should_clean_redis(callback) {
@@ -15,7 +19,7 @@ function should_clean_redis(callback) {
       callback(err, false);
     } else {
       try {
-        //logging.debug(info.toString());
+        // logging.debug(info.toString());
         logging.debug("used mem:" + info.used_memory);
         var used = parseInt(info.used_memory) / 1024;
         logging.log("RedisCleaner:", used + "KB used");
@@ -31,7 +35,7 @@ function should_clean_redis(callback) {
 // callback: error, true|false
 function should_clean_disk(callback) {
   df({
-    file: __dirname + "/../" + config.faces_dir,
+    file: path.join(__dirname, "..", config.faces_dir),
     prefixMultiplier: "KiB",
     isDisplayPrefixMultiplier: false,
     precision: 2
@@ -67,25 +71,29 @@ exp.run = function() {
       logging.error(err);
     } else if (clean) {
       logging.warn("DiskCleaner: Disk limit reached! Cleaning images now");
-      var facesdir = __dirname + "/../" + config.faces_dir;
-      var helmdir = __dirname + "/../" + config.helms_dir;
-      var renderdir = __dirname + "/../" + config.renders_dir;
-      var skindir = __dirname + "/../" + config.skins_dir;
-      fs.readdir(facesdir, function (err, files) {
-        for (var i = 0, l = Math.min(files.length, config.cleaning_amount); i < l; i++) {
-          var filename = files[i];
-          if (filename[0] !== ".") {
-            fs.unlink(facesdir + filename, nil);
-            fs.unlink(helmdir + filename, nil);
-            fs.unlink(skindir + filename, nil);
+      var facesdir = path.join(__dirname, "..", config.faces_dir);
+      var helmdir = path.join(__dirname, "..", config.helms_dir);
+      var renderdir = path.join(__dirname, "..", config.renders_dir);
+      var skindir = path.join(__dirname, "..", config.skins_dir);
+      fs.readdir(facesdir, function (readerr, files) {
+        if (!readerr) {
+          for (var i = 0, l = Math.min(files.length, config.cleaning_amount); i < l; i++) {
+            var filename = files[i];
+            if (filename[0] !== ".") {
+              fs.unlink(path.join(facesdir, filename), nil);
+              fs.unlink(path.join(helmdir, filename), nil);
+              fs.unlink(path.join(skindir, filename), nil);
+            }
           }
         }
       });
-      fs.readdir(renderdir, function (err, files) {
-        for (var j = 0, l = Math.min(files.length, config.cleaning_amount); j < l; j++) {
-          var filename = files[j];
-          if (filename[0] !== ".") {
-            fs.unlink(renderdir + filename, nil);
+      fs.readdir(renderdir, function (readerr, files) {
+        if (!readerr) {
+          for (var j = 0, l = Math.min(files.length, config.cleaning_amount); j < l; j++) {
+            var filename = files[j];
+            if (filename[0] !== ".") {
+              fs.unlink(renderdir + filename, nil);
+            }
           }
         }
       });
@@ -95,6 +103,4 @@ exp.run = function() {
   });
 };
 
-function nil () {}
-
-module.exports = exp;
+module.exports = exp;

+ 1 - 1
lib/config.example.js

@@ -22,4 +22,4 @@ var config = {
   log_time: true,                 // set to false if you use an external logger that provides timestamps
 };
 
-module.exports = config;
+module.exports = config;

+ 55 - 50
lib/helpers.js

@@ -1,9 +1,10 @@
 var networking = require("./networking");
 var logging = require("./logging");
+var renders = require("./renders");
 var config = require("./config");
 var cache = require("./cache");
 var skins = require("./skins");
-var renders = require("./renders");
+var path = require("path");
 var fs = require("fs");
 
 // 0098cb60-fa8e-427c-b299-793cbd302c9a
@@ -24,13 +25,13 @@ function store_skin(rid, userId, profile, cache_details, callback) {
     if (!err && url) {
       var skin_hash = get_hash(url);
       if (cache_details && cache_details.skin === skin_hash) {
-        cache.update_timestamp(rid, userId, skin_hash, false, function(err) {
-          callback(err, skin_hash);
+        cache.update_timestamp(rid, userId, skin_hash, false, function(cache_err) {
+          callback(cache_err, skin_hash);
         });
       } else {
         logging.log(rid, "new skin hash:", skin_hash);
-        var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png";
-        var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png";
+        var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
+        var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
         fs.exists(facepath, function(exists) {
           if (exists) {
             logging.log(rid, "skin already exists, not downloading");
@@ -73,25 +74,25 @@ function store_cape(rid, userId, profile, cache_details, callback) {
     if (!err && url) {
       var cape_hash = get_hash(url);
       if (cache_details && cache_details.cape === cape_hash) {
-        cache.update_timestamp(rid, userId, cape_hash, false, function(err) {
-          callback(err, cape_hash);
+        cache.update_timestamp(rid, userId, cape_hash, false, function(cache_err) {
+          callback(cache_err, cape_hash);
         });
       } else {
         logging.log(rid, "new cape hash:", cape_hash);
-        var capepath = __dirname + "/../" + config.capes_dir + cape_hash + ".png";
+        var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
         fs.exists(capepath, function(exists) {
           if (exists) {
             logging.log(rid, "cape already exists, not downloading");
             callback(null, cape_hash);
           } else {
-            networking.get_from(rid, url, function(img, response, err) {
-              if (err || !img) {
-                logging.error(rid, err.stack);
-                callback(err, null);
+            networking.get_from(rid, url, function(img, response, net_err) {
+              if (net_err || !img) {
+                logging.error(rid, net_err.stack);
+                callback(net_err, null);
               } else {
-                skins.save_image(img, capepath, function(err) {
+                skins.save_image(img, capepath, function(skin_err) {
                   logging.debug(rid, "cape saved");
-                  callback(err, cape_hash);
+                  callback(skin_err, cape_hash);
                 });
               }
             });
@@ -171,23 +172,23 @@ function store_images(rid, userId, cache_details, type, callback) {
         }
       } else {
         // no error and we have a profile (if it's a uuid)
-        store_skin(rid, userId, profile, cache_details, function(err, skin_hash) {
-          if (err && !skin_hash) {
+        store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash) {
+          if (store_err && !skin_hash) {
             // an error occured, not caching. we can try in 60 seconds
-            callback_for(userId, "skin", err, null);
+            callback_for(userId, "skin", store_err, null);
           } else {
             cache.save_hash(rid, userId, skin_hash, null, function(cache_err) {
-              callback_for(userId, "skin", (err || cache_err), skin_hash);
+              callback_for(userId, "skin", (store_err || cache_err), skin_hash);
             });
           }
         });
-        store_cape(rid, userId, profile, cache_details, function(err, cape_hash) {
-          if (err && !cape_hash) {
+        store_cape(rid, userId, profile, cache_details, function(store_err, cape_hash) {
+          if (store_err && !cape_hash) {
             // an error occured, not caching. we can try in 60 seconds
-            callback_for(userId, "cape", (err), cape_hash);
+            callback_for(userId, "cape", (store_err), cape_hash);
           } else {
             cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
-              callback_for(userId, "cape", (err || cache_err), cape_hash);
+              callback_for(userId, "cape", (store_err || cache_err), cape_hash);
             });
           }
         });
@@ -217,7 +218,10 @@ exp.id_valid = function(userId) {
 //   3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
 exp.get_image_hash = function(rid, userId, type, callback) {
   cache.get_details(userId, function(err, cache_details) {
-    var cached_hash = (cache_details !== null) ? (type === "skin" ? cache_details.skin : cache_details.cape) : null;
+    var cached_hash = null;
+    if (cache_details !== null) {
+      cached_hash = type === "skin" ? cache_details.skin : cache_details.cape;
+    }
     if (err) {
       callback(err, -1, null);
     } else {
@@ -232,12 +236,12 @@ exp.get_image_hash = function(rid, userId, type, callback) {
         } else {
           logging.log(rid, "userId not cached");
         }
-        store_images(rid, userId, cache_details, type, function(err, new_hash) {
-          if (err) {
+        store_images(rid, userId, cache_details, type, function(store_err, new_hash) {
+          if (store_err) {
             // we might have a cached hash although an error occured
             // (e.g. Mojang servers not reachable, using outdated hash)
             cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
-              callback(err2 || err, -1, cache_details && cached_hash);
+              callback(err2 || store_err, -1, cache_details && cached_hash);
             });
           } else {
             var status = cache_details && (cached_hash === new_hash) ? 3 : 2;
@@ -259,8 +263,8 @@ exp.get_image_hash = function(rid, userId, type, callback) {
 exp.get_avatar = function(rid, userId, helm, size, callback) {
   exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
     if (skin_hash) {
-      var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png";
-      var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png";
+      var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
+      var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
       var filepath = facepath;
       fs.exists(helmpath, function(exists) {
         if (helm && exists) {
@@ -285,16 +289,17 @@ exp.get_avatar = function(rid, userId, helm, size, callback) {
 // callback: error, skin hash, image buffer
 exp.get_skin = function(rid, userId, callback) {
   exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
-    var skinpath = __dirname + "/../" + config.skins_dir + skin_hash + ".png";
+    // FIXME: err is not used / handled
+    var skinpath = path.join(__dirname, "..", config.skins_dir, skin_hash + ".png");
     fs.exists(skinpath, function(exists) {
       if (exists) {
         logging.log(rid, "skin already exists, not downloading");
-        skins.open_skin(rid, skinpath, function(err, img) {
-          callback(err, skin_hash, img);
+        skins.open_skin(rid, skinpath, function(skin_err, img) {
+          callback(skin_err, skin_hash, img);
         });
       } else {
-        networking.save_texture(rid, skin_hash, skinpath, function(err, response, img) {
-          callback(err, skin_hash, img);
+        networking.save_texture(rid, skin_hash, skinpath, function(net_err, response, img) {
+          callback(net_err, skin_hash, img);
         });
       }
     });
@@ -317,11 +322,11 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
       callback(err, -1, skin_hash, null);
       return;
     }
-    var renderpath = __dirname + "/../" + config.renders_dir + skin_hash + "-" + scale + "-" + get_type(helm, body) + ".png";
+    var renderpath = path.join(__dirname, "..", config.renders_dir, [skin_hash, "-", scale, "-", get_type(helm, body)].join("-") + ".png");
     fs.exists(renderpath, function(exists) {
       if (exists) {
-        renders.open_render(rid, renderpath, function(err, img) {
-          callback(err, 1, skin_hash, img);
+        renders.open_render(rid, renderpath, function(render_err, rendered_img) {
+          callback(render_err, 1, skin_hash, rendered_img);
         });
         return;
       } else {
@@ -329,15 +334,15 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
           callback(err, 0, skin_hash, null);
           return;
         }
-        renders.draw_model(rid, img, scale, helm, body, function(err, img) {
-          if (err) {
-            callback(err, -1, skin_hash, null);
-          } else if (!img) {
+        renders.draw_model(rid, img, scale, helm, body, function(draw_err, drawn_img) {
+          if (draw_err) {
+            callback(draw_err, -1, skin_hash, null);
+          } else if (!drawn_img) {
             callback(null, 0, skin_hash, null);
           } else {
-            fs.writeFile(renderpath, img, "binary", function(err) {
-              if (err) {
-                logging.error(rid, err.stack);
+            fs.writeFile(renderpath, drawn_img, "binary", function(fs_err) {
+              if (fs_err) {
+                logging.error(rid, fs_err.stack);
               }
               callback(null, 2, skin_hash, img);
             });
@@ -356,19 +361,19 @@ exp.get_cape = function(rid, userId, callback) {
       callback(err, null, null);
       return;
     }
-    var capepath = __dirname + "/../" + config.capes_dir + cape_hash + ".png";
+    var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
     fs.exists(capepath, function(exists) {
       if (exists) {
         logging.log(rid, "cape already exists, not downloading");
-        skins.open_skin(rid, capepath, function(err, img) {
-          callback(err, cape_hash, img);
+        skins.open_skin(rid, capepath, function(skin_err, img) {
+          callback(skin_err, cape_hash, img);
         });
       } else {
-        networking.save_texture(rid, cape_hash, capepath, function(err, response, img) {
+        networking.save_texture(rid, cape_hash, capepath, function(net_err, response, img) {
           if (response && response.statusCode === 404) {
-            callback(err, cape_hash, null);
+            callback(net_err, cape_hash, null);
           } else {
-            callback(err, cape_hash, img);
+            callback(net_err, cape_hash, img);
           }
         });
       }
@@ -376,4 +381,4 @@ exp.get_cape = function(rid, userId, callback) {
   });
 };
 
-module.exports = exp;
+module.exports = exp;

+ 1 - 1
lib/logging.js

@@ -39,7 +39,7 @@ if (config.debug_enabled || process.env.DEBUG === "true") {
     log("DEBUG", arguments);
   };
 } else {
-  exp.debug = function(){};
+  exp.debug = function() {};
 }
 
 module.exports = exp;

+ 26 - 22
lib/networking.js

@@ -29,16 +29,31 @@ function extract_url(profile, type) {
   return url;
 }
 
+// helper method that calls `get_username_url` or `get_uuid_url` based on the +usedId+
+// +userId+ is used for usernames, while +profile+ is used for UUIDs
+function get_url(rid, userId, profile, type, callback) {
+  if (userId.length <= 16) {
+    // username
+    exp.get_username_url(rid, userId, type, function(err, url) {
+      callback(err, url || null);
+    });
+  } else {
+    exp.get_uuid_url(profile, type, function(url) {
+      callback(null, url || null);
+    });
+  }
+}
+
 // exracts the skin URL of a +profile+ object
 // returns null when no URL found (user has no skin)
 exp.extract_skin_url = function(profile) {
-  return extract_url(profile, 'SKIN');
+  return extract_url(profile, "SKIN");
 };
 
 // exracts the cape URL of a +profile+ object
 // returns null when no URL found (user has no cape)
 exp.extract_cape_url = function(profile) {
-  return extract_url(profile, 'CAPE');
+  return extract_url(profile, "CAPE");
 };
 
 // performs a GET request to the +url+
@@ -99,7 +114,11 @@ exp.get_from = function(rid, url, callback) {
 exp.get_username_url = function(rid, name, type, callback) {
   exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
     if (!err) {
-      callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null);
+      if (response) {
+        callback(err, response.statusCode === 404 ? null : response.headers.location);
+      } else {
+        callback(err, null);
+      }
     } else {
       callback(err, null);
     }
@@ -146,21 +165,6 @@ exp.get_cape_url = function(rid, userId, profile, callback) {
   });
 };
 
-// helper method that calls `get_username_url` or `get_uuid_url` based on the +usedId+
-// +userId+ is used for usernames, while +profile+ is used for UUIDs
-function get_url(rid, userId, profile, type, callback) {
-  if (userId.length <= 16) {
-    //username
-    exp.get_username_url(rid, userId, type, function(err, url) {
-      callback(err, url || null);
-    });
-  } else {
-    exp.get_uuid_url(profile, type, function(url) {
-      callback(null, url || null);
-    });
-  }
-}
-
 // download the +tex_hash+ image from the texture server
 // and save it in the +outpath+ file
 // callback: error, response, image buffer
@@ -172,11 +176,11 @@ exp.save_texture = function(rid, tex_hash, outpath, callback) {
         logging.error(rid, "error while downloading texture");
         callback(err, response, null);
       } else {
-        fs.writeFile(outpath, img, "binary", function(err) {
-          if (err) {
-            logging.error(rid, "error:", err.stack);
+        fs.writeFile(outpath, img, "binary", function(fs_err) {
+          if (fs_err) {
+            logging.error(rid, "error:", fs_err.stack);
           }
-          callback(err, response, img);
+          callback(fs_err, response, img);
         });
       }
     });

+ 116 - 115
lib/renders.js

@@ -8,34 +8,50 @@ var Canvas = require("canvas");
 var Image = Canvas.Image;
 var exp = {};
 
+// scales an image from the +imagedata+ onto the +context+
+// scaled by a factor of +scale+ with options +d_x+ and +d_y+
+function scale_image(imageData, context, d_x, d_y, scale) {
+  var width = imageData.width;
+  var height = imageData.height;
+  context.clearRect(0, 0, width, height); // Clear the spot where it originated from
+  for (var y = 0; y < height; y++) { // height original
+    for (var x = 0; x < width; x++) { // width original
+      // Gets original colour, then makes a scaled square of the same colour
+      var index = (x + y * width) * 4;
+      context.fillStyle = "rgba(" + imageData.data[index + 0] + ", " + imageData.data[index + 1] + ", " + imageData.data[index + 2] + ", " + imageData.data[index + 3] + ")";
+      context.fillRect(d_x + x * scale, d_y + y * scale, scale, scale);
+    }
+  }
+}
+
 // draws the helmet on to the +skin_canvas+
 // using the skin from the +model_ctx+ at the +scale+
 exp.draw_helmet = function(skin_canvas, model_ctx, scale) {
-  //Helmet - Front
-  model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-  model_ctx.drawImage(skin_canvas, 40*scale, 8*scale, 8*scale, 8*scale, 10*scale, 13/1.2*scale, 8*scale, 8*scale);
-  //Helmet - Right
-  model_ctx.setTransform(1,0.5,0,1.2,0,0);
-  model_ctx.drawImage(skin_canvas, 32*scale, 8*scale, 8*scale, 8*scale, 2*scale, 3/1.2*scale, 8*scale, 8*scale);
-  //Helmet - Top
-  model_ctx.setTransform(-1,0.5,1,0.5,0,0);
-  model_ctx.scale(-1,1);
-  model_ctx.drawImage(skin_canvas, 40*scale, 0, 8*scale, 8*scale, -3*scale, 5*scale, 8*scale, 8*scale);
+  // Helmet - Front
+  model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+  model_ctx.drawImage(skin_canvas, 40 * scale, 8 * scale, 8 * scale, 8 * scale, 10 * scale, 13 / 1.2 * scale, 8 * scale, 8 * scale);
+  // Helmet - Right
+  model_ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
+  model_ctx.drawImage(skin_canvas, 32 * scale, 8 * scale, 8 * scale, 8 * scale, 2 * scale, 3 / 1.2 * scale, 8 * scale, 8 * scale);
+  // Helmet - Top
+  model_ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
+  model_ctx.scale(-1, 1);
+  model_ctx.drawImage(skin_canvas, 40 * scale, 0, 8 * scale, 8 * scale, -3 * scale, 5 * scale, 8 * scale, 8 * scale);
 };
 
 // draws the head on to the +skin_canvas+
 // using the skin from the +model_ctx+ at the +scale+
 exp.draw_head = function(skin_canvas, model_ctx, scale) {
-  //Head - Front
-  model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-  model_ctx.drawImage(skin_canvas, 8*scale, 8*scale, 8*scale, 8*scale, 10*scale, 13/1.2*scale, 8*scale, 8*scale);
-  //Head - Right
-  model_ctx.setTransform(1,0.5,0,1.2,0,0);
-  model_ctx.drawImage(skin_canvas, 0, 8*scale, 8*scale, 8*scale, 2*scale, 3/1.2*scale, 8*scale, 8*scale);
-  //Head - Top
-  model_ctx.setTransform(-1,0.5,1,0.5,0,0);
-  model_ctx.scale(-1,1);
-  model_ctx.drawImage(skin_canvas, 8*scale, 0, 8*scale, 8*scale, -3*scale, 5*scale, 8*scale, 8*scale);
+  // Head - Front
+  model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+  model_ctx.drawImage(skin_canvas, 8 * scale, 8 * scale, 8 * scale, 8 * scale, 10 * scale, 13 / 1.2 * scale, 8 * scale, 8 * scale);
+  // Head - Right
+  model_ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
+  model_ctx.drawImage(skin_canvas, 0, 8 * scale, 8 * scale, 8 * scale, 2 * scale, 3 / 1.2 * scale, 8 * scale, 8 * scale);
+  // Head - Top
+  model_ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
+  model_ctx.scale(-1, 1);
+  model_ctx.drawImage(skin_canvas, 8 * scale, 0, 8 * scale, 8 * scale, -3 * scale, 5 * scale, 8 * scale, 8 * scale);
 };
 
 // draws the body on to the +skin_canvas+
@@ -44,84 +60,84 @@ exp.draw_head = function(skin_canvas, model_ctx, scale) {
 exp.draw_body = function(rid, skin_canvas, model_ctx, scale) {
   if (skin_canvas.height === 32 * scale) {
     logging.debug(rid, "uses old skin format");
-    //Left Leg
-    //Left Leg - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.scale(-1,1);
-    model_ctx.drawImage(skin_canvas, 4*scale, 20*scale, 4*scale, 12*scale, -16*scale, 34.4/1.2*scale, 4*scale, 12*scale);
-
-    //Right Leg
-    //Right Leg - Right
-    model_ctx.setTransform(1,0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 0*scale, 20*scale, 4*scale, 12*scale, 4*scale, 26.4/1.2*scale, 4*scale, 12*scale);
-    //Right Leg - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 4*scale, 20*scale, 4*scale, 12*scale, 8*scale, 34.4/1.2*scale, 4*scale, 12*scale);
-
-    //Arm Left
-    //Arm Left - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.scale(-1,1);
-    model_ctx.drawImage(skin_canvas, 44*scale, 20*scale, 4*scale, 12*scale, -20*scale, 20/1.2*scale, 4*scale, 12*scale);
-    //Arm Left - Top
-    model_ctx.setTransform(-1,0.5,1,0.5,0,0);
-    model_ctx.drawImage(skin_canvas, 44*scale, 16*scale, 4*scale, 4*scale, 0, 16*scale, 4*scale, 4*scale);
-
-    //Body
-    //Body - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 20*scale, 20*scale, 8*scale, 12*scale, 8*scale, 20/1.2*scale, 8*scale, 12*scale);
-
-    //Arm Right
-    //Arm Right - Right
-    model_ctx.setTransform(1,0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 40*scale, 20*scale, 4*scale, 12*scale, 0, 16/1.2*scale, 4*scale, 12*scale);
-    //Arm Right - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 44*scale, 20*scale, 4*scale, 12*scale, 4*scale, 20/1.2*scale, 4*scale, 12*scale);
-    //Arm Right - Top
-    model_ctx.setTransform(-1,0.5,1,0.5,0,0);
-    model_ctx.scale(-1,1);
-    model_ctx.drawImage(skin_canvas, 44*scale, 16*scale, 4*scale, 4*scale, -16*scale, 16*scale, 4*scale, 4*scale);
+    // Left Leg
+    // Left Leg - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.scale(-1, 1);
+    model_ctx.drawImage(skin_canvas, 4 * scale, 20 * scale, 4 * scale, 12 * scale, -16 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
+
+    // Right Leg
+    // Right Leg - Right
+    model_ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 0 * scale, 20 * scale, 4 * scale, 12 * scale, 4 * scale, 26.4 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Right Leg - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 4 * scale, 20 * scale, 4 * scale, 12 * scale, 8 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
+
+    // Arm Left
+    // Arm Left - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.scale(-1, 1);
+    model_ctx.drawImage(skin_canvas, 44 * scale, 20 * scale, 4 * scale, 12 * scale, -20 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Arm Left - Top
+    model_ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
+    model_ctx.drawImage(skin_canvas, 44 * scale, 16 * scale, 4 * scale, 4 * scale, 0, 16 * scale, 4 * scale, 4 * scale);
+
+    // Body
+    // Body - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 20 * scale, 20 * scale, 8 * scale, 12 * scale, 8 * scale, 20 / 1.2 * scale, 8 * scale, 12 * scale);
+
+    // Arm Right
+    // Arm Right - Right
+    model_ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 40 * scale, 20 * scale, 4 * scale, 12 * scale, 0, 16 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Arm Right - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 44 * scale, 20 * scale, 4 * scale, 12 * scale, 4 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Arm Right - Top
+    model_ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
+    model_ctx.scale(-1, 1);
+    model_ctx.drawImage(skin_canvas, 44 * scale, 16 * scale, 4 * scale, 4 * scale, -16 * scale, 16 * scale, 4 * scale, 4 * scale);
   } else {
     logging.debug(rid, "uses new skin format");
-    //Left Leg
-    //Left Leg - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 20*scale, 52*scale, 4*scale, 12*scale, 12*scale, 34.4/1.2*scale, 4*scale, 12*scale);
-
-    //Right Leg
-    //Right Leg - Right
-    model_ctx.setTransform(1,0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 0, 20*scale, 4*scale, 12*scale, 4*scale, 26.4/1.2*scale, 4*scale, 12*scale);
-    //Right Leg - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 4*scale, 20*scale, 4*scale, 12*scale, 8*scale, 34.4/1.2*scale, 4*scale, 12*scale);
-
-    //Arm Left
-    //Arm Left - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 36*scale, 52*scale, 4*scale, 12*scale, 16*scale, 20/1.2*scale, 4*scale, 12*scale);
-    //Arm Left - Top
-    model_ctx.setTransform(-1,0.5,1,0.5,0,0);
-    model_ctx.drawImage(skin_canvas, 36*scale, 48*scale, 4*scale, 4*scale, 0, 16*scale, 4*scale, 4*scale);
-
-    //Body
-    //Body - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 20*scale, 20*scale, 8*scale, 12*scale, 8*scale, 20/1.2*scale, 8*scale, 12*scale);
-
-    //Arm Right
-    //Arm Right - Right
-    model_ctx.setTransform(1,0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 40*scale, 20*scale, 4*scale, 12*scale, 0, 16/1.2*scale, 4*scale, 12*scale);
-    //Arm Right - Front
-    model_ctx.setTransform(1,-0.5,0,1.2,0,0);
-    model_ctx.drawImage(skin_canvas, 44*scale, 20*scale, 4*scale, 12*scale, 4*scale, 20/1.2*scale, 4*scale, 12*scale);
-    //Arm Right - Top
-    model_ctx.setTransform(-1,0.5,1,0.5,0,0);
-    model_ctx.scale(-1,1);
-    model_ctx.drawImage(skin_canvas, 44*scale, 16*scale, 4*scale, 4*scale, -16*scale, 16*scale, 4*scale, 4*scale);
+    // Left Leg
+    // Left Leg - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 20 * scale, 52 * scale, 4 * scale, 12 * scale, 12 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
+
+    // Right Leg
+    // Right Leg - Right
+    model_ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 0, 20 * scale, 4 * scale, 12 * scale, 4 * scale, 26.4 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Right Leg - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 4 * scale, 20 * scale, 4 * scale, 12 * scale, 8 * scale, 34.4 / 1.2 * scale, 4 * scale, 12 * scale);
+
+    // Arm Left
+    // Arm Left - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 36 * scale, 52 * scale, 4 * scale, 12 * scale, 16 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Arm Left - Top
+    model_ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
+    model_ctx.drawImage(skin_canvas, 36 * scale, 48 * scale, 4 * scale, 4 * scale, 0, 16 * scale, 4 * scale, 4 * scale);
+
+    // Body
+    // Body - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 20 * scale, 20 * scale, 8 * scale, 12 * scale, 8 * scale, 20 / 1.2 * scale, 8 * scale, 12 * scale);
+
+    // Arm Right
+    // Arm Right - Right
+    model_ctx.setTransform(1, 0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 40 * scale, 20 * scale, 4 * scale, 12 * scale, 0, 16 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Arm Right - Front
+    model_ctx.setTransform(1, -0.5, 0, 1.2, 0, 0);
+    model_ctx.drawImage(skin_canvas, 44 * scale, 20 * scale, 4 * scale, 12 * scale, 4 * scale, 20 / 1.2 * scale, 4 * scale, 12 * scale);
+    // Arm Right - Top
+    model_ctx.setTransform(-1, 0.5, 1, 0.5, 0, 0);
+    model_ctx.scale(-1, 1);
+    model_ctx.drawImage(skin_canvas, 44 * scale, 16 * scale, 4 * scale, 4 * scale, -16 * scale, 16 * scale, 4 * scale, 4 * scale);
   }
 };
 
@@ -146,9 +162,9 @@ exp.draw_model = function(rid, img, scale, helm, body, callback) {
     var model_ctx = model_canvas.getContext("2d");
     var skin_ctx = skin_canvas.getContext("2d");
 
-    skin_ctx.drawImage(image,0,0,64,original_height);
-    //Scale it
-    scale_image(skin_ctx.getImageData(0,0,64,original_height), skin_ctx, 0, 0, scale);
+    skin_ctx.drawImage(image, 0, 0, 64, original_height);
+    // Scale it
+    scale_image(skin_ctx.getImageData(0, 0, 64, original_height), skin_ctx, 0, 0, scale);
     if (body) {
       exp.draw_body(rid, skin_canvas, model_ctx, scale);
     }
@@ -179,20 +195,5 @@ exp.open_render = function(rid, renderpath, callback) {
   });
 };
 
-// scales an image from the +imagedata+ onto the +context+
-// scaled by a factor of +scale+ with options +d_x+ and +d_y+
-function scale_image(imageData, context, d_x, d_y, scale) {
-  var width = imageData.width;
-  var height = imageData.height;
-  context.clearRect(0,0,width,height); //Clear the spot where it originated from
-  for(var y = 0; y < height; y++) { // height original
-    for(var x = 0; x < width; x++) { // width original
-      //Gets original colour, then makes a scaled square of the same colour
-      var index = (x + y * width) * 4;
-      context.fillStyle = "rgba(" + imageData.data[index+0] + "," + imageData.data[index+1] + "," + imageData.data[index+2] + "," + imageData.data[index+3] + ")";
-      context.fillRect(d_x + x*scale, d_y + y*scale, scale, scale);
-    }
-  }
-}
 
-module.exports = exp;
+module.exports = exp;

+ 26 - 22
lib/skins.js

@@ -14,9 +14,9 @@ exp.extract_face = function(buffer, outname, callback) {
     } else {
       image.batch()
       .crop(8, 8, 15, 15) // face
-      .writeFile(outname, function(err) {
-        if (err) {
-          callback(err);
+      .writeFile(outname, function(write_err) {
+        if (write_err) {
+          callback(write_err);
         } else {
           callback(null);
         }
@@ -34,23 +34,25 @@ exp.extract_helm = function(rid, facefile, buffer, outname, callback) {
     if (err) {
       callback(err);
     } else {
-      lwip.open(facefile, function(err, face_img) {
-        if (err) {
-          callback(err);
+      lwip.open(facefile, function(open_err, face_img) {
+        if (open_err) {
+          callback(open_err);
         } else {
-          face_img.toBuffer("png", { compression: "none" }, function(err, face_buffer) {
-            skin_img.crop(40, 8, 47, 15, function(err, helm_img) {
-              if (err) {
-                callback(err);
+          face_img.toBuffer("png", { compression: "none" }, function(buf_err, face_buffer) {
+            // FIXME: buf_err is not handled
+            skin_img.crop(40, 8, 47, 15, function(crop_err, helm_img) {
+              if (crop_err) {
+                callback(crop_err);
               } else {
-                face_img.paste(0, 0, helm_img, function(err, face_helm_img) {
-                  if (err) {
-                    callback(err);
+                face_img.paste(0, 0, helm_img, function(img_err, face_helm_img) {
+                  if (img_err) {
+                    callback(img_err);
                   } else {
-                    face_helm_img.toBuffer("png", {compression: "none"}, function(err, face_helm_buffer) {
+                    face_helm_img.toBuffer("png", {compression: "none"}, function(buf_err2, face_helm_buffer) {
+                      // FIXME: buf_err2 is not handled
                       if (face_helm_buffer.toString() !== face_buffer.toString()) {
-                        face_helm_img.writeFile(outname, function(err) {
-                          callback(err);
+                        face_helm_img.writeFile(outname, function(write_err) {
+                          callback(write_err);
                         });
                       } else {
                         logging.log(rid, "helm img == face img, not storing!");
@@ -77,7 +79,8 @@ exp.resize_img = function(inname, size, callback) {
     } else {
       image.batch()
       .resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
-      .toBuffer("png", function(err, buffer) {
+      .toBuffer("png", function(buf_err, buffer) {
+        // FIXME: buf_err is not handled
         callback(null, buffer);
       });
     }
@@ -96,11 +99,11 @@ exp.default_skin = function(uuid) {
     // that can be compacted to counting the LSBs of every 4th byte in the UUID
     // an odd sum means alex, an even sum means steve
     // XOR-ing all the LSBs gives us 1 for alex and 0 for steve
-    var lsbs_even = parseInt(uuid[07], 16) ^
+    var lsbs_even = parseInt(uuid[ 7], 16) ^
                     parseInt(uuid[15], 16) ^
                     parseInt(uuid[23], 16) ^
                     parseInt(uuid[31], 16);
-      return lsbs_even ? "alex" : "steve";
+    return lsbs_even ? "alex" : "steve";
   }
 };
 
@@ -120,14 +123,15 @@ exp.open_skin = function(rid, skinpath, callback) {
 // write the image +buffer+ to the +outpath+ file
 // callback: error
 exp.save_image = function(buffer, outpath, callback) {
+  logging.error("outpath:" + outpath);
   lwip.open(buffer, "png", function(err, image) {
     if (err) {
       callback(err);
     } else {
       image.batch()
-      .writeFile(outpath, function(err) {
-        if (err) {
-          callback(err);
+      .writeFile(outpath, function(write_err) {
+        if (write_err) {
+          callback(write_err);
         } else {
           callback(null);
         }

+ 1 - 1
lib/www.js

@@ -1,4 +1,4 @@
-var logging = require ("../lib/logging");
+var logging = require("../lib/logging");
 var cleaner = require("../lib/cleaner");
 var config = require("../lib/config");
 var cluster = require("cluster");

+ 15 - 15
routes/avatars.js

@@ -22,6 +22,20 @@ module.exports = function(req, res) {
   var etag = null;
   var rid = req.id;
 
+  function sendimage(rid, http_status, img_status, image) {
+    logging.log(rid, "status:", http_status);
+    res.writeHead(http_status, {
+      "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-Request-ID": rid,
+      "Access-Control-Allow-Origin": "*",
+      "Etag": '"' + etag + '"'
+    });
+    res.end(http_status === 304 ? null : image);
+  }
+
   // Prevent app from crashing/freezing
   if (size < config.min_size || size > config.max_size) {
     // "Unprocessable Entity", valid request, but semantically erroneous:
@@ -96,18 +110,4 @@ module.exports = function(req, res) {
       });
     }
   }
-
-  function sendimage(rid, http_status, img_status, image) {
-    logging.log(rid, "status:", http_status);
-    res.writeHead(http_status, {
-      "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-Request-ID": rid,
-      "Access-Control-Allow-Origin": "*",
-      "Etag": '"' + etag + '"'
-    });
-    res.end(http_status === 304 ? null : image);
-  }
-};
+};

+ 14 - 14
routes/capes.js

@@ -18,6 +18,19 @@ module.exports = function(req, res) {
   var etag = null;
   var rid = req.id;
 
+  function sendimage(rid, http_status, img_status, image) {
+    res.writeHead(http_status, {
+      "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-Request-ID": rid,
+      "Access-Control-Allow-Origin": "*",
+      "Etag": '"' + etag + '"'
+    });
+    res.end(http_status === 304 ? null : image);
+  }
+
   if (!helpers.id_valid(userId)) {
     res.writeHead(422, {
       "Content-Type": "text/plain",
@@ -70,17 +83,4 @@ module.exports = function(req, res) {
     });
     res.end("500 server error");
   }
-
-  function sendimage(rid, http_status, img_status, image) {
-    res.writeHead(http_status, {
-      "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-Request-ID": rid,
-      "Access-Control-Allow-Origin": "*",
-      "Etag": '"' + etag + '"'
-    });
-    res.end(http_status === 304 ? null : image);
-  }
-};
+};

+ 2 - 1
routes/index.js

@@ -1,8 +1,9 @@
 var config = require("../lib/config");
+var path = require("path");
 var jade = require("jade");
 
 // compile jade
-var index = jade.compileFile(__dirname + "/../views/index.jade");
+var index = jade.compileFile(path.join(__dirname, "../views/index.jade"));
 
 module.exports = function(req, res) {
   var html = index({

+ 46 - 47
routes/renders.js

@@ -41,6 +41,52 @@ module.exports = function(req, res) {
   var helm = req.url.query.hasOwnProperty("helm");
   var etag = null;
 
+  function sendimage(rid, http_status, img_status, image) {
+    logging.log(rid, "status:", http_status);
+    res.writeHead(http_status, {
+      "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-Request-ID": rid,
+      "Access-Control-Allow-Origin": "*",
+      "Etag": '"' + etag + '"'
+    });
+    res.end(http_status === 304 ? null : image);
+  }
+
+  // default alex/steve images can be rendered, but
+  // custom images will not be
+  function handle_default(rid, http_status, img_status, userId) {
+    if (def && def !== "steve" && def !== "alex") {
+      logging.log(rid, "status: 301");
+      res.writeHead(301, {
+        "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
+        "Response-Time": new Date() - start,
+        "X-Storage-Type": human_status[img_status],
+        "X-Request-ID": rid,
+        "Access-Control-Allow-Origin": "*",
+        "Location": def
+      });
+      res.end();
+    } else {
+      def = def || skins.default_skin(userId);
+      fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
+        if (err) {
+          // errored while loading the default image, continuing with null image
+          logging.error(rid, "error loading default render image:", err);
+        }
+        // we render the default skins, but not custom images
+        renders.draw_model(rid, buf, scale, helm, body, function(render_err, def_img) {
+          if (render_err) {
+            logging.error(rid, "error while rendering default image:", render_err);
+          }
+          sendimage(rid, http_status, img_status, def_img);
+        });
+      });
+    }
+  }
+
   if (scale < config.min_scale || scale > config.max_scale) {
     res.writeHead(422, {
       "Content-Type": "text/plain",
@@ -92,51 +138,4 @@ module.exports = function(req, res) {
     logging.error(rid, "error:", e.stack);
     handle_default(rid, 500, -1, userId);
   }
-
-
-  // default alex/steve images can be rendered, but
-  // custom images will not be
-  function handle_default(rid, http_status, img_status, userId) {
-    if (def && def !== "steve" && def !== "alex") {
-      logging.log(rid, "status: 301");
-      res.writeHead(301, {
-        "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
-        "Response-Time": new Date() - start,
-        "X-Storage-Type": human_status[img_status],
-        "X-Request-ID": rid,
-        "Access-Control-Allow-Origin": "*",
-        "Location": def
-      });
-      res.end();
-    } else {
-      def = def || skins.default_skin(userId);
-      fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
-        if (err) {
-          // errored while loading the default image, continuing with null image
-          logging.error(rid, "error loading default render image:", err);
-        }
-        // we render the default skins, but not custom images
-        renders.draw_model(rid, buf, scale, helm, body, function(err, def_img) {
-          if (err) {
-            logging.error(rid, "error while rendering default image:", err);
-          }
-          sendimage(rid, http_status, img_status, def_img);
-        });
-      });
-    }
-  }
-
-  function sendimage(rid, http_status, img_status, image) {
-    logging.log(rid, "status:", http_status);
-    res.writeHead(http_status, {
-      "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-Request-ID": rid,
-      "Access-Control-Allow-Origin": "*",
-      "Etag": '"' + etag + '"'
-    });
-    res.end(http_status === 304 ? null : image);
-  }
 };

+ 38 - 36
routes/skins.js

@@ -12,6 +12,44 @@ module.exports = function(req, res) {
   var etag = null;
   var rid = req.id;
 
+  function sendimage(rid, http_status, image) {
+    logging.log(rid, "status:", http_status);
+    res.writeHead(http_status, {
+      "Content-Type": "image/png",
+      "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
+      "Response-Time": new Date() - start,
+      "X-Storage-Type": "downloaded",
+      "X-Request-ID": rid,
+      "Access-Control-Allow-Origin": "*",
+      "Etag": '"' + etag + '"'
+    });
+    res.end(http_status === 304 ? null : image);
+  }
+
+  function handle_default(rid, http_status, userId) {
+    if (def && def !== "steve" && def !== "alex") {
+      logging.log(rid, "status: 301");
+      res.writeHead(301, {
+        "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
+        "Response-Time": new Date() - start,
+        "X-Storage-Type": "downloaded",
+        "X-Request-ID": rid,
+        "Access-Control-Allow-Origin": "*",
+        "Location": def
+      });
+      res.end();
+    } else {
+      def = def || skins.default_skin(userId);
+      lwip.open("public/images/" + def + "_skin.png", function(err, image) {
+        // FIXME: err is not handled
+        image.toBuffer("png", function(buf_err, buffer) {
+          // FIXME: buf_err is not handled
+          sendimage(rid, http_status, buffer);
+        });
+      });
+    }
+  }
+
   if (!helpers.id_valid(userId)) {
     res.writeHead(422, {
       "Content-Type": "text/plain",
@@ -50,40 +88,4 @@ module.exports = function(req, res) {
     logging.error(rid, "error:", e.stack);
     handle_default(rid, 500, userId);
   }
-
-  function handle_default(rid, http_status, userId) {
-    if (def && def !== "steve" && def !== "alex") {
-      logging.log(rid, "status: 301");
-      res.writeHead(301, {
-        "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
-        "Response-Time": new Date() - start,
-        "X-Storage-Type": "downloaded",
-        "X-Request-ID": rid,
-        "Access-Control-Allow-Origin": "*",
-        "Location": def
-      });
-      res.end();
-    } else {
-      def = def || skins.default_skin(userId);
-      lwip.open("public/images/" + def + "_skin.png", function(err, image) {
-        image.toBuffer("png", function(err, buffer) {
-          sendimage(rid, http_status, buffer);
-        });
-      });
-    }
-  }
-
-  function sendimage(rid, http_status, image) {
-    logging.log(rid, "status:", http_status);
-    res.writeHead(http_status, {
-      "Content-Type": "image/png",
-      "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
-      "Response-Time": new Date() - start,
-      "X-Storage-Type": "downloaded",
-      "X-Request-ID": rid,
-      "Access-Control-Allow-Origin": "*",
-      "Etag": '"' + etag + '"'
-    });
-    res.end(http_status === 304 ? null : image);
-  }
 };

+ 3 - 3
server.js

@@ -21,11 +21,11 @@ function asset_request(req, res) {
   var filename = path.join(__dirname, "public", req.url.path_list.join("/"));
   fs.exists(filename, function(exists) {
     if (exists) {
-      res.writeHead(200, { "Content-type" : mime.lookup(filename) });
+      res.writeHead(200, { "Content-type": mime.lookup(filename) });
       fs.createReadStream(filename).pipe(res);
     } else {
       res.writeHead(404, {
-        "Content-type" : "text/plain"
+        "Content-type": "text/plain"
       });
       res.end("Not Found");
     }
@@ -46,7 +46,7 @@ function requestHandler(req, res) {
   request.url.path_list = path_list;
 
   // generate 12 character random string
-  request.id = Math.random().toString(36).substring(2,14);
+  request.id = Math.random().toString(36).substring(2, 14);
 
   var local_path = request.url.path_list[1];
   logging.log(request.id + request.method, request.url.href);

+ 16 - 17
test/test.js

@@ -6,7 +6,6 @@ var logging = require("../lib/logging");
 var config = require("../lib/config");
 var skins = require("../lib/skins");
 var cache = require("../lib/cache");
-var renders = require("../lib/renders");
 var server = require("../server");
 var cleaner = require("../lib/cleaner");
 var request = require("request");
@@ -29,25 +28,25 @@ var name = names[Math.round(Math.random() * (names.length - 1))];
 
 // Let's hope these will never be assigned
 var steve_ids = [
-  "fffffff0"+"fffffff0"+"fffffff0"+"fffffff0",
-  "fffffff0"+"fffffff0"+"fffffff1"+"fffffff1",
-  "fffffff0"+"fffffff1"+"fffffff0"+"fffffff1",
-  "fffffff0"+"fffffff1"+"fffffff1"+"fffffff0",
-  "fffffff1"+"fffffff0"+"fffffff0"+"fffffff1",
-  "fffffff1"+"fffffff0"+"fffffff1"+"fffffff0",
-  "fffffff1"+"fffffff1"+"fffffff0"+"fffffff0",
-  "fffffff1"+"fffffff1"+"fffffff1"+"fffffff1",
+  "fffffff0" + "fffffff0" + "fffffff0" + "fffffff0",
+  "fffffff0" + "fffffff0" + "fffffff1" + "fffffff1",
+  "fffffff0" + "fffffff1" + "fffffff0" + "fffffff1",
+  "fffffff0" + "fffffff1" + "fffffff1" + "fffffff0",
+  "fffffff1" + "fffffff0" + "fffffff0" + "fffffff1",
+  "fffffff1" + "fffffff0" + "fffffff1" + "fffffff0",
+  "fffffff1" + "fffffff1" + "fffffff0" + "fffffff0",
+  "fffffff1" + "fffffff1" + "fffffff1" + "fffffff1",
 ];
 // Let's hope these will never be assigned
 var alex_ids = [
-  "fffffff0"+"fffffff0"+"fffffff0"+"fffffff1",
-  "fffffff0"+"fffffff0"+"fffffff1"+"fffffff0",
-  "fffffff0"+"fffffff1"+"fffffff0"+"fffffff0",
-  "fffffff0"+"fffffff1"+"fffffff1"+"fffffff1",
-  "fffffff1"+"fffffff0"+"fffffff0"+"fffffff0",
-  "fffffff1"+"fffffff0"+"fffffff1"+"fffffff1",
-  "fffffff1"+"fffffff1"+"fffffff0"+"fffffff1",
-  "fffffff1"+"fffffff1"+"fffffff1"+"fffffff0",
+  "fffffff0" + "fffffff0" + "fffffff0" + "fffffff1",
+  "fffffff0" + "fffffff0" + "fffffff1" + "fffffff0",
+  "fffffff0" + "fffffff1" + "fffffff0" + "fffffff0",
+  "fffffff0" + "fffffff1" + "fffffff1" + "fffffff1",
+  "fffffff1" + "fffffff0" + "fffffff0" + "fffffff0",
+  "fffffff1" + "fffffff0" + "fffffff1" + "fffffff1",
+  "fffffff1" + "fffffff1" + "fffffff0" + "fffffff1",
+  "fffffff1" + "fffffff1" + "fffffff1" + "fffffff0",
 ];
 
 var rid = "TestReqID: ";