diff --git a/modules/config.js b/modules/config.js index 4b77193..5fe20ee 100644 --- a/modules/config.js +++ b/modules/config.js @@ -2,8 +2,8 @@ var config = { min_size: 1, // < 1 will (obviously) cause crash max_size: 512, // too big values might lead to slow response time or DoS default_size: 160, // size to be used when no size given - local_cache_time: 3600, // seconds until we will check if the image changed. should be > 60 to prevent mojang 429 response - browser_cache_time: 3600, // seconds until browser will request image again + local_cache_time: 30, // seconds until we will check if the image changed. should be > 60 to prevent mojang 429 response + browser_cache_time: 30, // seconds until browser will request image again http_timeout: 3000, // ms until connection to mojang is dropped faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/' helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/' diff --git a/modules/helpers.js b/modules/helpers.js index 7ce43c9..a2599bd 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -35,11 +35,12 @@ 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, extract face/helm - networking.skin_file(skin_url, facepath, function(err, img) { - if (err) { + // 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); @@ -148,7 +149,7 @@ exp.get_skin = function(uuid, callback) { exp.get_image_hash(uuid, function(err, status, hash) { if (hash) { var skinurl = "http://textures.minecraft.net/texture/" + hash; - networking.skin_file(skinurl, null, function(err, img) { + networking.get_skin(skinurl, null, function(err, img) { if (err) { logging.log("\nerror while downloading skin"); callback(err, hash, null); diff --git a/modules/networking.js b/modules/networking.js index 39d02fa..a08ec6a 100644 --- a/modules/networking.js +++ b/modules/networking.js @@ -99,15 +99,8 @@ exp.get_skin_url = function(uuid, callback) { }; // downloads skin file from +url+ -// stores face image as +facename+ -// stores helm image as +helmname+ -// callback contains error -exp.skin_file = function(url, facename, callback) { - if (facename && fs.existsSync(facename)) { - logging.log("Images already exist, not downloading."); - callback(null, null); - return; - } +// callback contains error, image +exp.get_skin = function(url, callback) { request.get({ url: url, encoding: null, // encoding must be null so we get a buffer diff --git a/test/test.js b/test/test.js index 6119d31..6494e3c 100644 --- a/test/test.js +++ b/test/test.js @@ -23,6 +23,9 @@ var username = usernames[Math.round(Math.random() * (usernames.length - 1))]; console.log("using username '" + username + "'"); describe('Crafatar', function() { + // we might have to make 2 HTTP requests + this.timeout(config.http_timeout * 2 + 50); + before(function() { cache.get_redis().flushall(); }); @@ -131,10 +134,15 @@ describe('Crafatar', function() { }); }); 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.skin_file("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", "face.png", function(err) { + 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"); @@ -189,7 +197,7 @@ describe('Crafatar', function() { it("should time out on skin download", function(done) { var original_timeout = config.http_timeout; config.http_timeout = 1; - networking.skin_file("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", "face.png", function(err) { + networking.get_skin("http://textures.minecraft.net/texture/477be35554684c28bdeee4cf11c591d3c88afb77e0b98da893fd7bc318c65184", function(err, img) { assert.strictEqual(err.code, "ETIMEDOUT"); config.http_timeout = original_timeout; done(); @@ -197,7 +205,7 @@ describe('Crafatar', function() { }); it("should not find the skin", function(done) { assert.doesNotThrow(function() { - networking.skin_file("http://textures.minecraft.net/texture/this-does-not-exist", "face.png", function(err) { + networking.get_skin("http://textures.minecraft.net/texture/this-does-not-exist", function(err, img) { assert.strictEqual(err, null); // no error here, but it shouldn't throw exceptions done(); }); @@ -210,4 +218,13 @@ 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){} + }); + });