skins.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var logging = require("../logging");
  2. var helpers = require("../helpers");
  3. var config = require("../config");
  4. var skins = require("../skins");
  5. var path = require("path");
  6. var lwip = require("lwip");
  7. // GET skin request
  8. module.exports = function(req, res) {
  9. var start = new Date();
  10. var userId = (req.url.path_list[2] || "").split(".")[0];
  11. var def = req.url.query.default;
  12. var etag = null;
  13. var rid = req.id;
  14. function sendimage(rid, http_status, image) {
  15. logging.log(rid, "status:", http_status);
  16. res.writeHead(http_status, {
  17. "Content-Type": "image/png",
  18. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  19. "Response-Time": new Date() - start,
  20. "X-Storage-Type": "downloaded",
  21. "X-Request-ID": rid,
  22. "Access-Control-Allow-Origin": "*",
  23. "Etag": '"' + etag + '"'
  24. });
  25. res.end(http_status === 304 ? null : image);
  26. }
  27. function handle_default(rid, http_status, userId) {
  28. if (def && def !== "steve" && def !== "alex") {
  29. logging.log(rid, "status: 301");
  30. res.writeHead(301, {
  31. "Cache-Control": "max-age=" + config.browser_cache_time + ", public",
  32. "Response-Time": new Date() - start,
  33. "X-Storage-Type": "downloaded",
  34. "X-Request-ID": rid,
  35. "Access-Control-Allow-Origin": "*",
  36. "Location": def
  37. });
  38. res.end();
  39. } else {
  40. def = def || skins.default_skin(userId);
  41. lwip.open(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function(err, image) {
  42. // FIXME: err is not handled
  43. image.toBuffer("png", function(buf_err, buffer) {
  44. // FIXME: buf_err is not handled
  45. sendimage(rid, http_status, buffer);
  46. });
  47. });
  48. }
  49. }
  50. if (!helpers.id_valid(userId)) {
  51. res.writeHead(422, {
  52. "Content-Type": "text/plain",
  53. "Response-Time": new Date() - start
  54. });
  55. res.end("Invalid ID");
  56. return;
  57. }
  58. // strip dashes
  59. userId = userId.replace(/-/g, "");
  60. logging.debug(rid, "userid:", userId);
  61. try {
  62. helpers.get_skin(rid, userId, function(err, hash, image) {
  63. if (err) {
  64. logging.error(rid, err);
  65. }
  66. etag = 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 (err) {
  71. http_status = 503;
  72. }
  73. logging.debug(rid, "etag:", req.headers["if-none-match"]);
  74. logging.debug(rid, "matches:", matches);
  75. sendimage(rid, matches ? 304 : http_status, image);
  76. } else {
  77. handle_default(rid, 200, userId);
  78. }
  79. });
  80. } catch(e) {
  81. logging.error(rid, "error:", e.stack);
  82. handle_default(rid, 500, userId);
  83. }
  84. };