helpers.js 4.9 KB

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