helpers.js 4.1 KB

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