Rename hash vars to something that makes more sense in the specific context

This commit is contained in:
Jake 2015-02-02 17:47:22 -06:00
parent 1ea94355ae
commit 103519637f
3 changed files with 62 additions and 63 deletions

View File

@ -35,11 +35,11 @@ function connect_redis() {
});
}
// sets the date of the face file belonging to +hash+ to now
// sets the date of the face file belonging to +skin_hash+ to now
// the helms file is ignored because we only need 1 file to read/write from
function update_file_date(rid, hash) {
if (hash) {
var path = config.faces_dir + hash + ".png";
function update_file_date(rid, skin_hash) {
if (skin_hash) {
var path = config.faces_dir + skin_hash + ".png";
fs.exists(path, function(exists) {
if (exists) {
var date = new Date();
@ -102,17 +102,17 @@ exp.update_timestamp = function(rid, userId, hash) {
update_file_date(rid, hash);
};
// create the key +userId+, store +skin+ hash, +cape+ hash and time
exp.save_hash = function(rid, userId, skin, cape) {
// create the key +userId+, store +skin_hash+ hash, +cape_hash+ hash and time
exp.save_hash = function(rid, userId, skin_hash, cape_hash) {
logging.log(rid + "cache: saving hash");
logging.log(rid + "skin:" + skin + " cape:" + cape);
logging.log(rid + "skin:" + skin_hash + " cape:" + cape_hash);
var time = new Date().getTime();
// store shorter null byte instead of "null"
skin = skin || ".";
cape = cape || ".";
skin_hash = skin_hash || ".";
cape_hash = cape_hash || ".";
// store userId in lower case if not null
userId = userId && userId.toLowerCase();
redis.hmset(userId, "s", skin, "c", cape, "t", time);
redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time);
};
exp.remove_hash = function(rid, userId) {

View File

@ -18,18 +18,18 @@ function get_hash(url) {
function store_skin(rid, userId, profile, details, callback) {
networking.get_skin_url(rid, userId, profile, function(url) {
if (url) {
var hash = get_hash(url);
if (details && details.skin === hash) {
cache.update_timestamp(rid, userId, hash);
callback(null, hash);
var skin_hash = get_hash(url);
if (details && details.skin === skin_hash) {
cache.update_timestamp(rid, userId, skin_hash);
callback(null, skin_hash);
} else {
logging.log(rid + "new skin hash: " + hash);
var facepath = __dirname + "/../" + config.faces_dir + hash + ".png";
var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png";
logging.log(rid + "new skin hash: " + skin_hash);
var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png";
var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png";
fs.exists(facepath, function(exists) {
if (exists) {
logging.log(rid + "skin already exists, not downloading");
callback(null, hash);
callback(null, skin_hash);
} else {
networking.get_from(rid, url, function(img, response, err) {
if (err || !img) {
@ -44,7 +44,7 @@ function store_skin(rid, userId, profile, details, callback) {
skins.extract_helm(rid, facepath, img, helmpath, function(err) {
logging.log(rid + "helm extracted");
logging.debug(rid + helmpath);
callback(err, hash);
callback(err, skin_hash);
});
}
});
@ -62,17 +62,17 @@ function store_skin(rid, userId, profile, details, callback) {
function store_cape(rid, userId, profile, details, callback) {
networking.get_cape_url(rid, userId, profile, function(url) {
if (url) {
var hash = get_hash(url);
if (details && details.cape === hash) {
cache.update_timestamp(rid, userId, hash);
callback(null, hash);
var cape_hash = get_hash(url);
if (details && details.cape === cape_hash) {
cache.update_timestamp(rid, userId, cape_hash);
callback(null, cape_hash);
} else {
logging.log(rid + "new cape hash: " + hash);
var capepath = __dirname + "/../" + config.capes_dir + hash + ".png";
logging.log(rid + "new cape hash: " + cape_hash);
var capepath = __dirname + "/../" + config.capes_dir + cape_hash + ".png";
fs.exists(capepath, function(exists) {
if (exists) {
logging.log(rid + "cape already exists, not downloading");
callback(null, hash);
callback(null, cape_hash);
} else {
networking.get_from(rid, url, function(img, response, err) {
if (err || !img) {
@ -81,7 +81,7 @@ function store_cape(rid, userId, profile, details, callback) {
} else {
skins.save_image(img, capepath, function(err) {
logging.log(rid + "cape saved");
callback(err, hash);
callback(err, cape_hash);
});
}
});
@ -212,14 +212,14 @@ exp.get_image_hash = function(rid, userId, type, callback) {
// handles requests for +userId+ avatars with +size+
// callback contains error, status, image buffer, hash
// callback contains 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) {
exp.get_image_hash(rid, userId, "skin", function(err, status, hash) {
if (hash) {
var facepath = __dirname + "/../" + config.faces_dir + hash + ".png";
var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png";
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
if (skin_hash) {
var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png";
var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png";
var filepath = facepath;
fs.exists(helmpath, function(exists) {
if (helm && exists) {
@ -227,11 +227,11 @@ exp.get_avatar = function(rid, userId, helm, size, callback) {
}
skins.resize_img(filepath, size, function(img_err, result) {
if (img_err) {
callback(img_err, -1, null, hash);
callback(img_err, -1, null, skin_hash);
} else {
// we might have a hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash)
callback(err, (err ? -1 : status), result, hash);
callback(err, (err ? -1 : status), result, skin_hash);
}
});
});
@ -243,20 +243,20 @@ exp.get_avatar = function(rid, userId, helm, size, callback) {
};
// handles requests for +userId+ skins
// callback contains error, hash, image buffer
// callback contains error, skin hash, image buffer
exp.get_skin = function(rid, userId, callback) {
logging.log(rid + "skin request");
exp.get_image_hash(rid, userId, "skin", function(err, status, hash) {
var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png";
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
var skinpath = __dirname + "/../" + config.skins_dir + skin_hash + ".png";
fs.exists(skinpath, function(exists) {
if (exists) {
logging.log(rid + "skin already exists, not downloading");
skins.open_skin(rid, skinpath, function(err, img) {
callback(err, hash, img);
callback(err, skin_hash, img);
});
} else {
networking.save_texture(rid, hash, skinpath, function(err, response, img) {
callback(err, hash, img);
networking.save_texture(rid, skin_hash, skinpath, function(err, response, img) {
callback(err, skin_hash, img);
});
}
});
@ -269,36 +269,36 @@ function get_type(helm, body) {
}
// handles creations of skin renders
// callback contanis error, hash, image buffer
// callback contains error, skin hash, image buffer
exp.get_render = function(rid, userId, scale, helm, body, callback) {
exp.get_skin(rid, userId, function(err, hash, img) {
if (!hash) {
callback(err, -1, hash, null);
exp.get_skin(rid, userId, function(err, skin_hash, img) {
if (!skin_hash) {
callback(err, -1, skin_hash, null);
return;
}
var renderpath = __dirname + "/../" + config.renders_dir + hash + "-" + scale + "-" + get_type(helm, body) + ".png";
var renderpath = __dirname + "/../" + config.renders_dir + skin_hash + "-" + scale + "-" + get_type(helm, body) + ".png";
fs.exists(renderpath, function(exists) {
if (exists) {
renders.open_render(rid, renderpath, function(err, img) {
callback(err, 1, hash, img);
callback(err, 1, skin_hash, img);
});
return;
} else {
if (!img) {
callback(err, 0, hash, null);
callback(err, 0, skin_hash, null);
return;
}
renders.draw_model(rid, img, scale, helm, body, function(err, img) {
if (err) {
callback(err, -1, hash, null);
callback(err, -1, skin_hash, null);
} else if (!img) {
callback(null, 0, hash, null);
callback(null, 0, skin_hash, null);
} else {
fs.writeFile(renderpath, img, "binary", function(err) {
if (err) {
logging.log(rid + err.stack);
}
callback(null, 2, hash, img);
callback(null, 2, skin_hash, img);
});
}
});
@ -308,27 +308,27 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
};
// handles requests for +userId+ capes
// callback contains error, hash, image buffer
// callback contains error, cape hash, image buffer
exp.get_cape = function(rid, userId, callback) {
logging.log(rid + "cape request");
exp.get_image_hash(rid, userId, "cape", function(err, status, hash) {
if (!hash) {
exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
if (!cape_hash) {
callback(err, null, null);
return;
}
var capepath = __dirname + "/../" + config.capes_path + hash + ".png";
var capepath = __dirname + "/../" + config.capes_path + cape_hash + ".png";
fs.exists(capepath, function(exists) {
if (exists) {
logging.log(rid + "cape already exists, not downloading");
skins.open_skin(rid, capepath, function(err, img) {
callback(err, hash, img);
callback(err, cape_hash, img);
});
} else {
networking.save_texture(rid, hash, capepath, function(err, response, img) {
networking.save_texture(rid, cape_hash, capepath, function(err, response, img) {
if (response && response.statusCode === 404) {
callback(err, hash, null);
callback(err, cape_hash, null);
} else {
callback(err, hash, img);
callback(err, cape_hash, img);
}
});
}

View File

@ -6,6 +6,7 @@ var fs = require("fs");
var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
var textures_url = "http://textures.minecraft.net/texture/";
var mojang_urls = [skins_url, capes_url];
var exp = {};
@ -50,7 +51,6 @@ exp.get_from_options = function(rid, url, options, callback) {
timeout: (options.timeout || config.http_timeout),
encoding: (options.encoding || null),
followRedirect: (options.folow_redirect || false),
maxAttempts: (options.max_attempts || 2)
}, function(error, response, body) {
// 200 or 301 depending on content type
if (!error && (response.statusCode === 200 || response.statusCode === 301)) {
@ -86,7 +86,6 @@ exp.get_from = function(rid, url, callback) {
// the skin url is taken from the HTTP redirect
// type reference is above
exp.get_username_url = function(rid, name, type, callback) {
console.log(mojang_urls[type])
exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
if (!err) {
callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null);
@ -151,9 +150,9 @@ function get_url(rid, userId, profile, type, callback) {
}
}
exp.save_texture = function(rid, hash, outpath, callback) {
if (hash) {
var textureurl = "http://textures.minecraft.net/texture/" + hash;
exp.save_texture = function(rid, tex_hash, outpath, callback) {
if (tex_hash) {
var textureurl = textures_url + tex_hash;
exp.get_from(rid, textureurl, function(img, response, err) {
if (err) {
logging.error(rid + "error while downloading texture");