avatars.js 3.4 KB

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