avatars.js 3.2 KB

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