skins.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var logging = require("../logging");
  2. var helpers = require("../helpers");
  3. var skins = require("../skins");
  4. var cache = require("../cache");
  5. var path = require("path");
  6. var lwip = require("lwip");
  7. var url = require("url");
  8. function handle_default(img_status, userId, def, req, err, callback) {
  9. def = def || skins.default_skin(userId);
  10. if (def !== "steve" && def !== "alex") {
  11. if (helpers.id_valid(def)) {
  12. // clean up the old URL to match new image
  13. var parsed = req.url;
  14. delete parsed.query.default;
  15. delete parsed.search;
  16. parsed.pathname = parsed.pathname.replace(userId, def);
  17. var newUrl = url.format(parsed);
  18. callback({
  19. status: img_status,
  20. redirect: newUrl,
  21. err: err
  22. });
  23. } else {
  24. callback({
  25. status: img_status,
  26. redirect: def,
  27. err: err
  28. });
  29. }
  30. } else {
  31. // handle steve and alex
  32. lwip.open(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function(lwip_err, image) {
  33. if (image) {
  34. image.toBuffer("png", function(buf_err, buffer) {
  35. callback({
  36. status: img_status,
  37. body: buffer,
  38. type: "image/png",
  39. hash: def,
  40. err: buf_err || lwip_err || err
  41. });
  42. });
  43. } else {
  44. callback({
  45. status: -1,
  46. err: lwip_err || err
  47. });
  48. }
  49. });
  50. }
  51. }
  52. // GET skin request
  53. module.exports = function(req, callback) {
  54. var userId = (req.url.path_list[1] || "").split(".")[0];
  55. var def = req.url.query.default;
  56. var rid = req.id;
  57. // check for extra paths
  58. if (req.url.path_list.length > 2) {
  59. callback({
  60. status: -2,
  61. body: "Invalid Path",
  62. code: 404
  63. });
  64. return;
  65. }
  66. 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_skin(rid, userId, function(err, hash, status, image) {
  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. hash: hash,
  89. err: err
  90. });
  91. } else {
  92. handle_default(2, userId, def, req, err, callback);
  93. }
  94. });
  95. } catch(e) {
  96. handle_default(-1, userId, def, req, e, callback);
  97. }
  98. };