helpers.js 4.8 KB

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