avatars.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var networking = require('../modules/networking');
  2. var logging = require('../modules/logging');
  3. var helpers = require('../modules/helpers');
  4. var router = require('express').Router();
  5. var config = require('../modules/config');
  6. var skins = require('../modules/skins');
  7. var human_status = {
  8. 0: "none",
  9. 1: "cached",
  10. 2: "downloaded",
  11. 3: "checked",
  12. "-1": "error"
  13. };
  14. router.get('/skins/:uuid.:ext?', function(req, res) {
  15. var uuid = req.params.uuid;
  16. var start = new Date();
  17. if (!helpers.uuid_valid(uuid)) {
  18. res.status(422).send("422 Invalid UUID");
  19. return;
  20. }
  21. // strip dashes
  22. uuid = uuid.replace(/-/g, "");
  23. try {
  24. helpers.get_image_hash(uuid, function(err, status, hash) {
  25. if (hash) {
  26. res.writeHead(301, {
  27. 'Location': "http://textures.minecraft.net/texture/" + hash,
  28. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  29. 'Response-Time': new Date() - start,
  30. 'X-Storage-Type': human_status[status]
  31. });
  32. res.end();
  33. } else if (!err) {
  34. res.writeHead(404, {
  35. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  36. 'Response-Time': new Date() - start,
  37. 'X-Storage-Type': human_status[status]
  38. });
  39. res.end("404 Not found");
  40. } else {
  41. res.status(500).send("500 Internal server error");
  42. }
  43. });
  44. } catch(e) {
  45. logging.error("Error!");
  46. logging.error(e);
  47. res.status(500).send("500 Internal server error");
  48. }
  49. });
  50. /* GET avatar request. */
  51. router.get('/avatars/:uuid.:ext?', function(req, res) {
  52. var uuid = req.params.uuid;
  53. var size = req.query.size || config.default_size;
  54. var def = req.query.default;
  55. var helm = req.query.hasOwnProperty('helm');
  56. var start = new Date();
  57. // Prevent app from crashing/freezing
  58. if (size < config.min_size || size > config.max_size) {
  59. // "Unprocessable Entity", valid request, but semantically erroneous:
  60. // https://tools.ietf.org/html/rfc4918#page-78
  61. res.status(422).send("422 Invalid size");
  62. return;
  63. } else if (!helpers.uuid_valid(uuid)) {
  64. res.status(422).send("422 Invalid UUID");
  65. return;
  66. }
  67. // strip dashes
  68. uuid = uuid.replace(/-/g, "");
  69. try {
  70. helpers.get_avatar(uuid, helm, size, function(err, status, image) {
  71. logging.log(uuid + " - " + human_status[status]);
  72. if (err) {
  73. logging.error(err);
  74. }
  75. if (image) {
  76. sendimage(err ? 503 : 200, status, image);
  77. } else {
  78. handle_default(404, status);
  79. }
  80. });
  81. } catch(e) {
  82. logging.error("Error!");
  83. logging.error(e);
  84. handle_default(500, status);
  85. }
  86. function handle_default(http_status, img_status) {
  87. if (def && def != "steve" && def != "alex") {
  88. res.writeHead(301, {
  89. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  90. 'Response-Time': new Date() - start,
  91. 'X-Storage-Type': human_status[img_status],
  92. 'Location': def
  93. });
  94. res.end();
  95. } else {
  96. def = def || skins.default_skin(uuid);
  97. skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
  98. sendimage(http_status, img_status, image);
  99. });
  100. }
  101. }
  102. function sendimage(http_status, img_status, image) {
  103. res.writeHead(http_status, {
  104. 'Content-Type': 'image/png',
  105. 'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
  106. 'Response-Time': new Date() - start,
  107. 'X-Storage-Type': human_status[img_status],
  108. 'Access-Control-Allow-Origin': "*"
  109. });
  110. res.end(image);
  111. }
  112. });
  113. module.exports = router;