avatars.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var networking = require("../modules/networking");
  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 cache = require("../modules/cache");
  7. var human_status = {
  8. 0: "none",
  9. 1: "cached",
  10. 2: "downloaded",
  11. 3: "checked",
  12. "-1": "error"
  13. };
  14. // GET avatar request
  15. module.exports = function(req, res) {
  16. var start = new Date();
  17. var uuid = (req.url.path_list[2] || "").split(".")[0];
  18. var size = parseInt(req.url.query.size) || config.default_size;
  19. var def = req.url.query.default;
  20. var helm = req.url.query.hasOwnProperty("helm");
  21. var etag = null;
  22. // Prevent app from crashing/freezing
  23. if (size < config.min_size || size > config.max_size) {
  24. // "Unprocessable Entity", valid request, but semantically erroneous:
  25. // https://tools.ietf.org/html/rfc4918#page-78
  26. res.writeHead(422, {
  27. "Content-Type": "text/plain",
  28. "Response-Time": new Date() - start
  29. });
  30. res.end("Invalid Size");
  31. return;
  32. } else if (!helpers.uuid_valid(uuid)) {
  33. res.writeHead(422, {
  34. "Content-Type": "text/plain",
  35. "Response-Time": new Date() - start
  36. });
  37. res.end("Invalid UUID");
  38. return;
  39. }
  40. // strip dashes
  41. uuid = uuid.replace(/-/g, "");
  42. try {
  43. helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) {
  44. logging.log(uuid + " - " + human_status[status]);
  45. if (err) {
  46. logging.error(uuid + " " + err);
  47. if (err.code == "ENOENT") {
  48. logging.warn("Deleting " + uuid + " from cache!");
  49. cache.remove_hash(uuid);
  50. }
  51. }
  52. etag = image && hash && hash.substr(0, 32) || "none";
  53. var matches = req.headers["if-none-match"] == '"' + etag + '"';
  54. if (image) {
  55. var http_status = 200;
  56. if (matches) {
  57. http_status = 304;
  58. } else if (err) {
  59. http_status = 503;
  60. }
  61. logging.debug("Etag: " + req.headers["if-none-match"]);
  62. logging.debug("matches: " + matches);
  63. sendimage(http_status, status, image);
  64. } else {
  65. handle_default(404, status);
  66. }
  67. });
  68. } catch(e) {
  69. logging.error(uuid + " error:");
  70. logging.error(e);
  71. handle_default(500, status);
  72. }
  73. function handle_default(http_status, img_status) {
  74. if (def && def != "steve" && def != "alex") {
  75. logging.log("status: 301");
  76. res.writeHead(301, {
  77. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  78. "Response-Time": new Date() - start,
  79. "X-Storage-Type": human_status[img_status],
  80. "Access-Control-Allow-Origin": "*",
  81. "Location": def
  82. });
  83. res.end();
  84. } else {
  85. def = def || skins.default_skin(uuid);
  86. skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
  87. sendimage(http_status, img_status, image);
  88. });
  89. }
  90. }
  91. function sendimage(http_status, img_status, image) {
  92. logging.log("status: " + http_status);
  93. res.writeHead(http_status, {
  94. "Content-Type": "image/png",
  95. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  96. "Response-Time": new Date() - start,
  97. "X-Storage-Type": human_status[img_status],
  98. "Access-Control-Allow-Origin": "*",
  99. "Etag": '"' + etag + '"'
  100. });
  101. res.end(http_status == 304 ? null : image);
  102. }
  103. };