Jake 10 лет назад
Родитель
Сommit
2873157e97
7 измененных файлов с 279 добавлено и 10 удалено
  1. 2 0
      .buildpacks
  2. 8 7
      app.js
  3. 5 3
      modules/config.example.js
  4. 21 0
      modules/config.js
  5. 133 0
      modules/renders.js
  6. 1 0
      package.json
  7. 109 0
      routes/renders.js

+ 2 - 0
.buildpacks

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

+ 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) {

+ 5 - 3
modules/config.example.js

@@ -8,9 +8,11 @@ var config = {
   cleaning_limit: 10240,     // minumum required available KB on disk to trigger cleaning
   cleaning_limit: 10240,     // minumum required available KB on disk to trigger cleaning
   cleaning_amount: 50000,    // amount of avatar (and their helm) files to clean
   cleaning_amount: 50000,    // amount of avatar (and their helm) files to clean
   http_timeout: 1000,        // ms until connection to mojang is dropped
   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
+  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
+  default_scale: 6,          // the scale of rendered avatars
+  maximum_sale: 10           // the maximum scale of rendered avatars
 };
 };
 
 
 module.exports = config;
 module.exports = config;

+ 21 - 0
modules/config.js

@@ -0,0 +1,21 @@
+var config = {
+  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: 3,       // seconds interval: deleting images if disk size at limit
+  cleaning_limit: 900000000000,         // 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: true,          // enables logging.debug
+  min_scale: 1,                  // for renders
+  max_scale: 100,                 // 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;

+ 133 - 0
modules/renders.js

@@ -0,0 +1,133 @@
+// Skin locations are based on the work of Confuser
+// 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 exp = {};
+
+var Canvas = require('canvas');
+var Image = Canvas.Image;
+
+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);
+}
+
+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);
+}
+
+exp.draw_body = function(skin_canvas, model_ctx, 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);
+}
+
+exp.draw_model = function(uuid, scale, helm, body, callback) {
+  helpers.get_skin(uuid, function(err, hash, img) {
+    var image = new Image;
+    var width = 64 * scale;
+    var height = 64 * 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');
+
+    image.onerror = function(err) {
+      console.log("render error: " + err);
+      callback(err, 2, null, hash);
+    };
+
+    image.onload = function() {
+      skin_ctx.drawImage(image,0,0,64,64,0,0,64,64);
+      //Scale it
+      scale_image(skin_ctx.getImageData(0,0,64,64), skin_ctx, 0, 0, scale);
+      if (body) {
+        console.log("drawing body");
+        exp.draw_body(skin_canvas, model_ctx, scale);
+      }
+      console.log("drawing head");
+      exp.draw_head(skin_canvas, model_ctx, scale);
+      if (helm) {
+        console.log("drawing helmet");
+        exp.draw_helmet(skin_canvas, model_ctx, scale);
+      }
+
+      model_canvas.toBuffer(function(err, buf){
+        callback(err, 2, buf, hash);
+      });
+    };
+
+    image.src = img;
+  });
+}
+
+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;

+ 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",

+ 109 - 0
routes/renders.js

@@ -0,0 +1,109 @@
+var router = require('express').Router();
+var networking = require('../modules/networking');
+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 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 == "head" ? false : true
+  var uuid = req.params.uuid;
+  var def = req.params.def;
+  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.maximum_scale) {
+    // Preventing from OOM crashes.
+    res.status(422).send("422 Invalid Size");
+  } else if (!helpers.uuid_valid(uuid)) {
+    res.status(422).send("422 Invalid UUID");
+    return;
+  }
+
+  // strip dashes
+  uuid = uuid.replace(/-/g, "");
+
+  try {
+    renders.draw_model(uuid, scale, helm, body, function(err, status, image, hash) {
+      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 {
+        //handle_default(404, status);
+      }
+    });
+  } catch(e) {
+    logging.error("Error!");
+    logging.error(e);
+    //handle_default(500, status);
+  }
+
+  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);
+      skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
+        sendimage(http_status, img_status, image);
+      });
+    }
+  }
+
+  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;