avatars.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. cache.remove_hash(uuid);
  49. }
  50. }
  51. etag = image && hash && hash.substr(0, 32) || "none";
  52. var matches = req.headers["if-none-match"] == '"' + etag + '"';
  53. if (image) {
  54. var http_status = 200;
  55. if (matches) {
  56. http_status = 304;
  57. } else if (err) {
  58. http_status = 503;
  59. }
  60. logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
  61. logging.debug(uuid + " matches: " + matches);
  62. sendimage(http_status, status, image, uuid);
  63. } else {
  64. handle_default(404, status, uuid);
  65. }
  66. });
  67. } catch(e) {
  68. logging.error(uuid + " error: " + e);
  69. handle_default(500, status, uuid);
  70. }
  71. function handle_default(http_status, img_status, uuid) {
  72. if (def && def != "steve" && def != "alex") {
  73. logging.log(uuid + " status: 301");
  74. res.writeHead(301, {
  75. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  76. "Response-Time": new Date() - start,
  77. "X-Storage-Type": human_status[img_status],
  78. "Access-Control-Allow-Origin": "*",
  79. "Location": def
  80. });
  81. res.end();
  82. } else {
  83. def = def || skins.default_skin(uuid);
  84. skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
  85. sendimage(http_status, img_status, image, uuid);
  86. });
  87. }
  88. }
  89. function sendimage(http_status, img_status, image, uuid) {
  90. logging.log(uuid + " status: " + http_status);
  91. res.writeHead(http_status, {
  92. "Content-Type": "image/png",
  93. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  94. "Response-Time": new Date() - start,
  95. "X-Storage-Type": human_status[img_status],
  96. "Access-Control-Allow-Origin": "*",
  97. "Etag": '"' + etag + '"'
  98. });
  99. res.end(http_status == 304 ? null : image);
  100. }
  101. };