avatars.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var helpers = require("../helpers");
  2. var config = require("../../config");
  3. var skins = require("../skins");
  4. var cache = require("../cache");
  5. var path = require("path");
  6. var url = require("url");
  7. function handle_default(img_status, userId, size, def, req, err, callback) {
  8. def = def || skins.default_skin(userId);
  9. if (def !== "steve" && def !== "alex") {
  10. if (helpers.id_valid(def)) {
  11. // clean up the old URL to match new image
  12. var parsed = req.url;
  13. delete parsed.query.default;
  14. delete parsed.search;
  15. parsed.pathname = parsed.pathname.replace(userId, def);
  16. var newUrl = url.format(parsed);
  17. callback({
  18. status: img_status,
  19. redirect: newUrl,
  20. err: err
  21. });
  22. } else {
  23. callback({
  24. status: img_status,
  25. redirect: def,
  26. err: err
  27. });
  28. }
  29. } else {
  30. // handle steve and alex
  31. skins.resize_img(path.join(__dirname, "..", "public", "images", def + ".png"), size, function(resize_err, image) {
  32. callback({
  33. status: img_status,
  34. body: image,
  35. type: "image/png",
  36. hash: def,
  37. err: resize_err || err
  38. });
  39. });
  40. }
  41. }
  42. // GET avatar request
  43. module.exports = function(req, callback) {
  44. var userId = (req.url.path_list[1] || "").split(".")[0];
  45. var size = parseInt(req.url.query.size) || config.avatars.default_size;
  46. var def = req.url.query.default;
  47. var helm = req.url.query.hasOwnProperty("helm");
  48. // check for extra paths
  49. if (req.url.path_list.length > 2) {
  50. callback({
  51. status: -2,
  52. body: "Invalid Path",
  53. code: 404
  54. });
  55. return;
  56. }
  57. // Prevent app from crashing/freezing
  58. if (size < config.avatars.min_size || size > config.avatars.max_size) {
  59. // "Unprocessable Entity", valid request, but semantically erroneous:
  60. // https://tools.ietf.org/html/rfc4918#page-78
  61. callback({
  62. status: -2,
  63. body: "Invalid Size"
  64. });
  65. return;
  66. } else if (!helpers.id_valid(userId)) {
  67. callback({
  68. status: -2,
  69. body: "Invalid UserID"
  70. });
  71. return;
  72. }
  73. // strip dashes
  74. userId = userId.replace(/-/g, "");
  75. try {
  76. helpers.get_avatar(req.id, userId, helm, size, function(err, status, image, hash) {
  77. if (err) {
  78. if (err.code === "ENOENT") {
  79. // no such file
  80. cache.remove_hash(req.id, userId);
  81. }
  82. }
  83. if (image) {
  84. callback({
  85. status: status,
  86. body: image,
  87. type: "image/png",
  88. err: err,
  89. hash: hash
  90. });
  91. } else {
  92. handle_default(status, userId, size, def, req, err, callback);
  93. }
  94. });
  95. } catch (e) {
  96. handle_default(-1, userId, size, def, req, e, callback);
  97. }
  98. };