helpers.js 4.5 KB

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