From 9affb0057676f51dc42aa74aa9d55faa90aafd44 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 4 Jan 2015 05:03:45 +0100 Subject: [PATCH] this is a better fix for #48 --- modules/helpers.js | 85 ++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 48 deletions(-) diff --git a/modules/helpers.js b/modules/helpers.js index 1b629f0..42f06af 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -24,59 +24,48 @@ function store_images(uuid, details, callback) { } else { if (skin_url) { logging.log(uuid + " " + skin_url); - var hash = get_hash(skin_url); // set file paths - var facepath = __dirname + "/../" + config.faces_dir + hash + ".png"; - var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png"; + var hash = get_hash(skin_url); + if (details && details.hash == hash) { + // hash hasn't changed + logging.log(uuid + " hash has not changed"); + cache.update_timestamp(uuid, hash); + callback(null, hash); + } else { + // hash has changed + logging.log(uuid + " new hash: " + hash); + var facepath = __dirname + "/../" + config.faces_dir + hash + ".png"; + var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png"; - // make sure the file is still there - fs.exists(facepath, function(err, face_exists) { - if (err) { - logging.error(uuid + "error: " + err); - } - var exists = !err && face_exists; - - if (exists && details && details.hash == hash) { - logging.log(uuid + " hash has not changed"); - cache.update_timestamp(uuid, hash); + if (fs.existsSync(facepath)) { + logging.log(uuid + " Avatar already exists, not downloading"); + cache.save_hash(uuid, hash); callback(null, hash); } else { - if (details && details.hash && !exists) { - logging.warn(uuid + " File was deleted! Downloading again."); - } else { - // hash has changed - logging.log(uuid + " new hash: " + hash); - } - if (exists) { - logging.log(uuid + " Avatar already exists, not downloading"); - cache.save_hash(uuid, hash); - callback(null, hash); - } else { - // download skin - networking.get_skin(skin_url, function(err, img) { - if (err || !img) { - callback(err, null); - } else { - // extract face / helm - skins.extract_face(img, facepath, function(err) { - if (err) { - callback(err); - } else { - logging.log(uuid + " face extracted"); - logging.debug(facepath); - skins.extract_helm(facepath, img, helmpath, function(err) { - logging.log(uuid + " helm extracted"); - logging.debug(helmpath); - cache.save_hash(uuid, hash); - callback(err, hash); - }); - } - }); - } - }); - } + // download skin + networking.get_skin(skin_url, function(err, img) { + if (err || !img) { + callback(err, null); + } else { + // extract face / helm + skins.extract_face(img, facepath, function(err) { + if (err) { + callback(err); + } else { + logging.log(uuid + " face extracted"); + logging.debug(facepath); + skins.extract_helm(facepath, img, helmpath, function(err) { + logging.log(uuid + " helm extracted"); + logging.debug(helmpath); + cache.save_hash(uuid, hash); + callback(err, hash); + }); + } + }); + } + }); } - }); + } } else { // profile found, but has no skin cache.save_hash(uuid, null);