Merge pull request #71 from connor4312/sync-is-evil

Swap out those syncs
This commit is contained in:
Jake 2015-01-07 15:33:10 -06:00
commit 96730705c8
2 changed files with 99 additions and 84 deletions

View File

@ -71,26 +71,31 @@ exp.run = function() {
var helmdir = __dirname + "/../" + config.helms_dir; var helmdir = __dirname + "/../" + config.helms_dir;
var renderdir = __dirname + "/../" + config.renders_dir; var renderdir = __dirname + "/../" + config.renders_dir;
var skindir = __dirname + "/../" + config.skins_dir; var skindir = __dirname + "/../" + config.skins_dir;
var files = fs.readdirSync(facesdir);
for (var i = 0; i < Math.min(files.length, config.cleaning_amount); i++) { fs.readdir(facesdir, function (err, files) {
var filename = files[i]; for (var i = 0, l = Math.min(files.length, config.cleaning_amount); i < l; i++) {
if (filename[0] != ".") { var filename = files[i];
fs.unlink(facesdir + filename, function(){}); if (filename[0] !== ".") {
fs.unlink(helmdir + filename, function(){}); fs.unlink(facesdir + filename, nil);
fs.unlink(skindir + filename, function(){}); fs.unlink(helmdir + filename, nil);
fs.unlink(skindir + filename, nil);
}
} }
} });
files = fs.readdirSync(renderdir); fs.readdir(renderdir, function (err, files) {
for (var j = 0; j < Math.min(files.length, config.cleaning_amount); j++) { for (var j = 0, l = Math.min(files.length, config.cleaning_amount); j < l; j++) {
var filename = files[j]; var filename = files[j];
if (filename[0] != ".") { if (filename[0] !== ".") {
fs.unlink(renderdir + filename, function(){}); fs.unlink(renderdir + filename, nil);
}
} }
} });
} else { } else {
logging.log("DiskCleaner: Nothing to clean"); logging.log("DiskCleaner: Nothing to clean");
} }
}); });
}; };
function nil () {}
module.exports = exp; module.exports = exp;

View File

@ -37,34 +37,36 @@ function store_images(uuid, details, callback) {
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";
if (fs.existsSync(facepath)) { fs.exists(facepath, function (exists) {
logging.log(uuid + " Avatar already exists, not downloading"); if (exists) {
cache.save_hash(uuid, hash); logging.log(uuid + " Avatar already exists, not downloading");
callback(null, hash); cache.save_hash(uuid, hash);
} else { callback(null, hash);
// download skin } else {
networking.get_skin(skin_url, function(err, img) { // download skin
if (err || !img) { networking.get_skin(skin_url, function(err, img) {
callback(err, null); if (err || !img) {
} else { callback(err, null);
// extract face / helm } else {
skins.extract_face(img, facepath, function(err) { // extract face / helm
if (err) { skins.extract_face(img, facepath, function(err) {
callback(err); if (err) {
} else { callback(err);
logging.log(uuid + " face extracted"); } else {
logging.debug(facepath); logging.log(uuid + " face extracted");
skins.extract_helm(facepath, img, helmpath, function(err) { logging.debug(facepath);
logging.log(uuid + " helm extracted"); skins.extract_helm(facepath, img, helmpath, function(err) {
logging.debug(helmpath); logging.log(uuid + " helm extracted");
cache.save_hash(uuid, hash); logging.debug(helmpath);
callback(err, hash); cache.save_hash(uuid, hash);
}); callback(err, hash);
} });
}); }
} });
}); }
} });
}
});
} }
} else { } else {
// profile found, but has no skin // profile found, but has no skin
@ -137,17 +139,21 @@ exp.get_avatar = function(uuid, helm, size, callback) {
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";
var filepath = facepath; var filepath = facepath;
if (helm && fs.existsSync(helmpath)) {
filepath = helmpath; fs.exists(helmpath, function (exists) {
} if (helm && exists) {
skins.resize_img(filepath, size, function(img_err, result) { filepath = helmpath;
if (img_err) {
callback(img_err, -1, null, hash);
} else {
// we might have a hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash)
callback(err, (err ? -1 : status), result, hash);
} }
skins.resize_img(filepath, size, function(img_err, result) {
if (img_err) {
callback(img_err, -1, null, hash);
} else {
// we might have a hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash)
callback(err, (err ? -1 : status), result, hash);
}
});
}); });
} else { } else {
// hash is null when uuid has no skin // hash is null when uuid has no skin
@ -162,15 +168,17 @@ exp.get_skin = function(uuid, callback) {
logging.log(uuid + " skin request"); logging.log(uuid + " skin request");
exp.get_image_hash(uuid, function(err, status, hash) { exp.get_image_hash(uuid, function(err, status, hash) {
var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png"; var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png";
if (fs.existsSync(skinpath)) { fs.exists(skinpath, function (exists) {
logging.log("skin already exists, not downloading"); if (exists) {
skins.open_skin(skinpath, function(err, img) { logging.log("skin already exists, not downloading");
callback(err, hash, img); skins.open_skin(skinpath, function(err, img) {
}); callback(err, hash, img);
return; });
} } else {
networking.save_skin(uuid, hash, skinpath, function(err, img) { networking.save_skin(uuid, hash, skinpath, function(err, img) {
callback(err, hash, img); callback(err, hash, img);
});
}
}); });
}); });
}; };
@ -190,29 +198,31 @@ exp.get_render = function(uuid, scale, helm, body, callback) {
return; return;
} }
var renderpath = __dirname + "/../" + config.renders_dir + hash + "-" + scale + "-" + get_type(helm, body) + ".png"; var renderpath = __dirname + "/../" + config.renders_dir + hash + "-" + scale + "-" + get_type(helm, body) + ".png";
if (fs.existsSync(renderpath)) { fs.exists(renderpath, function (exists) {
renders.open_render(renderpath, function(err, img) { if (exists) {
callback(err, 1, hash, img); renders.open_render(renderpath, function(err, img) {
}); callback(err, 1, hash, img);
return;
}
if (!img) {
callback(err, 0, hash, null);
return;
}
renders.draw_model(uuid, img, scale, helm, body, function(err, img) {
if (err) {
callback(err, -1, hash, null);
} else if (!img) {
callback(null, 0, hash, null);
} else {
fs.writeFile(renderpath, img, "binary", function(err){
if (err) {
logging.log(err);
}
callback(null, 2, hash, img);
}); });
return;
} }
if (!img) {
callback(err, 0, hash, null);
return;
}
renders.draw_model(uuid, img, scale, helm, body, function(err, img) {
if (err) {
callback(err, -1, hash, null);
} else if (!img) {
callback(null, 0, hash, null);
} else {
fs.writeFile(renderpath, img, "binary", function(err){
if (err) {
logging.log(err);
}
callback(null, 2, hash, img);
});
}
});
}); });
}); });
}; };