From e01cff53b2020c5d551fbf028bddb92b968c2b10 Mon Sep 17 00:00:00 2001 From: jomo Date: Sat, 29 Nov 2014 23:15:43 +0100 Subject: [PATCH] re-add checking for existing file before downloading --- modules/helpers.js | 66 ++++++++++++++++++------------------------- modules/networking.js | 3 +- test/test.js | 28 +----------------- 3 files changed, 31 insertions(+), 66 deletions(-) diff --git a/modules/helpers.js b/modules/helpers.js index a2599bd..1ee69cc 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -35,26 +35,34 @@ function store_images(uuid, details, callback) { logging.log(uuid + " new hash: " + hash); var facepath = __dirname + '/../' + config.faces_dir + hash + ".png"; var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png"; - // 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(facepath + " face extracted"); - skins.extract_helm(facepath, img, helmpath, function(err) { - logging.log(helmpath + " helm extracted."); - cache.save_hash(uuid, hash); - callback(err, hash); - }); - } - }); - } - }); + + if (fs.existsSync(facepath)) { + logging.log(uuid + " Avatar already exists, not downloading"); + 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); + }); + } + }); + } + }); + } } } else { // profile found, but has no skin @@ -144,22 +152,4 @@ exp.get_avatar = function(uuid, helm, size, callback) { }); }; -exp.get_skin = function(uuid, callback) { - logging.log("\nskin request: " + uuid); - exp.get_image_hash(uuid, function(err, status, hash) { - if (hash) { - var skinurl = "http://textures.minecraft.net/texture/" + hash; - networking.get_skin(skinurl, null, function(err, img) { - if (err) { - logging.log("\nerror while downloading skin"); - callback(err, hash, null); - } else { - logging.log("\nreturning skin"); - callback(null, hash, img); - } - }); - } - }); -}; - module.exports = exp; \ No newline at end of file diff --git a/modules/networking.js b/modules/networking.js index a08ec6a..3a1d4cc 100644 --- a/modules/networking.js +++ b/modules/networking.js @@ -108,7 +108,8 @@ exp.get_skin = function(url, callback) { }, function (error, response, body) { if (!error && response.statusCode == 200) { // skin downloaded successfully - logging.log(url + " skin downloaded"); + logging.log("downloaded skin"); + logging.debug(url); callback(null, body); } else { if (error) { diff --git a/test/test.js b/test/test.js index ee8b0a0..de66606 100644 --- a/test/test.js +++ b/test/test.js @@ -133,23 +133,6 @@ describe('Crafatar', function() { done(); }); }); - it("should already have the files / not download", function(done) { - - // FIXME: implement & remove this line - throw("Please re-implement this and add a new test"); - - - assert.doesNotThrow(function() { - fs.openSync("face.png", "w"); - fs.openSync("helm.png", "w"); - networking.get_skin("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(err, img) { - assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions - fs.unlinkSync("face.png"); - fs.unlinkSync("helm.png"); - done(); - }); - }); - }); it("should default to Alex", function(done) { assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex"); done(); @@ -218,13 +201,4 @@ describe('Crafatar', function() { done(); }); }); - - after(function() { - try { - // these files could be here if a test failed - fs.unlinkSync("face.png"); - fs.unlinkSync("helm.png"); - } catch(e){} - }); - -}); +}); \ No newline at end of file