renders.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var router = require("express").Router();
  2. var logging = require("../modules/logging");
  3. var helpers = require("../modules/helpers");
  4. var config = require("../modules/config");
  5. var skins = require("../modules/skins");
  6. var renders = require("../modules/renders");
  7. var fs = require("fs");
  8. var human_status = {
  9. 0: "none",
  10. 1: "cached",
  11. 2: "downloaded",
  12. 3: "checked",
  13. "-1": "error"
  14. };
  15. // valid types: head, body. helmet is query param
  16. // The Type logic should be two separate GET
  17. // functions once response methods are extracted
  18. router.get("/:type/:uuid.:ext?", function(req, res) {
  19. var raw_type = req.params.type;
  20. // Check valid type for now
  21. if (raw_type != "body" && raw_type != "head") {
  22. res.status(404).send("404 Invalid Render Type");
  23. return;
  24. }
  25. var body = raw_type == "body";
  26. var uuid = req.params.uuid;
  27. var def = req.query.default;
  28. var scale = parseInt(req.query.scale) || config.default_scale;
  29. var helm = req.query.hasOwnProperty("helm");
  30. var start = new Date();
  31. var etag = null;
  32. if (scale < config.min_scale || scale > config.max_scale) {
  33. // Preventing from OOM crashes.
  34. res.status(422).send("422 Invalid Scale");
  35. return;
  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(uuid + " " + 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. logging.log("image not found, using default.");
  63. handle_default(404, status);
  64. }
  65. });
  66. } catch(e) {
  67. logging.error(uuid + " error:");
  68. logging.error(e);
  69. handle_default(500, status);
  70. }
  71. // default alex/steve images can be rendered, but
  72. // custom images will not be
  73. function handle_default(http_status, img_status) {
  74. if (def && def != "steve" && def != "alex") {
  75. res.writeHead(301, {
  76. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  77. "Response-Time": new Date() - start,
  78. "X-Storage-Type": human_status[img_status],
  79. "Access-Control-Allow-Origin": "*",
  80. "Location": def
  81. });
  82. res.end();
  83. } else {
  84. def = def || skins.default_skin(uuid);
  85. fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
  86. if (err) {
  87. // errored while loading the default image, continuing with null image
  88. logging.error("error loading default render image: " + err);
  89. }
  90. // we render the default skins, but not custom images
  91. renders.draw_model(uuid, buf, scale, helm, body, function(err, def_img) {
  92. if (err) {
  93. logging.log("error while rendering default image: " + err);
  94. }
  95. sendimage(http_status, img_status, def_img);
  96. });
  97. });
  98. }
  99. }
  100. function sendimage(http_status, img_status, image) {
  101. res.writeHead(http_status, {
  102. "Content-Type": "image/png",
  103. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  104. "Response-Time": new Date() - start,
  105. "X-Storage-Type": human_status[img_status],
  106. "Access-Control-Allow-Origin": "*",
  107. "Etag": '"' + etag + '"'
  108. });
  109. res.end(http_status == 304 ? null : image);
  110. }
  111. });
  112. module.exports = router;