Răsfoiți Sursa

Merge pull request #26 from Jake0oo0/render

3d Render Support :tada:
Jake 10 ani în urmă
părinte
comite
1b4069bade
16 a modificat fișierele cu 514 adăugiri și 35 ștergeri
  1. 2 0
      .buildpacks
  2. 2 0
      .travis.yml
  3. 2 1
      README.md
  4. 8 7
      app.js
  5. 13 3
      modules/clean_images.js
  6. 17 12
      modules/config.example.js
  7. 53 9
      modules/helpers.js
  8. 21 0
      modules/networking.js
  9. 202 0
      modules/renders.js
  10. 11 0
      modules/skins.js
  11. 1 0
      package.json
  12. 124 0
      routes/renders.js
  13. 0 0
      skins/renders/.gitkeep
  14. 0 0
      skins/skins/.gitkeep
  15. 20 1
      test/test.js
  16. 38 2
      views/index.jade

+ 2 - 0
.buildpacks

@@ -0,0 +1,2 @@
+https://github.com/mojodna/heroku-buildpack-cairo.git
+https://github.com/heroku/heroku-buildpack-nodejs.git

+ 2 - 0
.travis.yml

@@ -3,6 +3,8 @@ node_js:
   - "0.10"
   - "0.10"
 before_script:
 before_script:
   - cp "modules/config.example.js" "modules/config.js"
   - cp "modules/config.example.js" "modules/config.js"
+before_install: 
+  - sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++
 notifications:
 notifications:
   irc:
   irc:
     channels:
     channels:

+ 2 - 1
README.md

@@ -5,7 +5,7 @@ https://crafatar.com
 Crafatar serves Minecraft avatars based on the skin for use in external applications.
 Crafatar serves Minecraft avatars based on the skin for use in external applications.
 Inspired by [Gravatar](https://gravatar.com) (hence the name) and [Minotar](https://minotar.net).
 Inspired by [Gravatar](https://gravatar.com) (hence the name) and [Minotar](https://minotar.net).
 
 
-Image manipulation is done by [lwip](https://github.com/EyalAr/lwip)
+Image manipulation is done by [lwip](https://github.com/EyalAr/lwip). Renders are created with [node-canvas](https://github.com/Automattic/node-canvas), based on math by [confuser](https://github.com/confuser/serverless-mc-skin-viewer).
 
 
 ![redstone_sheep's avatar](https://crafatar.com/avatars/ae795aa86327408e92ab25c8a59f3ba1?size=128) ![Jake0oo0's avatar](https://crafatar.com/avatars/2d5aa9cdaeb049189930461fc9b91cc5?size=128) ![Notch's avatar](https://crafatar.com/avatars/069a79f444e94726a5befca90e38aaf5?size=128) ![sk89q's avatar](https://crafatar.com/avatars/0ea8eca3dbf647cc9d1ac64551ca975c?size=128) ![md_5's avatar](https://crafatar.com/avatars/af74a02d19cb445bb07f6866a861f783?size=128) 
 ![redstone_sheep's avatar](https://crafatar.com/avatars/ae795aa86327408e92ab25c8a59f3ba1?size=128) ![Jake0oo0's avatar](https://crafatar.com/avatars/2d5aa9cdaeb049189930461fc9b91cc5?size=128) ![Notch's avatar](https://crafatar.com/avatars/069a79f444e94726a5befca90e38aaf5?size=128) ![sk89q's avatar](https://crafatar.com/avatars/0ea8eca3dbf647cc9d1ac64551ca975c?size=128) ![md_5's avatar](https://crafatar.com/avatars/af74a02d19cb445bb07f6866a861f783?size=128) 
 ## Usage / Documentation
 ## Usage / Documentation
@@ -20,6 +20,7 @@ Please [visit the website](https://crafatar.com) for details.
 ## Install
 ## Install
 
 
 * Clone the repository
 * Clone the repository
+* Install [node-canvas](https://github.com/Automattic/node-canvas/wiki#desktop) dependencies.
 * `npm install`
 * `npm install`
 * `redis-server`
 * `redis-server`
 * `cp "modules/config.example.js" "modules/config.js"`
 * `cp "modules/config.example.js" "modules/config.js"`

+ 8 - 7
app.js

@@ -4,9 +4,10 @@ var logger = require("morgan");
 var cookieParser = require("cookie-parser");
 var cookieParser = require("cookie-parser");
 var bodyParser = require("body-parser");
 var bodyParser = require("body-parser");
 
 
-var routes = require("./routes/index");
-var avatars = require("./routes/avatars");
-var skins = require("./routes/skins")
+var routes = require('./routes/index');
+var avatars = require('./routes/avatars');
+var skins = require('./routes/skins');
+var renders = require('./routes/renders');
 
 
 var app = express();
 var app = express();
 
 
@@ -20,10 +21,10 @@ app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, "public")));
 app.use(express.static(path.join(__dirname, "public")));
 
 
-app.use("/", routes);
-app.use("/avatars", avatars);
-app.use("/skins", skins)
-
+app.use('/', routes);
+app.use('/avatars', avatars);
+app.use('/skins', skins);
+app.use('/renders', renders);
 
 
 // catch 404 and forward to error handler
 // catch 404 and forward to error handler
 app.use(function(req, res, next) {
 app.use(function(req, res, next) {

+ 13 - 3
modules/clean_images.js

@@ -33,14 +33,24 @@ exp.run = function() {
       logging.error(err);
       logging.error(err);
     } else if (clean) {
     } else if (clean) {
       logging.warn("ImageCleaner: Disk limit reached! Cleaning images now");
       logging.warn("ImageCleaner: Disk limit reached! Cleaning images now");
-      var skindir = __dirname + "/../" + config.faces_dir;
+      var facesdir = __dirname + "/../" + config.faces_dir;
       var helmdir = __dirname + "/../" + config.helms_dir;
       var helmdir = __dirname + "/../" + config.helms_dir;
-      var files = fs.readdirSync(skindir);
+      var renderdir = __dirname + "/../" + config.renders_dir;
+      var skindir = __dirname + "/../" + config.skins_dir;
+      var files = fs.readdirSync(facesdir);
       for (var i = 0; i < Math.min(files.length, config.cleaning_amount); i++) {
       for (var i = 0; i < Math.min(files.length, config.cleaning_amount); i++) {
         var filename = files[i];
         var filename = files[i];
         if (filename[0] != ".") {
         if (filename[0] != ".") {
-          fs.unlink(skindir + filename, function(){});
+          fs.unlink(facesdir + filename, function(){});
           fs.unlink(helmdir + filename, function(){});
           fs.unlink(helmdir + filename, function(){});
+          fs.unlink(skindir + filename, function(){});
+        }
+      }
+      files = fs.readdirSync(renderdir);
+      for (var j = 0; j < Math.min(files.length, config.cleaning_amount); j++) {
+        var filename = files[j];
+        if (filename[0] != ".") {
+          fs.unlink(renderdir + filename, function(){});
         }
         }
       }
       }
     } else {
     } else {

+ 17 - 12
modules/config.example.js

@@ -1,16 +1,21 @@
 var config = {
 var config = {
-  min_size: 1,               // < 1 will (obviously) cause crash
-  max_size: 512,             // too big values might lead to slow response time or DoS
-  default_size: 160,         // size to be used when no size given
-  local_cache_time: 1200,    // seconds until we will check if the image changed. should be > 60 to prevent mojang 429 response
-  browser_cache_time: 3600,  // seconds until browser will request image again
-  cleaning_interval: 1800,   // seconds interval: deleting images if disk size at limit
-  cleaning_limit: 10240,     // minumum required available KB on disk to trigger cleaning
-  cleaning_amount: 50000,    // amount of avatar (and their helm) files to clean
-  http_timeout: 1000,        // ms until connection to mojang is dropped
-  faces_dir: "skins/faces/", // directory where faces are kept. should have trailing "/"
-  helms_dir: "skins/helms/", // directory where helms are kept. should have trailing "/"
-  debug_enabled: false       // enables logging.debug
+  min_size: 1,                   // for avatars
+  max_size: 512,                 // for avatars; too big values might lead to slow response time or DoS
+  default_size: 160,             // for avatars; size to be used when no size given
+  local_cache_time: 1200,        // seconds until we will check if the image changed. should be > 60 to prevent mojang 429 response
+  browser_cache_time: 3600,      // seconds until browser will request image again
+  cleaning_interval: 1800,       // seconds interval: deleting images if disk size at limit
+  cleaning_limit: 10240,         // minumum required available KB on disk to trigger cleaning
+  cleaning_amount: 50000,        // amount of avatar (and their helm) files to clean
+  http_timeout: 1000,            // ms until connection to mojang is dropped
+  faces_dir: 'skins/faces/',     // directory where faces are kept. should have trailing '/'
+  helms_dir: 'skins/helms/',     // directory where helms are kept. should have trailing '/'
+  skins_dir: 'skins/skins/',     // directory where skins are kept. should have trailing '/'
+  renders_dir: 'skins/renders/', // Directory where rendered skins are kept. should have trailing '/'
+  debug_enabled: false,          // enables logging.debug
+  min_scale: 1,                  // for renders
+  max_scale: 10,                 // for renders; too big values might lead to slow response time or DoS
+  default_scale: 6,              // for renders; scale to be used when no scale given
 };
 };
 
 
 module.exports = config;
 module.exports = config;

+ 53 - 9
modules/helpers.js

@@ -3,6 +3,7 @@ var logging = require("./logging");
 var config = require("./config");
 var config = require("./config");
 var cache = require("./cache");
 var cache = require("./cache");
 var skins = require("./skins");
 var skins = require("./skins");
+var renders = require("./renders");
 var fs = require("fs");
 var fs = require("fs");
 
 
 // 0098cb60-fa8e-427c-b299-793cbd302c9a
 // 0098cb60-fa8e-427c-b299-793cbd302c9a
@@ -160,19 +161,62 @@ exp.get_avatar = function(uuid, helm, size, callback) {
 exp.get_skin = function(uuid, callback) {
 exp.get_skin = function(uuid, callback) {
   logging.log(uuid + " skin request");
   logging.log(uuid + " skin request");
   exp.get_image_hash(uuid, function(err, status, hash) {
   exp.get_image_hash(uuid, function(err, status, hash) {
-    if (hash) {
-      var skinurl = "http://textures.minecraft.net/texture/" + hash;
-      networking.get_skin(skinurl, function(err, img) {
+    var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png";
+    if (fs.existsSync(skinpath)) {
+      logging.log("skin already exists, not downloading");
+      skins.open_skin(skinpath, function(err, img) {
+        callback(err, hash, img);
+      });
+      return;
+    }
+    networking.save_skin(uuid, hash, skinpath, function(err, img) {
+      callback(err, hash, img);
+    });
+  });
+};
+
+function get_type(helm, body) {
+  var text = body ? "body" : "head";
+  return helm ? text+"helm" : text;
+}
+
+// handles creations of skin renders
+// callback contanis error, hash, image buffer
+exp.get_render = function(uuid, scale, helm, body, callback) {
+  logging.log(uuid + " render request");
+  exp.get_image_hash(uuid, function(err, status, hash) {
+    exp.get_skin(uuid, function(err, hash, img) {
+      if (!hash) {
+        callback(err, -1, hash, null);
+        return;
+      }
+      logging.debug("TYPE: " + get_type(helm, body));
+      var renderpath = __dirname + "/../" + config.renders_dir + hash + "-" + scale + "-" + get_type(helm, body) + ".png";
+      if (fs.existsSync(renderpath)) {
+        renders.open_render(renderpath, function(err, img) {
+          callback(err, 1, hash, img);
+        });
+        return;
+      }
+      if (!img) {
+        callback(err, 0, hash, null);
+        return;
+      }
+      renders.draw_model(uuid, img, scale, helm, body, function(err, img) {
         if (err) {
         if (err) {
-          logging.error("error while downloading skin");
-          callback(err, hash, null);
+          callback(err, -1, hash, null);
+        } else if (!img) {
+          callback(null, 0, hash, null);
         } else {
         } else {
-          callback(null, hash, img);
+          fs.writeFile(renderpath, img, 'binary', function(err){
+            if (err) {
+              logging.log(err);
+            }
+            callback(null, 2, hash, img);
+          });
         }
         }
       });
       });
-    } else {
-      callback(err, null, null);
-    }
+    });
   });
   });
 };
 };
 
 

+ 21 - 0
modules/networking.js

@@ -141,4 +141,25 @@ exp.get_skin = function(url, callback) {
   });
   });
 };
 };
 
 
+exp.save_skin = function(uuid, hash, outpath, callback) {
+  if (hash) {
+    var skinurl = "http://textures.minecraft.net/texture/" + hash;
+    exp.get_skin(skinurl, function(err, img) {
+      if (err) {
+        logging.error("error while downloading skin");
+        callback(err, null);
+      } else {
+        fs.writeFile(outpath, img, 'binary', function(err){
+          if (err) {
+            logging.log(err);
+          }
+          callback(null, img);
+        });
+      }
+    });
+  } else {
+    callback(null, null);
+  }
+};
+
 module.exports = exp;
 module.exports = exp;

+ 202 - 0
modules/renders.js

@@ -0,0 +1,202 @@
+// Skin locations are based on the work of Confuser, with 1.8 updates by Jake0oo0
+// https://github.com/confuser/serverless-mc-skin-viewer
+// Permission to use & distribute https://github.com/confuser/serverless-mc-skin-viewer/blob/master/LICENSE
+
+var helpers = require('./helpers');
+var logging = require('./logging');
+var fs = require('fs');
+var Canvas = require('canvas');
+var Image = Canvas.Image;
+var exp = {};
+
+// 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);
+};
+
+// 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);
+};
+
+// draws the body on to the +skin_canvas+
+// using the skin from the +model_ctx+ at the +scale+
+// parts are labeled as if drawn from the skin's POV
+exp.draw_body = function(skin_canvas, model_ctx, scale) {
+  if (skin_canvas.height == 32 * scale) {
+    logging.log("old skin");
+    //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.log("new skin");
+    //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);
+  }
+};
+
+// sets up the necessary components to draw the skin model
+// uses the +img+ skin from the +uuid+ with options of drawing
+// the +helm+ and the +body+
+// callback contains error, image buffer
+exp.draw_model = function(uuid, img, scale, helm, body, callback) {
+  var image = new Image();
+
+  image.onerror = function(err) {
+    logging.error("render error: " + err);
+    callback(err, null);
+  };
+
+  image.onload = function() {
+    var width = 64 * scale;
+    var original_height = (image.height == 32 ? 32 : 64);
+    var height = original_height * scale;
+    var model_canvas = new Canvas(20 * scale, (body ? 44.8 : 17.6) * scale);
+    var skin_canvas = new Canvas(width, height);
+    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);
+    if (body) {
+      logging.log("drawing body");
+      exp.draw_body(skin_canvas, model_ctx, scale);
+    }
+    logging.log("drawing head");
+    exp.draw_head(skin_canvas, model_ctx, scale);
+    if (helm) {
+      logging.log("drawing helmet");
+      exp.draw_helmet(skin_canvas, model_ctx, scale);
+    }
+
+    model_canvas.toBuffer(function(err, buf){
+      if (err) {
+        logging.log("error creating buffer: " + err);
+      }
+      callback(err, buf);
+    });
+  };
+
+  image.src = img;
+};
+
+// helper method to open a render from +renderpath+
+// callback contains error, image buffer
+exp.open_render = function(renderpath, callback) {
+  fs.readFile(renderpath, function (err, buf) {
+    if (err) {
+      logging.error("error while opening skin file: " + err);
+    }
+    callback(err, buf);
+  });
+};
+
+// 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(y=0; y<height; y++) { //height original
+    for(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;

+ 11 - 0
modules/skins.js

@@ -93,4 +93,15 @@ exp.default_skin = function(uuid) {
   }
   }
 };
 };
 
 
+// helper method for opening a skin file from +skinpath+
+// callback contains error, image buffer
+exp.open_skin = function(skinpath, callback) {
+  fs.readFile(skinpath, function (err, buf) {
+    if (err) {
+      logging.error("error while opening skin file: " + err);
+    }
+    callback(err, buf);
+  });
+};
+
 module.exports = exp;
 module.exports = exp;

+ 1 - 0
package.json

@@ -26,6 +26,7 @@
   },
   },
   "dependencies": {
   "dependencies": {
     "body-parser": "~1.8.1",
     "body-parser": "~1.8.1",
+    "canvas": "1.0.1",
     "cookie-parser": "~1.3.3",
     "cookie-parser": "~1.3.3",
     "coveralls": "^2.11.2",
     "coveralls": "^2.11.2",
     "debug": "~2.0.0",
     "debug": "~2.0.0",

+ 124 - 0
routes/renders.js

@@ -0,0 +1,124 @@
+var router = require('express').Router();
+var logging = require('../modules/logging');
+var helpers = require('../modules/helpers');
+var config = require('../modules/config');
+var skins = require('../modules/skins');
+var renders = require('../modules/renders');
+var fs = require('fs');
+
+var human_status = {
+  0: "none",
+  1: "cached",
+  2: "downloaded",
+  3: "checked",
+  "-1": "error"
+};
+
+// valid types: head, body. helmet is query param
+
+// The Type logic should be two separate GET
+// functions once response methods are extracted
+router.get('/:type/:uuid.:ext?', function(req, res) {
+  var raw_type = req.params.type;
+
+  // Check valid type for now
+  if (raw_type != "body" && raw_type != "head") {
+    res.status(404).send("404 Invalid Render Type");
+    return;
+  }
+
+  var body = raw_type == "body";
+  var uuid = req.params.uuid;
+  var def = req.query.default;
+  var scale = parseInt(req.query.scale) || config.default_scale;
+  var helm = req.query.hasOwnProperty('helm');
+  var start = new Date();
+  var etag = null;
+
+  if (scale < config.min_scale || scale > config.max_scale) {
+    // Preventing from OOM crashes.
+    res.status(422).send("422 Invalid Scale");
+    return;
+  } else if (!helpers.uuid_valid(uuid)) {
+    res.status(422).send("422 Invalid UUID");
+    return;
+  }
+
+  // strip dashes
+  uuid = uuid.replace(/-/g, "");
+
+  try {
+    helpers.get_render(uuid, scale, helm, body, function(err, status, hash, image) {
+      logging.log(uuid + " - " + human_status[status]);
+      if (err) {
+        logging.error(err);
+      }
+      etag = hash && hash.substr(0, 32) || "none";
+      var matches = req.get("If-None-Match") == '"' + etag + '"';
+      if (image) {
+        var http_status = 200;
+        if (matches) {
+          http_status = 304;
+        } else if (err) {
+          http_status = 503;
+        }
+        logging.log("matches: " + matches);
+        logging.log("Etag: " + req.get("If-None-Match"));
+        logging.log("status: " + http_status);
+        sendimage(http_status, status, image);
+      } else {
+        logging.log("image not found, using default.");
+        handle_default(404, status);
+      }
+    });
+  } catch(e) {
+    logging.error("Error!");
+    logging.error(e);
+    handle_default(500, status);
+  }
+
+
+  // default alex/steve images can be rendered, but
+  // custom images will not be
+  function handle_default(http_status, img_status) {
+    if (def && def != "steve" && def != "alex") {
+      res.writeHead(301, {
+        'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
+        'Response-Time': new Date() - start,
+        'X-Storage-Type': human_status[img_status],
+        'Access-Control-Allow-Origin': '*',
+        'Location': def
+      });
+      res.end();
+    } else {
+      def = def || skins.default_skin(uuid);
+      fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
+        if (err) {
+          // errored while loading the default image, continuing with null image
+          logging.error("error loading default render image: " + err);
+        }
+        // we render the default skins, but not custom images
+        renders.draw_model(uuid, buf, scale, helm, body, function(err, def_img) {
+          if (err) {
+            logging.log("error while rendering default image: " + err);
+          }
+          sendimage(http_status, img_status, def_img);
+        });
+      });
+    }
+  }
+
+  function sendimage(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],
+      'Access-Control-Allow-Origin': '*',
+      'Etag': '"' + etag + '"'
+    });
+    res.end(http_status == 304 ? null : image);
+  }
+});
+
+module.exports = router;

+ 0 - 0
skins/renders/.gitkeep


+ 0 - 0
skins/skins/.gitkeep


+ 20 - 1
test/test.js

@@ -7,6 +7,7 @@ var logging = require("../modules/logging");
 var config = require("../modules/config");
 var config = require("../modules/config");
 var skins = require("../modules/skins");
 var skins = require("../modules/skins");
 var cache = require("../modules/cache");
 var cache = require("../modules/cache");
+var renders = require("../modules/renders");
 
 
 // we don't want tests to fail because of slow internet
 // we don't want tests to fail because of slow internet
 config.http_timeout *= 3;
 config.http_timeout *= 3;
@@ -112,7 +113,6 @@ describe("Crafatar", function() {
       done();
       done();
     });
     });
   });
   });
-
   describe("Errors", function() {
   describe("Errors", function() {
     it("should time out on uuid info download", function(done) {
     it("should time out on uuid info download", function(done) {
       var original_timeout = config.http_timeout;
       var original_timeout = config.http_timeout;
@@ -205,6 +205,25 @@ describe("Crafatar", function() {
         });
         });
       });
       });
 
 
+      describe("Networking: Render", function() {
+        it("should not fail (username, 64x64 skin)", function(done) {
+          helpers.get_render("Jake0oo0", 6, true, true, function(err, hash, img) {
+            assert.strictEqual(err, null);
+            done();
+          });
+        });
+      });
+
+      describe("Networking: Render", function() {
+        it("should not fail (username, 32x64 skin)", function(done) {
+          helpers.get_render("md_5", 6, true, true, function(err, hash, img) {
+            assert.strictEqual(err, null);
+            done();
+          });
+        });
+      });
+
+
       describe("Errors", function() {
       describe("Errors", function() {
         before(function() {
         before(function() {
           cache.get_redis().flushall();
           cache.get_redis().flushall();

+ 38 - 2
views/index.jade

@@ -63,12 +63,44 @@ block content
           | Replace 
           | Replace 
           mark.green id 
           mark.green id 
           | with a Mojang <b>UUID</b> or <b>username</b> to get the related skin.
           | with a Mojang <b>UUID</b> or <b>username</b> to get the related skin.
-          | You are redirected to the textures URL, or the default image is served.<br>
+          | The user's skin will be returned, or the default image is served.<br>
           | You can use the default parameter here as well.
           | You can use the default parameter here as well.
         .code
         .code
           | #{domain}/skins/
           | #{domain}/skins/
           mark.green id
           mark.green id
 
 
+        a(id="renders", class="anchor")
+        a(href="renders")
+          h3 3D Renders
+        p
+          | Crafatar also provides support for 3D renders of Minecraft skins.
+          | Replace
+          mark.green id
+          | with a Mojang <b>UUID</b> or <b>username</b> to get an render for the skin.
+        .code
+          | #{domain}/renders/head/
+          mark.green id
+        .code
+          | #{domain}/renders/body/
+          mark.green id
+        | The <b>default</b> parameter can also be used here. Using alex or steve will create a
+        | render with the same parameters. A custom image will not be rendered. A UUID or username
+        | without a skin, will produce a render based on the input id, or the <b>default</b> parameter.
+        | Using the <b>helm</b> parameter is also allowed, which will be overlayed onto the head.
+        | The <b>head</b> render type will return only a render of the skin's head, while the
+        | <b>body</b> render will return a render of the entire skin.
+
+        a(id="#render-parameters", class="#render-anchor")
+        a(href="#render-parameters")
+          h3 Render Parameters
+        a(id="scale", class="anchor")
+        a(href="#scale")
+          h4 scale
+        p
+          | The scale factor of the image #{config.min_scale} - #{config.max_scale}.<br>
+          | Default is #{config.default_scale}. The actual size differs between the type of render.
+
+
         a(id="http-headers", class="anchor")
         a(id="http-headers", class="anchor")
         a(href="#http-headers")
         a(href="#http-headers")
           h3 HTTP headers
           h3 HTTP headers
@@ -126,7 +158,7 @@ block content
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6
         p Jeb's avatar, 64 × 64
         p Jeb's avatar, 64 × 64
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6?size=64
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6?size=64
-        p Jeb's avatar, 64 × 64, with helm
+        p Jeb's avatar, 64 × 64, with helmet
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6?size=64&amp;helm
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6?size=64&amp;helm
         p Jeb's avatar, or fall back to steve
         p Jeb's avatar, or fall back to steve
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6?default=steve
         .code #{domain}/avatars/853c80ef3c3749fdaa49938b674adae6?default=steve
@@ -138,6 +170,10 @@ block content
         .code #{domain}/skins/853c80ef3c3749fdaa49938b674adae6
         .code #{domain}/skins/853c80ef3c3749fdaa49938b674adae6
         p Jeb's skin by username
         p Jeb's skin by username
         .code #{domain}/skins/jeb_
         .code #{domain}/skins/jeb_
+        p Render of Jeb's Head
+        .code #{domain}/renders/head/853c80ef3c3749fdaa49938b674adae6
+        p Render of Jeb's Body, with helmet, by username
+        .code #{domain}/renders/body/jeb_?helm
       .col-md-2.center
       .col-md-2.center
         .sideface.redstone_sheep(title="redstone_sheep")
         .sideface.redstone_sheep(title="redstone_sheep")
         .sideface.Jake0oo0(title="Jake0oo0")
         .sideface.Jake0oo0(title="Jake0oo0")