helpers.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var networking = require('./networking');
  2. var config = require('./config');
  3. var cache = require('./cache');
  4. var skins = require('./skins');
  5. // 0098cb60-fa8e-427c-b299-793cbd302c9a
  6. var valid_uuid = /^([0-9a-f-]{32,36}|[a-zA-Z0-9_]{1,16})$/; // uuid|username
  7. var hash_pattern = /[0-9a-f]+$/;
  8. function get_hash(url) {
  9. return hash_pattern.exec(url)[0].toLowerCase();
  10. }
  11. // requests skin for +uuid+ and extracts face/helm if image hash in +details+ changed
  12. // callback contains error, image hash
  13. function store_images(uuid, details, callback) {
  14. // get skin_url for +uuid+
  15. networking.get_skin_url(uuid, function(err, skin_url) {
  16. if (err) {
  17. callback(err, null);
  18. } else {
  19. if (skin_url) {
  20. console.log(uuid + " " + skin_url);
  21. // set file paths
  22. var hash = get_hash(skin_url);
  23. if (details && details.hash == hash) {
  24. // hash hasn't changed
  25. console.log(uuid + " hash has not changed");
  26. cache.update_timestamp(uuid, hash);
  27. callback(null, hash);
  28. } else {
  29. // hash has changed
  30. console.log(uuid + " new hash: " + hash);
  31. var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
  32. var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
  33. // download skin, extract face/helm
  34. networking.skin_file(skin_url, facepath, helmpath, function(err) {
  35. if (err) {
  36. callback(err, null);
  37. } else {
  38. cache.save_hash(uuid, hash);
  39. callback(null, hash);
  40. }
  41. });
  42. }
  43. } else {
  44. // profile found, but has no skin
  45. cache.save_hash(uuid, null);
  46. callback(null, null);
  47. }
  48. }
  49. });
  50. }
  51. // decides whether to get an image from disk or to download it
  52. // callback contains error, status, hash
  53. // the status gives information about how the image was received
  54. // -1: "error"
  55. // 0: "none" - cached as null
  56. // 1: "cached" - found on disk
  57. // 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
  58. // 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
  59. function get_image_hash(uuid, callback) {
  60. cache.get_details(uuid, function(err, details) {
  61. if (err) {
  62. callback(err, -1, null);
  63. } else {
  64. if (details && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
  65. // uuid known + recently updated
  66. console.log(uuid + " uuid known & recently updated");
  67. callback(null, (details.hash ? 1 : 0), details.hash);
  68. } else {
  69. console.log(uuid + " uuid not known or too old");
  70. console.log("details:");
  71. console.log(details);
  72. console.log("/details");
  73. store_images(uuid, details, function(err, hash) {
  74. if (err) {
  75. callback(err, -1, details && details.hash);
  76. } else {
  77. console.log(uuid + " hash: " + hash);
  78. var oldhash = details && details.hash;
  79. var status = hash !== oldhash ? 2 : 3;
  80. callback(null, status, hash);
  81. }
  82. });
  83. }
  84. }
  85. });
  86. }
  87. var exp = {};
  88. // returns true if the +uuid+ is a valid uuid or username
  89. // the uuid may be not exist, however
  90. exp.uuid_valid = function(uuid) {
  91. return valid_uuid.test(uuid);
  92. };
  93. // handles requests for +uuid+ images with +size+
  94. // callback contains error, status, image buffer
  95. // image is the user's face+helm when helm is true, or the face otherwise
  96. // for status, see get_image_hash
  97. exp.get_avatar = function(uuid, helm, size, callback) {
  98. console.log("\nrequest: " + uuid);
  99. get_image_hash(uuid, function(err, status, hash) {
  100. if (hash) {
  101. var filepath = __dirname + '/../' + (helm ? config.helms_dir : config.faces_dir) + hash + ".png";
  102. skins.resize_img(filepath, size, function(img_err, result) {
  103. if (img_err) {
  104. callback(img_err, -1, null);
  105. } else {
  106. // we might have a hash although an error occured
  107. // (e.g. Mojang servers not reachable, using outdated hash)
  108. callback(err, (err ? -1 : status), result);
  109. }
  110. });
  111. } else {
  112. // hash is null when uuid has no skin
  113. callback(err, status, null);
  114. }
  115. });
  116. };
  117. module.exports = exp;