avatars.js 3.4 KB

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