Cache caching for skins and renders'

This commit is contained in:
Jake 2014-12-06 00:18:41 -06:00
parent 4bcd9e4530
commit 1c33119e05
9 changed files with 86 additions and 24 deletions

View File

@ -10,6 +10,8 @@ var config = {
http_timeout: 1000, // ms until connection to mojang is dropped http_timeout: 1000, // ms until connection to mojang is dropped
faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/' faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/'
helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/' helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/'
skins_dir: 'skins/skins/', // directory where skins are kept. should have trailing '/'
renders_dir: 'skins/renders', // Directory where rendered skins are kept. should have trailing '/'
debug_enabled: false, // enables logging.debug debug_enabled: false, // enables logging.debug
default_scale: 6, // the scale of rendered avatars default_scale: 6, // the scale of rendered avatars
maximum_sale: 10 // the maximum scale of rendered avatars maximum_sale: 10 // the maximum scale of rendered avatars

View File

@ -8,9 +8,10 @@ var config = {
cleaning_interval: 1800, // seconds interval: deleting images if disk size at limit cleaning_interval: 1800, // seconds interval: deleting images if disk size at limit
cleaning_limit: 10240, // minumum required available KB on disk to trigger cleaning cleaning_limit: 10240, // minumum required available KB on disk to trigger cleaning
cleaning_amount: 50000, // amount of avatar (and their helm) files to clean cleaning_amount: 50000, // amount of avatar (and their helm) files to clean
http_timeout: 1000, // ms until connection to mojang is dropped
faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/' faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/'
helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/' helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/'
skins_dir: 'skins/skins/', // directory where skins are kept. should have trailing '/'
renders_dir: 'skins/renders/', // Directory where rendered skins are kept. should have trailing '/'
debug_enabled: true, // enables logging.debug debug_enabled: true, // enables logging.debug
default_scale: 6, // the scale of rendered avatars default_scale: 6, // the scale of rendered avatars
maximum_sale: 10 // the maximum scale of rendered avatars maximum_sale: 10 // the maximum scale of rendered avatars

View File

@ -160,19 +160,17 @@ exp.get_avatar = function(uuid, helm, size, callback) {
exp.get_skin = function(uuid, callback) { 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) {
if (hash) { var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png";
var skinurl = "http://textures.minecraft.net/texture/" + hash; if (fs.existsSync(skinpath)) {
networking.get_skin(skinurl, function(err, img) { logging.log("skin already exists, not downloading");
if (err) { skins.open_skin(hash, function(err, img) {
logging.error("error while downloading skin"); callback(err, hash, img);
callback(err, hash, null);
} else {
callback(null, hash, img);
}
}); });
} else { return;
callback(err, null, null);
} }
networking.save_skin(uuid, hash, skinpath, function(err, img) {
callback(err, hash, img);
});
}); });
}; };
@ -180,7 +178,19 @@ exp.get_skin = function(uuid, callback) {
// callback contanis error, hash, image buffer // callback contanis error, hash, image buffer
exp.get_render = function(uuid, scale, helm, body, callback) { exp.get_render = function(uuid, scale, helm, body, callback) {
logging.log(uuid + " render request"); logging.log(uuid + " render request");
exp.get_image_hash(uuid, function(err, status, hash) {
exp.get_skin(uuid, function(err, hash, img) { exp.get_skin(uuid, function(err, hash, img) {
if (!hash) {
callback(err, -1, hash, null);
return;
}
var renderpath = __dirname + "/../" + config.renders_dir + hash + "-" + scale + ".png";
if (fs.existsSync(renderpath)) {
renders.open_render(hash, scale, function(err, img) {
callback(err, 1, hash, img);
});
return;
}
if (!img) { if (!img) {
callback(err, 0, hash, null); callback(err, 0, hash, null);
return; return;
@ -191,8 +201,14 @@ exp.get_render = function(uuid, scale, helm, body, callback) {
} else if (!img) { } else if (!img) {
callback(null, 0, hash, null); callback(null, 0, hash, null);
} else { } else {
callback(null, 2, hash, img); fs.writeFile(renderpath, img, 'binary', function(err){
if (err) {
logging.log(err);
} }
callback(null, 2, hash, img);
});
}
});
}); });
}); });
}; };

View File

@ -141,4 +141,25 @@ exp.get_skin = function(url, callback) {
}); });
}; };
exp.save_skin = function(uuid, hash, outpath, callback) {
if (hash) {
var skinurl = "http://textures.minecraft.net/texture/" + hash;
exp.get_skin(skinurl, function(err, img) {
if (err) {
logging.error("error while downloading skin");
callback(err, null);
} else {
fs.writeFile(outpath, img, 'binary', function(err){
if (err) {
logging.log(err);
}
callback(null, img);
})
}
});
} else {
callback(null, null);
}
};
module.exports = exp; module.exports = exp;

View File

@ -4,6 +4,8 @@
var helpers = require('./helpers'); var helpers = require('./helpers');
var logging = require('./logging'); var logging = require('./logging');
var config = require('./config');
var fs = require('fs');
var exp = {}; var exp = {};
@ -118,6 +120,15 @@ exp.draw_model = function(uuid, img, scale, helm, body, callback) {
image.src = img; image.src = img;
} }
exp.open_render = function(hash, scale, callback) {
fs.readFile(__dirname + "/../" + config.renders_dir + hash + "-" + scale + ".png", function (err, buf) {
if (err) {
logging.error("error while opening skin file: " + err);
}
callback(err, buf);
});
};
function scale_image(imageData, context, d_x, d_y, scale) { function scale_image(imageData, context, d_x, d_y, scale) {
var width = imageData.width; var width = imageData.width;
var height = imageData.height; var height = imageData.height;

View File

@ -1,6 +1,7 @@
var logging = require("./logging"); var logging = require("./logging");
var lwip = require("lwip"); var lwip = require("lwip");
var fs = require("fs"); var fs = require("fs");
var config = require("./config");
var exp = {}; var exp = {};
@ -93,4 +94,13 @@ exp.default_skin = function(uuid) {
} }
}; };
exp.open_skin = function(hash, callback) {
fs.readFile(__dirname + "/../" + config.skins_dir + hash + ".png", function (err, buf) {
if (err) {
logging.error("error while opening skin file: " + err);
}
callback(err, buf);
});
};
module.exports = exp; module.exports = exp;

0
skins/renders/.gitkeep Normal file
View File

0
skins/skins/.gitkeep Normal file
View File

View File

@ -84,7 +84,8 @@ block content
| #{domain}/renders/body/ | #{domain}/renders/body/
mark.green id mark.green id
| The <b>default</b> parameter can also be used here. Using alex or steve will create a | The <b>default</b> parameter can also be used here. Using alex or steve will create a
| render with the same parameters. A custom image will not be rendered. | render with the same parameters. A custom image will not be rendered. A UUID or username
| without a skin, will produce a render based on the input id, or the <b>default</b> parameter.
| Using the <b>helm</b> parameter is also allowed, which will be overlayed onto the head. | Using the <b>helm</b> parameter is also allowed, which will be overlayed onto the head.
| The <b>head</b> render type will return only a render of the skin's head, while the | The <b>head</b> render type will return only a render of the skin's head, while the
| <b>body</b> render will return a render of the entire skin. | <b>body</b> render will return a render of the entire skin.