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

View File

@ -18,18 +18,18 @@ function get_hash(url) {
function store_skin(rid, userId, profile, details, callback) { function store_skin(rid, userId, profile, details, callback) {
networking.get_skin_url(rid, userId, profile, function(url) { networking.get_skin_url(rid, userId, profile, function(url) {
if (url) { if (url) {
var hash = get_hash(url); var skin_hash = get_hash(url);
if (details && details.skin === hash) { if (details && details.skin === skin_hash) {
cache.update_timestamp(rid, userId, hash); cache.update_timestamp(rid, userId, skin_hash);
callback(null, hash); callback(null, skin_hash);
} else { } else {
logging.log(rid + "new skin hash: " + hash); logging.log(rid + "new skin hash: " + skin_hash);
var facepath = __dirname + "/../" + config.faces_dir + hash + ".png"; var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png";
var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png"; var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png";
fs.exists(facepath, function(exists) { fs.exists(facepath, function(exists) {
if (exists) { if (exists) {
logging.log(rid + "skin already exists, not downloading"); logging.log(rid + "skin already exists, not downloading");
callback(null, hash); callback(null, skin_hash);
} else { } else {
networking.get_from(rid, url, function(img, response, err) { networking.get_from(rid, url, function(img, response, err) {
if (err || !img) { if (err || !img) {
@ -44,7 +44,7 @@ function store_skin(rid, userId, profile, details, callback) {
skins.extract_helm(rid, facepath, img, helmpath, function(err) { skins.extract_helm(rid, facepath, img, helmpath, function(err) {
logging.log(rid + "helm extracted"); logging.log(rid + "helm extracted");
logging.debug(rid + helmpath); 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) { function store_cape(rid, userId, profile, details, callback) {
networking.get_cape_url(rid, userId, profile, function(url) { networking.get_cape_url(rid, userId, profile, function(url) {
if (url) { if (url) {
var hash = get_hash(url); var cape_hash = get_hash(url);
if (details && details.cape === hash) { if (details && details.cape === cape_hash) {
cache.update_timestamp(rid, userId, hash); cache.update_timestamp(rid, userId, cape_hash);
callback(null, hash); callback(null, cape_hash);
} else { } else {
logging.log(rid + "new cape hash: " + hash); logging.log(rid + "new cape hash: " + cape_hash);
var capepath = __dirname + "/../" + config.capes_dir + hash + ".png"; var capepath = __dirname + "/../" + config.capes_dir + cape_hash + ".png";
fs.exists(capepath, function(exists) { fs.exists(capepath, function(exists) {
if (exists) { if (exists) {
logging.log(rid + "cape already exists, not downloading"); logging.log(rid + "cape already exists, not downloading");
callback(null, hash); callback(null, cape_hash);
} else { } else {
networking.get_from(rid, url, function(img, response, err) { networking.get_from(rid, url, function(img, response, err) {
if (err || !img) { if (err || !img) {
@ -81,7 +81,7 @@ function store_cape(rid, userId, profile, details, callback) {
} else { } else {
skins.save_image(img, capepath, function(err) { skins.save_image(img, capepath, function(err) {
logging.log(rid + "cape saved"); 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+ // 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 // image is the user's face+helm when helm is true, or the face otherwise
// for status, see get_image_hash // for status, see get_image_hash
exp.get_avatar = function(rid, userId, helm, size, callback) { exp.get_avatar = function(rid, userId, helm, size, callback) {
exp.get_image_hash(rid, userId, "skin", function(err, status, hash) { exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
if (hash) { if (skin_hash) {
var facepath = __dirname + "/../" + config.faces_dir + hash + ".png"; var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png";
var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png"; var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png";
var filepath = facepath; var filepath = facepath;
fs.exists(helmpath, function(exists) { fs.exists(helmpath, function(exists) {
if (helm && 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) { skins.resize_img(filepath, size, function(img_err, result) {
if (img_err) { if (img_err) {
callback(img_err, -1, null, hash); callback(img_err, -1, null, skin_hash);
} else { } else {
// we might have a hash although an error occured // we might have a hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash) // (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 // 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) { exp.get_skin = function(rid, userId, callback) {
logging.log(rid + "skin request"); logging.log(rid + "skin request");
exp.get_image_hash(rid, userId, "skin", function(err, status, hash) { exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png"; var skinpath = __dirname + "/../" + config.skins_dir + skin_hash + ".png";
fs.exists(skinpath, function(exists) { fs.exists(skinpath, function(exists) {
if (exists) { if (exists) {
logging.log(rid + "skin already exists, not downloading"); logging.log(rid + "skin already exists, not downloading");
skins.open_skin(rid, skinpath, function(err, img) { skins.open_skin(rid, skinpath, function(err, img) {
callback(err, hash, img); callback(err, skin_hash, img);
}); });
} else { } else {
networking.save_texture(rid, hash, skinpath, function(err, response, img) { networking.save_texture(rid, skin_hash, skinpath, function(err, response, img) {
callback(err, hash, img); callback(err, skin_hash, img);
}); });
} }
}); });
@ -269,36 +269,36 @@ function get_type(helm, body) {
} }
// handles creations of skin renders // 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_render = function(rid, userId, scale, helm, body, callback) {
exp.get_skin(rid, userId, function(err, hash, img) { exp.get_skin(rid, userId, function(err, skin_hash, img) {
if (!hash) { if (!skin_hash) {
callback(err, -1, hash, null); callback(err, -1, skin_hash, null);
return; 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) { fs.exists(renderpath, function(exists) {
if (exists) { if (exists) {
renders.open_render(rid, renderpath, function(err, img) { renders.open_render(rid, renderpath, function(err, img) {
callback(err, 1, hash, img); callback(err, 1, skin_hash, img);
}); });
return; return;
} else { } else {
if (!img) { if (!img) {
callback(err, 0, hash, null); callback(err, 0, skin_hash, null);
return; return;
} }
renders.draw_model(rid, img, scale, helm, body, function(err, img) { renders.draw_model(rid, img, scale, helm, body, function(err, img) {
if (err) { if (err) {
callback(err, -1, hash, null); callback(err, -1, skin_hash, null);
} else if (!img) { } else if (!img) {
callback(null, 0, hash, null); callback(null, 0, skin_hash, null);
} else { } else {
fs.writeFile(renderpath, img, "binary", function(err) { fs.writeFile(renderpath, img, "binary", function(err) {
if (err) { if (err) {
logging.log(rid + err.stack); 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 // 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) { exp.get_cape = function(rid, userId, callback) {
logging.log(rid + "cape request"); logging.log(rid + "cape request");
exp.get_image_hash(rid, userId, "cape", function(err, status, hash) { exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
if (!hash) { if (!cape_hash) {
callback(err, null, null); callback(err, null, null);
return; return;
} }
var capepath = __dirname + "/../" + config.capes_path + hash + ".png"; var capepath = __dirname + "/../" + config.capes_path + cape_hash + ".png";
fs.exists(capepath, function(exists) { fs.exists(capepath, function(exists) {
if (exists) { if (exists) {
logging.log(rid + "cape already exists, not downloading"); logging.log(rid + "cape already exists, not downloading");
skins.open_skin(rid, capepath, function(err, img) { skins.open_skin(rid, capepath, function(err, img) {
callback(err, hash, img); callback(err, cape_hash, img);
}); });
} else { } 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) { if (response && response.statusCode === 404) {
callback(err, hash, null); callback(err, cape_hash, null);
} else { } 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 session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
var skins_url = "https://skins.minecraft.net/MinecraftSkins/"; var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
var capes_url = "https://skins.minecraft.net/MinecraftCloaks/"; var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
var textures_url = "http://textures.minecraft.net/texture/";
var mojang_urls = [skins_url, capes_url]; var mojang_urls = [skins_url, capes_url];
var exp = {}; var exp = {};
@ -50,7 +51,6 @@ exp.get_from_options = function(rid, url, options, callback) {
timeout: (options.timeout || config.http_timeout), timeout: (options.timeout || config.http_timeout),
encoding: (options.encoding || null), encoding: (options.encoding || null),
followRedirect: (options.folow_redirect || false), followRedirect: (options.folow_redirect || false),
maxAttempts: (options.max_attempts || 2)
}, function(error, response, body) { }, function(error, response, body) {
// 200 or 301 depending on content type // 200 or 301 depending on content type
if (!error && (response.statusCode === 200 || response.statusCode === 301)) { 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 // the skin url is taken from the HTTP redirect
// type reference is above // type reference is above
exp.get_username_url = function(rid, name, type, callback) { 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) { exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
if (!err) { if (!err) {
callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null); 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) { exp.save_texture = function(rid, tex_hash, outpath, callback) {
if (hash) { if (tex_hash) {
var textureurl = "http://textures.minecraft.net/texture/" + hash; var textureurl = textures_url + tex_hash;
exp.get_from(rid, textureurl, function(img, response, err) { exp.get_from(rid, textureurl, function(img, response, err) {
if (err) { if (err) {
logging.error(rid + "error while downloading texture"); logging.error(rid + "error while downloading texture");