renders.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var router = require('express').Router();
  2. var networking = require('../modules/networking');
  3. var logging = require('../modules/logging');
  4. var helpers = require('../modules/helpers');
  5. var config = require('../modules/config');
  6. var skins = require('../modules/skins');
  7. var renders = require('../modules/renders');
  8. var fs = require('fs');
  9. var human_status = {
  10. 0: "none",
  11. 1: "cached",
  12. 2: "downloaded",
  13. 3: "checked",
  14. "-1": "error"
  15. };
  16. // valid types: head, body. helmet is query param
  17. // The Type logic should be two separate GET
  18. // functions once response methods are extracted
  19. router.get('/:type/:uuid.:ext?', function(req, res) {
  20. var raw_type = req.params.type;
  21. // Check valid type for now
  22. if (raw_type != "body" && raw_type != "head") {
  23. res.status(404).send("404 Invalid Render Type");
  24. return;
  25. }
  26. var body = raw_type == "head" ? false : true
  27. var uuid = req.params.uuid;
  28. var def = req.params.def;
  29. var scale = parseInt(req.query.scale) || config.default_scale;
  30. var helm = req.query.hasOwnProperty('helm');
  31. var start = new Date();
  32. var etag = null;
  33. if (scale > config.maximum_scale) {
  34. // Preventing from OOM crashes.
  35. res.status(422).send("422 Invalid Size");
  36. } else if (!helpers.uuid_valid(uuid)) {
  37. res.status(422).send("422 Invalid UUID");
  38. return;
  39. }
  40. // strip dashes
  41. uuid = uuid.replace(/-/g, "");
  42. try {
  43. helpers.get_render(uuid, scale, helm, body, function(err, status, hash, image) {
  44. logging.log(uuid + " - " + human_status[status]);
  45. if (err) {
  46. logging.error(err);
  47. }
  48. etag = hash && hash.substr(0, 32) || "none";
  49. var matches = req.get("If-None-Match") == '"' + etag + '"';
  50. if (image) {
  51. var http_status = 200;
  52. if (matches) {
  53. http_status = 304;
  54. } else if (err) {
  55. http_status = 503;
  56. }
  57. logging.log("matches: " + matches);
  58. logging.log("Etag: " + req.get("If-None-Match"));
  59. logging.log("status: " + http_status);
  60. sendimage(http_status, status, image);
  61. } else {
  62. handle_default(404, status);
  63. }
  64. });
  65. } catch(e) {
  66. logging.error("Error!");
  67. logging.error(e);
  68. handle_default(500, status);
  69. }
  70. function handle_default(http_status, img_status) {
  71. if (def && def != "steve" && def != "alex") {
  72. res.writeHead(301, {
  73. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  74. 'Response-Time': new Date() - start,
  75. 'X-Storage-Type': human_status[img_status],
  76. 'Access-Control-Allow-Origin': '*',
  77. 'Location': def
  78. });
  79. res.end();
  80. } else {
  81. def = def || skins.default_skin(uuid);
  82. fs.readFile("public/images/" + def + ".png", function (err, buf) {
  83. if (err) throw err;
  84. console.log(buf);
  85. renders.draw_model(uuid, buf, scale, helm, body, function(err, status, image) {
  86. sendimage(http_status, img_status, image);
  87. });
  88. });
  89. }
  90. }
  91. function sendimage(http_status, img_status, image) {
  92. res.writeHead(http_status, {
  93. 'Content-Type': 'image/png',
  94. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  95. 'Response-Time': new Date() - start,
  96. 'X-Storage-Type': human_status[img_status],
  97. 'Access-Control-Allow-Origin': '*',
  98. 'Etag': '"' + etag + '"'
  99. });
  100. res.end(http_status == 304 ? null : image);
  101. }
  102. });
  103. module.exports = router;