renders.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(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("Error!");
  68. logging.error(e);
  69. handle_default(500, status);
  70. }
  71. function handle_default(http_status, img_status) {
  72. if (def && def != "steve" && def != "alex") {
  73. res.writeHead(301, {
  74. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  75. 'Response-Time': new Date() - start,
  76. 'X-Storage-Type': human_status[img_status],
  77. 'Access-Control-Allow-Origin': '*',
  78. 'Location': def
  79. });
  80. res.end();
  81. } else {
  82. def = def || skins.default_skin(uuid);
  83. fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
  84. if (err) {
  85. logging.error("error rendering default image: " + err);
  86. }
  87. renders.draw_model(uuid, buf, scale, helm, body, function(err, def_img) {
  88. if (err) {
  89. logging.log("error while rendering default image: " + err);
  90. }
  91. sendimage(http_status, img_status, def_img);
  92. });
  93. });
  94. }
  95. }
  96. function sendimage(http_status, img_status, image) {
  97. res.writeHead(http_status, {
  98. 'Content-Type': 'image/png',
  99. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  100. 'Response-Time': new Date() - start,
  101. 'X-Storage-Type': human_status[img_status],
  102. 'Access-Control-Allow-Origin': '*',
  103. 'Etag': '"' + etag + '"'
  104. });
  105. res.end(http_status == 304 ? null : image);
  106. }
  107. });
  108. module.exports = router;