|
@@ -15,11 +15,15 @@ function get_hash(url) {
|
|
|
return hash_pattern.exec(url)[0].toLowerCase();
|
|
|
}
|
|
|
|
|
|
-function store_skin(rid, userId, profile, details, callback) {
|
|
|
+// gets the skin for +userId+ with +profile+
|
|
|
+// uses +cache_details+ to determine if the skin needs to be downloaded or can be taken from cache
|
|
|
+// face and face+helm images are extracted and stored to files
|
|
|
+// callback: error, skin hash
|
|
|
+function store_skin(rid, userId, profile, cache_details, callback) {
|
|
|
networking.get_skin_url(rid, userId, profile, function(err, url) {
|
|
|
if (!err && url) {
|
|
|
var skin_hash = get_hash(url);
|
|
|
- if (details && details.skin === skin_hash) {
|
|
|
+ if (cache_details && cache_details.skin === skin_hash) {
|
|
|
cache.update_timestamp(rid, userId, skin_hash, false, function(err) {
|
|
|
callback(err, skin_hash);
|
|
|
});
|
|
@@ -60,11 +64,15 @@ function store_skin(rid, userId, profile, details, callback) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-function store_cape(rid, userId, profile, details, callback) {
|
|
|
+// gets the cape for +userId+ with +profile+
|
|
|
+// uses +cache_details+ to determine if the cape needs to be downloaded or can be taken from cache
|
|
|
+// the cape - if downloaded - is stored to file
|
|
|
+// callback: error, cape hash
|
|
|
+function store_cape(rid, userId, profile, cache_details, callback) {
|
|
|
networking.get_cape_url(rid, userId, profile, function(err, url) {
|
|
|
if (!err && url) {
|
|
|
var cape_hash = get_hash(url);
|
|
|
- if (details && details.cape === cape_hash) {
|
|
|
+ if (cache_details && cache_details.cape === cape_hash) {
|
|
|
cache.update_timestamp(rid, userId, cape_hash, false, function(err) {
|
|
|
callback(err, cape_hash);
|
|
|
});
|
|
@@ -120,7 +128,10 @@ function callback_for(userId, type, err, hash) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// returns true if any object in +arr+ has +value+ as +property+
|
|
|
+// returns true if any object in +arr+ has a +property+ that matches +value+
|
|
|
+//
|
|
|
+// deep_property_check([{foo: "bar"}, {foo: "baz"}], "foo", "baz");
|
|
|
+//
|
|
|
function deep_property_check(arr, property, value) {
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
if (arr[i][property] === value) {
|
|
@@ -131,10 +142,10 @@ function deep_property_check(arr, property, value) {
|
|
|
}
|
|
|
|
|
|
// downloads the images for +userId+ while checking the cache
|
|
|
-// status based on +details+. +type+ specifies which
|
|
|
+// status based on +cache_details+. +type+ specifies which
|
|
|
// image type should be called back on
|
|
|
-// +callback+ contains error, image hash
|
|
|
-function store_images(rid, userId, details, type, callback) {
|
|
|
+// callback: error, image hash
|
|
|
+function store_images(rid, userId, cache_details, type, callback) {
|
|
|
var is_uuid = userId.length > 16;
|
|
|
var new_hash = {
|
|
|
rid: rid,
|
|
@@ -160,7 +171,7 @@ function store_images(rid, userId, details, type, callback) {
|
|
|
}
|
|
|
} else {
|
|
|
// no error and we have a profile (if it's a uuid)
|
|
|
- store_skin(rid, userId, profile, details, function(err, skin_hash) {
|
|
|
+ store_skin(rid, userId, profile, cache_details, function(err, skin_hash) {
|
|
|
if (err && !skin_hash) {
|
|
|
// an error occured, not caching. we can try in 60 seconds
|
|
|
callback_for(userId, "skin", err, null);
|
|
@@ -170,7 +181,7 @@ function store_images(rid, userId, details, type, callback) {
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
- store_cape(rid, userId, profile, details, function(err, cape_hash) {
|
|
|
+ store_cape(rid, userId, profile, cache_details, function(err, cape_hash) {
|
|
|
if (err && !cape_hash) {
|
|
|
// an error occured, not caching. we can try in 60 seconds
|
|
|
callback_for(userId, "cape", (err || cache_err), cape_hash);
|
|
@@ -197,7 +208,7 @@ exp.id_valid = function(userId) {
|
|
|
};
|
|
|
|
|
|
// decides whether to get a +type+ image for +userId+ from disk or to download it
|
|
|
-// callback contains error, status, hash
|
|
|
+// callback: error, status, hash
|
|
|
// the status gives information about how the image was received
|
|
|
// -1: "error"
|
|
|
// 0: "none" - cached as null
|
|
@@ -205,32 +216,32 @@ exp.id_valid = function(userId) {
|
|
|
// 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
|
|
|
// 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
|
|
|
exp.get_image_hash = function(rid, userId, type, callback) {
|
|
|
- cache.get_details(userId, function(err, details) {
|
|
|
- var cached_hash = (details !== null) ? (type === "skin" ? details.skin : details.cape) : null;
|
|
|
+ cache.get_details(userId, function(err, cache_details) {
|
|
|
+ var cached_hash = (cache_details !== null) ? (type === "skin" ? cache_details.skin : cache_details.cape) : null;
|
|
|
if (err) {
|
|
|
callback(err, -1, null);
|
|
|
} else {
|
|
|
- if (details && details[type] !== undefined && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
|
|
|
+ if (cache_details && cache_details[type] !== undefined && cache_details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
|
|
|
// use cached image
|
|
|
logging.log(rid + "userId cached & recently updated");
|
|
|
callback(null, (cached_hash ? 1 : 0), cached_hash);
|
|
|
} else {
|
|
|
// download image
|
|
|
- if (details) {
|
|
|
+ if (cache_details) {
|
|
|
logging.log(rid + "userId cached, but too old");
|
|
|
} else {
|
|
|
logging.log(rid + "userId not cached");
|
|
|
}
|
|
|
- store_images(rid, userId, details, type, function(err, new_hash) {
|
|
|
+ store_images(rid, userId, cache_details, type, function(err, new_hash) {
|
|
|
if (err) {
|
|
|
// we might have a cached hash although an error occured
|
|
|
// (e.g. Mojang servers not reachable, using outdated hash)
|
|
|
cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
|
|
|
- callback(err2 || err, -1, details && cached_hash);
|
|
|
+ callback(err2 || err, -1, cache_details && cached_hash);
|
|
|
});
|
|
|
} else {
|
|
|
- var status = details && (cached_hash === new_hash) ? 3 : 2;
|
|
|
- logging.debug(rid + "cached hash: " + (details && cached_hash));
|
|
|
+ var status = cache_details && (cached_hash === new_hash) ? 3 : 2;
|
|
|
+ logging.debug(rid + "cached hash: " + (cache_details && cached_hash));
|
|
|
logging.log(rid + "new hash: " + new_hash);
|
|
|
callback(null, status, new_hash);
|
|
|
}
|
|
@@ -242,7 +253,7 @@ exp.get_image_hash = function(rid, userId, type, callback) {
|
|
|
|
|
|
|
|
|
// handles requests for +userId+ avatars with +size+
|
|
|
-// callback contains error, status, image buffer, skin hash
|
|
|
+// callback: error, status, image buffer, skin hash
|
|
|
// image is the user's face+helm when helm is true, or the face otherwise
|
|
|
// for status, see get_image_hash
|
|
|
exp.get_avatar = function(rid, userId, helm, size, callback) {
|
|
@@ -271,7 +282,7 @@ exp.get_avatar = function(rid, userId, helm, size, callback) {
|
|
|
};
|
|
|
|
|
|
// handles requests for +userId+ skins
|
|
|
-// callback contains error, skin hash, image buffer
|
|
|
+// callback: error, skin hash, image buffer
|
|
|
exp.get_skin = function(rid, userId, callback) {
|
|
|
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
|
|
|
var skinpath = __dirname + "/../" + config.skins_dir + skin_hash + ".png";
|
|
@@ -290,13 +301,16 @@ exp.get_skin = function(rid, userId, callback) {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+// helper method used for file names
|
|
|
+// possible returned names based on +helm+ and +body+ are:
|
|
|
+// body, bodyhelm, head, headhelm
|
|
|
function get_type(helm, body) {
|
|
|
var text = body ? "body" : "head";
|
|
|
return helm ? text + "helm" : text;
|
|
|
}
|
|
|
|
|
|
// handles creations of 3D renders
|
|
|
-// callback contains error, skin hash, image buffer
|
|
|
+// callback: error, skin hash, image buffer
|
|
|
exp.get_render = function(rid, userId, scale, helm, body, callback) {
|
|
|
exp.get_skin(rid, userId, function(err, skin_hash, img) {
|
|
|
if (!skin_hash) {
|
|
@@ -335,7 +349,7 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
|
|
|
};
|
|
|
|
|
|
// handles requests for +userId+ capes
|
|
|
-// callback contains error, cape hash, image buffer
|
|
|
+// callback: error, cape hash, image buffer
|
|
|
exp.get_cape = function(rid, userId, callback) {
|
|
|
exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
|
|
|
if (!cape_hash) {
|