avatars.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 userId = (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. var rid = req.id;
  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.id_valid(userId)) {
  33. res.writeHead(422, {
  34. "Content-Type": "text/plain",
  35. "Response-Time": new Date() - start
  36. });
  37. res.end("Invalid ID");
  38. return;
  39. }
  40. // strip dashes
  41. userId = userId.replace(/-/g, "");
  42. logging.log(rid + "userid: " + userId);
  43. try {
  44. helpers.get_avatar(rid, userId, helm, size, function(err, status, image, hash) {
  45. logging.log(rid + "storage type: " + human_status[status]);
  46. if (err) {
  47. logging.error(rid + err);
  48. if (err.code === "ENOENT") {
  49. // no such file
  50. cache.remove_hash(rid, userId);
  51. }
  52. }
  53. etag = image && hash && hash.substr(0, 32) || "none";
  54. var matches = req.headers["if-none-match"] === '"' + etag + '"';
  55. if (image) {
  56. var http_status = 200;
  57. if (matches) {
  58. http_status = 304;
  59. } else if (err) {
  60. http_status = 503;
  61. }
  62. logging.debug(rid + "etag: " + req.headers["if-none-match"]);
  63. logging.debug(rid + "matches: " + matches);
  64. sendimage(rid, http_status, status, image);
  65. } else {
  66. handle_default(rid, 404, status, userId);
  67. }
  68. });
  69. } catch(e) {
  70. logging.error(rid + "error: " + e.stack);
  71. handle_default(rid, 500, -1, userId);
  72. }
  73. function handle_default(rid, http_status, img_status, userId) {
  74. if (def && def !== "steve" && def !== "alex") {
  75. logging.log(rid + "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. "X-Request-ID": rid,
  81. "Access-Control-Allow-Origin": "*",
  82. "Location": def
  83. });
  84. res.end();
  85. } else {
  86. def = def || skins.default_skin(userId);
  87. skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
  88. sendimage(rid, http_status, img_status, image);
  89. });
  90. }
  91. }
  92. function sendimage(rid, http_status, img_status, image) {
  93. logging.log(rid + "status: " + http_status);
  94. res.writeHead(http_status, {
  95. "Content-Type": "image/png",
  96. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  97. "Response-Time": new Date() - start,
  98. "X-Storage-Type": human_status[img_status],
  99. "X-Request-ID": rid,
  100. "Access-Control-Allow-Origin": "*",
  101. "Etag": '"' + etag + '"'
  102. });
  103. res.end(http_status === 304 ? null : image);
  104. }
  105. };