mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
re-add checking for existing file before downloading
This commit is contained in:
parent
8cea2ed65f
commit
e01cff53b2
@ -35,26 +35,34 @@ function store_images(uuid, details, callback) {
|
|||||||
logging.log(uuid + " new hash: " + hash);
|
logging.log(uuid + " new hash: " + hash);
|
||||||
var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
|
var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
|
||||||
var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
|
var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
|
||||||
// download skin
|
|
||||||
networking.get_skin(skin_url, function(err, img) {
|
if (fs.existsSync(facepath)) {
|
||||||
if (err || !img) {
|
logging.log(uuid + " Avatar already exists, not downloading");
|
||||||
callback(err, null);
|
callback(null, hash);
|
||||||
} else {
|
} else {
|
||||||
// extract face / helm
|
// download skin
|
||||||
skins.extract_face(img, facepath, function(err) {
|
networking.get_skin(skin_url, function(err, img) {
|
||||||
if (err) {
|
if (err || !img) {
|
||||||
callback(err);
|
callback(err, null);
|
||||||
} else {
|
} else {
|
||||||
logging.log(facepath + " face extracted");
|
// extract face / helm
|
||||||
skins.extract_helm(facepath, img, helmpath, function(err) {
|
skins.extract_face(img, facepath, function(err) {
|
||||||
logging.log(helmpath + " helm extracted.");
|
if (err) {
|
||||||
cache.save_hash(uuid, hash);
|
callback(err);
|
||||||
callback(err, hash);
|
} 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 {
|
} else {
|
||||||
// profile found, but has no skin
|
// 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;
|
module.exports = exp;
|
||||||
@ -108,7 +108,8 @@ exp.get_skin = function(url, callback) {
|
|||||||
}, function (error, response, body) {
|
}, function (error, response, body) {
|
||||||
if (!error && response.statusCode == 200) {
|
if (!error && response.statusCode == 200) {
|
||||||
// skin downloaded successfully
|
// skin downloaded successfully
|
||||||
logging.log(url + " skin downloaded");
|
logging.log("downloaded skin");
|
||||||
|
logging.debug(url);
|
||||||
callback(null, body);
|
callback(null, body);
|
||||||
} else {
|
} else {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
26
test/test.js
26
test/test.js
@ -133,23 +133,6 @@ describe('Crafatar', function() {
|
|||||||
done();
|
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) {
|
it("should default to Alex", function(done) {
|
||||||
assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex");
|
assert.strictEqual(skins.default_skin("ec561538f3fd461daff5086b22154bce"), "alex");
|
||||||
done();
|
done();
|
||||||
@ -218,13 +201,4 @@ describe('Crafatar', function() {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
after(function() {
|
|
||||||
try {
|
|
||||||
// these files could be here if a test failed
|
|
||||||
fs.unlinkSync("face.png");
|
|
||||||
fs.unlinkSync("helm.png");
|
|
||||||
} catch(e){}
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user