do not extract face on every request, store face only

This commit is contained in:
jomo 2014-10-19 23:28:21 +02:00
parent 48d33eae25
commit 81c51cdbbe
3 changed files with 41 additions and 23 deletions

View File

@ -7,27 +7,22 @@ var valid_uuid = /^[0-9a-f]{32}$/;
/* GET home page. */ /* GET home page. */
router.get('/:uuid/:size?', function(req, res) { router.get('/:uuid/:size?', function(req, res) {
//res.render('index', { title: 'Express' });
//res.send("uuid is set to " + req.param("uuid"));
//console.log(req.param('size'))
var uuid = req.param('uuid'); var uuid = req.param('uuid');
var size = req.param('size') || 180; var size = req.param('size') || 180;
console.log(uuid); console.log(uuid);
if (valid_uuid.test(uuid)) { if (valid_uuid.test(uuid)) {
var filename = 'skins/' + uuid + ".png"; var filename = uuid + ".png";
if (fs.existsSync(filename)) { if (fs.existsSync("skins/" + filename)) {
skins.extract_face(filename, size, function() { skins.resize_img(filename, size, function(data) {
skins.extract_face(filename, size, function(data) { res.writeHead(200, {'Content-Type': 'image/png'});
res.writeHead(200, {'Content-Type': 'image/png'}); res.end(data);
res.end(data);
});
}); });
} else { } else {
skins.get_profile(uuid, function(profile) { skins.get_profile(uuid, function(profile) {
var skinurl = skins.skin_url(profile); var skinurl = skins.skin_url(profile);
if (skinurl) { if (skinurl) {
skins.skin_file(skinurl, filename, function() { skins.skin_file(skinurl, filename, function() {
skins.extract_face(filename, size, function(data) { skins.resize_img(filename, size, function(data) {
res.writeHead(200, {'Content-Type': 'image/png'}); res.writeHead(200, {'Content-Type': 'image/png'});
res.end(data); res.end(data);
}); });

View File

@ -8,6 +8,18 @@ var lwip = require('lwip');
* Skin retrieval methods are based on @jomo's CLI Crafatar implementation. * Skin retrieval methods are based on @jomo's CLI Crafatar implementation.
* https://github.com/jomo/Crafatar * https://github.com/jomo/Crafatar
*/ */
function extract_face(inname, outname, callback) {
var outfile = fs.createWriteStream(outname);
lwip.open(inname, function(err, image) {
image.batch()
.crop(8, 8, 15, 15)
.writeFile(outname, function(err) {
callback();
});
});
}
module.exports = { module.exports = {
get_profile: function(uuid, callback) { get_profile: function(uuid, callback) {
https.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid, function(res) { https.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid, function(res) {
@ -17,8 +29,12 @@ module.exports = {
} }
res.on('data', function(d) { res.on('data', function(d) {
var profile = JSON.parse(d); var profile = JSON.parse(d);
if (profile.error) callback(null); if (profile.error) {
callback(profile); console.error(profile.error);
callback(null);
} else {
callback(profile);
}
}); });
}).on('error', function(e) { }).on('error', function(e) {
@ -41,22 +57,29 @@ module.exports = {
}, },
skin_file: function(url, filename, callback) { skin_file: function(url, filename, callback) {
var file = fs.createWriteStream(filename); var tmpname = "skins/tmp/" + filename;
var outname = "skins/" + filename;
var tmpfile = fs.createWriteStream(tmpname);
http.get(url, function(res) { http.get(url, function(res) {
res.on('data', function(data) { res.on('data', function(data) {
file.write(data); tmpfile.write(data);
}).on('end', function() { }).on('end', function() {
file.end(); tmpfile.end();
callback(); extract_face(tmpname, outname, function() {
fs.unlink(tmpname, function(err) { // unlink = delete
if (err) console.error(err);
});
callback(); // outside unlink callback cause we don't have to wait until it's deleted
});
}); });
}); });
}, },
extract_face: function(infile, size, callback) {
lwip.open(infile, function(err, image){ resize_img: function(inname, size, callback) {
lwip.open("skins/" + inname, function(err, image) {
image.batch() image.batch()
.crop(8,8,15,15) .resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
.resize(size, size, "nearest-neighbor") .toBuffer('png', function(err, buffer) {
.toBuffer('png', function(err, buffer){
callback(buffer); callback(buffer);
}); });
}); });

0
skins/tmp/.gitkeep Normal file
View File