❤️ eslint

This commit is contained in:
jomo 2015-03-27 23:12:44 +01:00
parent 7520957b93
commit af03fb63f8
16 changed files with 386 additions and 365 deletions

View File

@ -98,7 +98,7 @@ exp.info = function(callback) {
// callback: error // callback: error
exp.update_timestamp = function(rid, userId, hash, temp, callback) { exp.update_timestamp = function(rid, userId, hash, temp, callback) {
logging.log(rid, "cache: updating timestamp"); logging.log(rid, "cache: updating timestamp");
sub = temp ? (config.local_cache_time - 60) : 0; var sub = temp ? (config.local_cache_time - 60) : 0;
var time = new Date().getTime() - sub; var time = new Date().getTime() - sub;
// store userId in lower case if not null // store userId in lower case if not null
userId = userId && userId.toLowerCase(); userId = userId && userId.toLowerCase();

View File

@ -1,12 +1,16 @@
var logging = require("./logging"); var logging = require("./logging");
var config = require("./config"); var config = require("./config");
var cache = require("./cache"); var cache = require("./cache");
var path = require("path");
var df = require("node-df"); var df = require("node-df");
var fs = require("fs"); var fs = require("fs");
var redis = cache.get_redis(); var redis = cache.get_redis();
var exp = {}; var exp = {};
// does nothing
function nil() {}
// compares redis' used_memory with cleaning_redis_limit // compares redis' used_memory with cleaning_redis_limit
// callback: error, true|false // callback: error, true|false
function should_clean_redis(callback) { function should_clean_redis(callback) {
@ -31,7 +35,7 @@ function should_clean_redis(callback) {
// callback: error, true|false // callback: error, true|false
function should_clean_disk(callback) { function should_clean_disk(callback) {
df({ df({
file: __dirname + "/../" + config.faces_dir, file: path.join(__dirname, "..", config.faces_dir),
prefixMultiplier: "KiB", prefixMultiplier: "KiB",
isDisplayPrefixMultiplier: false, isDisplayPrefixMultiplier: false,
precision: 2 precision: 2
@ -67,27 +71,31 @@ exp.run = function() {
logging.error(err); logging.error(err);
} else if (clean) { } else if (clean) {
logging.warn("DiskCleaner: Disk limit reached! Cleaning images now"); logging.warn("DiskCleaner: Disk limit reached! Cleaning images now");
var facesdir = __dirname + "/../" + config.faces_dir; var facesdir = path.join(__dirname, "..", config.faces_dir);
var helmdir = __dirname + "/../" + config.helms_dir; var helmdir = path.join(__dirname, "..", config.helms_dir);
var renderdir = __dirname + "/../" + config.renders_dir; var renderdir = path.join(__dirname, "..", config.renders_dir);
var skindir = __dirname + "/../" + config.skins_dir; var skindir = path.join(__dirname, "..", config.skins_dir);
fs.readdir(facesdir, function (err, files) { fs.readdir(facesdir, function (readerr, files) {
if (!readerr) {
for (var i = 0, l = Math.min(files.length, config.cleaning_amount); i < l; i++) { for (var i = 0, l = Math.min(files.length, config.cleaning_amount); i < l; i++) {
var filename = files[i]; var filename = files[i];
if (filename[0] !== ".") { if (filename[0] !== ".") {
fs.unlink(facesdir + filename, nil); fs.unlink(path.join(facesdir, filename), nil);
fs.unlink(helmdir + filename, nil); fs.unlink(path.join(helmdir, filename), nil);
fs.unlink(skindir + filename, nil); fs.unlink(path.join(skindir, filename), nil);
}
} }
} }
}); });
fs.readdir(renderdir, function (err, files) { fs.readdir(renderdir, function (readerr, files) {
if (!readerr) {
for (var j = 0, l = Math.min(files.length, config.cleaning_amount); j < l; 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, nil); fs.unlink(renderdir + filename, nil);
} }
} }
}
}); });
} else { } else {
logging.log("DiskCleaner: Nothing to clean"); logging.log("DiskCleaner: Nothing to clean");
@ -95,6 +103,4 @@ exp.run = function() {
}); });
}; };
function nil () {}
module.exports = exp; module.exports = exp;

View File

@ -1,9 +1,10 @@
var networking = require("./networking"); var networking = require("./networking");
var logging = require("./logging"); var logging = require("./logging");
var renders = require("./renders");
var config = require("./config"); var config = require("./config");
var cache = require("./cache"); var cache = require("./cache");
var skins = require("./skins"); var skins = require("./skins");
var renders = require("./renders"); var path = require("path");
var fs = require("fs"); var fs = require("fs");
// 0098cb60-fa8e-427c-b299-793cbd302c9a // 0098cb60-fa8e-427c-b299-793cbd302c9a
@ -24,13 +25,13 @@ function store_skin(rid, userId, profile, cache_details, callback) {
if (!err && url) { if (!err && url) {
var skin_hash = get_hash(url); var skin_hash = get_hash(url);
if (cache_details && cache_details.skin === skin_hash) { if (cache_details && cache_details.skin === skin_hash) {
cache.update_timestamp(rid, userId, skin_hash, false, function(err) { cache.update_timestamp(rid, userId, skin_hash, false, function(cache_err) {
callback(err, skin_hash); callback(cache_err, skin_hash);
}); });
} else { } else {
logging.log(rid, "new skin hash:", skin_hash); logging.log(rid, "new skin hash:", skin_hash);
var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png"; var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png"; var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
fs.exists(facepath, function(exists) { fs.exists(facepath, function(exists) {
if (exists) { if (exists) {
logging.log(rid, "skin already exists, not downloading"); logging.log(rid, "skin already exists, not downloading");
@ -73,25 +74,25 @@ function store_cape(rid, userId, profile, cache_details, callback) {
if (!err && url) { if (!err && url) {
var cape_hash = get_hash(url); var cape_hash = get_hash(url);
if (cache_details && cache_details.cape === cape_hash) { if (cache_details && cache_details.cape === cape_hash) {
cache.update_timestamp(rid, userId, cape_hash, false, function(err) { cache.update_timestamp(rid, userId, cape_hash, false, function(cache_err) {
callback(err, cape_hash); callback(cache_err, cape_hash);
}); });
} else { } else {
logging.log(rid, "new cape hash:", cape_hash); logging.log(rid, "new cape hash:", cape_hash);
var capepath = __dirname + "/../" + config.capes_dir + cape_hash + ".png"; var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
fs.exists(capepath, function(exists) { fs.exists(capepath, function(exists) {
if (exists) { if (exists) {
logging.log(rid, "cape already exists, not downloading"); logging.log(rid, "cape already exists, not downloading");
callback(null, cape_hash); callback(null, cape_hash);
} else { } else {
networking.get_from(rid, url, function(img, response, err) { networking.get_from(rid, url, function(img, response, net_err) {
if (err || !img) { if (net_err || !img) {
logging.error(rid, err.stack); logging.error(rid, net_err.stack);
callback(err, null); callback(net_err, null);
} else { } else {
skins.save_image(img, capepath, function(err) { skins.save_image(img, capepath, function(skin_err) {
logging.debug(rid, "cape saved"); logging.debug(rid, "cape saved");
callback(err, cape_hash); callback(skin_err, cape_hash);
}); });
} }
}); });
@ -171,23 +172,23 @@ function store_images(rid, userId, cache_details, type, callback) {
} }
} else { } else {
// no error and we have a profile (if it's a uuid) // no error and we have a profile (if it's a uuid)
store_skin(rid, userId, profile, cache_details, function(err, skin_hash) { store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash) {
if (err && !skin_hash) { if (store_err && !skin_hash) {
// an error occured, not caching. we can try in 60 seconds // an error occured, not caching. we can try in 60 seconds
callback_for(userId, "skin", err, null); callback_for(userId, "skin", store_err, null);
} else { } else {
cache.save_hash(rid, userId, skin_hash, null, function(cache_err) { cache.save_hash(rid, userId, skin_hash, null, function(cache_err) {
callback_for(userId, "skin", (err || cache_err), skin_hash); callback_for(userId, "skin", (store_err || cache_err), skin_hash);
}); });
} }
}); });
store_cape(rid, userId, profile, cache_details, function(err, cape_hash) { store_cape(rid, userId, profile, cache_details, function(store_err, cape_hash) {
if (err && !cape_hash) { if (store_err && !cape_hash) {
// an error occured, not caching. we can try in 60 seconds // an error occured, not caching. we can try in 60 seconds
callback_for(userId, "cape", (err), cape_hash); callback_for(userId, "cape", (store_err), cape_hash);
} else { } else {
cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) { cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
callback_for(userId, "cape", (err || cache_err), cape_hash); callback_for(userId, "cape", (store_err || cache_err), cape_hash);
}); });
} }
}); });
@ -217,7 +218,10 @@ exp.id_valid = function(userId) {
// 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin // 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
exp.get_image_hash = function(rid, userId, type, callback) { exp.get_image_hash = function(rid, userId, type, callback) {
cache.get_details(userId, function(err, cache_details) { cache.get_details(userId, function(err, cache_details) {
var cached_hash = (cache_details !== null) ? (type === "skin" ? cache_details.skin : cache_details.cape) : null; var cached_hash = null;
if (cache_details !== null) {
cached_hash = type === "skin" ? cache_details.skin : cache_details.cape;
}
if (err) { if (err) {
callback(err, -1, null); callback(err, -1, null);
} else { } else {
@ -232,12 +236,12 @@ exp.get_image_hash = function(rid, userId, type, callback) {
} else { } else {
logging.log(rid, "userId not cached"); logging.log(rid, "userId not cached");
} }
store_images(rid, userId, cache_details, type, function(err, new_hash) { store_images(rid, userId, cache_details, type, function(store_err, new_hash) {
if (err) { if (store_err) {
// we might have a cached hash although an error occured // we might have a cached hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash) // (e.g. Mojang servers not reachable, using outdated hash)
cache.update_timestamp(rid, userId, cached_hash, true, function(err2) { cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
callback(err2 || err, -1, cache_details && cached_hash); callback(err2 || store_err, -1, cache_details && cached_hash);
}); });
} else { } else {
var status = cache_details && (cached_hash === new_hash) ? 3 : 2; var status = cache_details && (cached_hash === new_hash) ? 3 : 2;
@ -259,8 +263,8 @@ exp.get_image_hash = function(rid, userId, type, callback) {
exp.get_avatar = function(rid, userId, helm, size, callback) { exp.get_avatar = function(rid, userId, helm, size, callback) {
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) { exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
if (skin_hash) { if (skin_hash) {
var facepath = __dirname + "/../" + config.faces_dir + skin_hash + ".png"; var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
var helmpath = __dirname + "/../" + config.helms_dir + skin_hash + ".png"; var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
var filepath = facepath; var filepath = facepath;
fs.exists(helmpath, function(exists) { fs.exists(helmpath, function(exists) {
if (helm && exists) { if (helm && exists) {
@ -285,16 +289,17 @@ exp.get_avatar = function(rid, userId, helm, size, callback) {
// callback: error, skin hash, image buffer // callback: error, skin hash, image buffer
exp.get_skin = function(rid, userId, callback) { exp.get_skin = function(rid, userId, callback) {
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) { exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
var skinpath = __dirname + "/../" + config.skins_dir + skin_hash + ".png"; // FIXME: err is not used / handled
var skinpath = path.join(__dirname, "..", config.skins_dir, skin_hash + ".png");
fs.exists(skinpath, function(exists) { fs.exists(skinpath, function(exists) {
if (exists) { if (exists) {
logging.log(rid, "skin already exists, not downloading"); logging.log(rid, "skin already exists, not downloading");
skins.open_skin(rid, skinpath, function(err, img) { skins.open_skin(rid, skinpath, function(skin_err, img) {
callback(err, skin_hash, img); callback(skin_err, skin_hash, img);
}); });
} else { } else {
networking.save_texture(rid, skin_hash, skinpath, function(err, response, img) { networking.save_texture(rid, skin_hash, skinpath, function(net_err, response, img) {
callback(err, skin_hash, img); callback(net_err, skin_hash, img);
}); });
} }
}); });
@ -317,11 +322,11 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
callback(err, -1, skin_hash, null); callback(err, -1, skin_hash, null);
return; return;
} }
var renderpath = __dirname + "/../" + config.renders_dir + skin_hash + "-" + scale + "-" + get_type(helm, body) + ".png"; var renderpath = path.join(__dirname, "..", config.renders_dir, [skin_hash, "-", scale, "-", get_type(helm, body)].join("-") + ".png");
fs.exists(renderpath, function(exists) { fs.exists(renderpath, function(exists) {
if (exists) { if (exists) {
renders.open_render(rid, renderpath, function(err, img) { renders.open_render(rid, renderpath, function(render_err, rendered_img) {
callback(err, 1, skin_hash, img); callback(render_err, 1, skin_hash, rendered_img);
}); });
return; return;
} else { } else {
@ -329,15 +334,15 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
callback(err, 0, skin_hash, null); callback(err, 0, skin_hash, null);
return; return;
} }
renders.draw_model(rid, img, scale, helm, body, function(err, img) { renders.draw_model(rid, img, scale, helm, body, function(draw_err, drawn_img) {
if (err) { if (draw_err) {
callback(err, -1, skin_hash, null); callback(draw_err, -1, skin_hash, null);
} else if (!img) { } else if (!drawn_img) {
callback(null, 0, skin_hash, null); callback(null, 0, skin_hash, null);
} else { } else {
fs.writeFile(renderpath, img, "binary", function(err) { fs.writeFile(renderpath, drawn_img, "binary", function(fs_err) {
if (err) { if (fs_err) {
logging.error(rid, err.stack); logging.error(rid, fs_err.stack);
} }
callback(null, 2, skin_hash, img); callback(null, 2, skin_hash, img);
}); });
@ -356,19 +361,19 @@ exp.get_cape = function(rid, userId, callback) {
callback(err, null, null); callback(err, null, null);
return; return;
} }
var capepath = __dirname + "/../" + config.capes_dir + cape_hash + ".png"; var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
fs.exists(capepath, function(exists) { fs.exists(capepath, function(exists) {
if (exists) { if (exists) {
logging.log(rid, "cape already exists, not downloading"); logging.log(rid, "cape already exists, not downloading");
skins.open_skin(rid, capepath, function(err, img) { skins.open_skin(rid, capepath, function(skin_err, img) {
callback(err, cape_hash, img); callback(skin_err, cape_hash, img);
}); });
} else { } else {
networking.save_texture(rid, cape_hash, capepath, function(err, response, img) { networking.save_texture(rid, cape_hash, capepath, function(net_err, response, img) {
if (response && response.statusCode === 404) { if (response && response.statusCode === 404) {
callback(err, cape_hash, null); callback(net_err, cape_hash, null);
} else { } else {
callback(err, cape_hash, img); callback(net_err, cape_hash, img);
} }
}); });
} }

View File

@ -29,16 +29,31 @@ function extract_url(profile, type) {
return url; return url;
} }
// helper method that calls `get_username_url` or `get_uuid_url` based on the +usedId+
// +userId+ is used for usernames, while +profile+ is used for UUIDs
function get_url(rid, userId, profile, type, callback) {
if (userId.length <= 16) {
// username
exp.get_username_url(rid, userId, type, function(err, url) {
callback(err, url || null);
});
} else {
exp.get_uuid_url(profile, type, function(url) {
callback(null, url || null);
});
}
}
// exracts the skin URL of a +profile+ object // exracts the skin URL of a +profile+ object
// returns null when no URL found (user has no skin) // returns null when no URL found (user has no skin)
exp.extract_skin_url = function(profile) { exp.extract_skin_url = function(profile) {
return extract_url(profile, 'SKIN'); return extract_url(profile, "SKIN");
}; };
// exracts the cape URL of a +profile+ object // exracts the cape URL of a +profile+ object
// returns null when no URL found (user has no cape) // returns null when no URL found (user has no cape)
exp.extract_cape_url = function(profile) { exp.extract_cape_url = function(profile) {
return extract_url(profile, 'CAPE'); return extract_url(profile, "CAPE");
}; };
// performs a GET request to the +url+ // performs a GET request to the +url+
@ -99,7 +114,11 @@ exp.get_from = function(rid, url, callback) {
exp.get_username_url = function(rid, name, type, callback) { exp.get_username_url = function(rid, name, type, callback) {
exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) { exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
if (!err) { if (!err) {
callback(err, response ? (response.statusCode === 404 ? null : response.headers.location) : null); if (response) {
callback(err, response.statusCode === 404 ? null : response.headers.location);
} else {
callback(err, null);
}
} else { } else {
callback(err, null); callback(err, null);
} }
@ -146,21 +165,6 @@ exp.get_cape_url = function(rid, userId, profile, callback) {
}); });
}; };
// helper method that calls `get_username_url` or `get_uuid_url` based on the +usedId+
// +userId+ is used for usernames, while +profile+ is used for UUIDs
function get_url(rid, userId, profile, type, callback) {
if (userId.length <= 16) {
//username
exp.get_username_url(rid, userId, type, function(err, url) {
callback(err, url || null);
});
} else {
exp.get_uuid_url(profile, type, function(url) {
callback(null, url || null);
});
}
}
// download the +tex_hash+ image from the texture server // download the +tex_hash+ image from the texture server
// and save it in the +outpath+ file // and save it in the +outpath+ file
// callback: error, response, image buffer // callback: error, response, image buffer
@ -172,11 +176,11 @@ exp.save_texture = function(rid, tex_hash, outpath, callback) {
logging.error(rid, "error while downloading texture"); logging.error(rid, "error while downloading texture");
callback(err, response, null); callback(err, response, null);
} else { } else {
fs.writeFile(outpath, img, "binary", function(err) { fs.writeFile(outpath, img, "binary", function(fs_err) {
if (err) { if (fs_err) {
logging.error(rid, "error:", err.stack); logging.error(rid, "error:", fs_err.stack);
} }
callback(err, response, img); callback(fs_err, response, img);
}); });
} }
}); });

View File

@ -8,6 +8,22 @@ var Canvas = require("canvas");
var Image = Canvas.Image; var Image = Canvas.Image;
var exp = {}; var exp = {};
// scales an image from the +imagedata+ onto the +context+
// scaled by a factor of +scale+ with options +d_x+ and +d_y+
function scale_image(imageData, context, d_x, d_y, scale) {
var width = imageData.width;
var height = imageData.height;
context.clearRect(0, 0, width, height); // Clear the spot where it originated from
for (var y = 0; y < height; y++) { // height original
for (var x = 0; x < width; x++) { // width original
// Gets original colour, then makes a scaled square of the same colour
var index = (x + y * width) * 4;
context.fillStyle = "rgba(" + imageData.data[index + 0] + ", " + imageData.data[index + 1] + ", " + imageData.data[index + 2] + ", " + imageData.data[index + 3] + ")";
context.fillRect(d_x + x * scale, d_y + y * scale, scale, scale);
}
}
}
// draws the helmet on to the +skin_canvas+ // draws the helmet on to the +skin_canvas+
// using the skin from the +model_ctx+ at the +scale+ // using the skin from the +model_ctx+ at the +scale+
exp.draw_helmet = function(skin_canvas, model_ctx, scale) { exp.draw_helmet = function(skin_canvas, model_ctx, scale) {
@ -179,20 +195,5 @@ exp.open_render = function(rid, renderpath, callback) {
}); });
}; };
// scales an image from the +imagedata+ onto the +context+
// scaled by a factor of +scale+ with options +d_x+ and +d_y+
function scale_image(imageData, context, d_x, d_y, scale) {
var width = imageData.width;
var height = imageData.height;
context.clearRect(0,0,width,height); //Clear the spot where it originated from
for(var y = 0; y < height; y++) { // height original
for(var x = 0; x < width; x++) { // width original
//Gets original colour, then makes a scaled square of the same colour
var index = (x + y * width) * 4;
context.fillStyle = "rgba(" + imageData.data[index+0] + "," + imageData.data[index+1] + "," + imageData.data[index+2] + "," + imageData.data[index+3] + ")";
context.fillRect(d_x + x*scale, d_y + y*scale, scale, scale);
}
}
}
module.exports = exp; module.exports = exp;

View File

@ -14,9 +14,9 @@ exp.extract_face = function(buffer, outname, callback) {
} else { } else {
image.batch() image.batch()
.crop(8, 8, 15, 15) // face .crop(8, 8, 15, 15) // face
.writeFile(outname, function(err) { .writeFile(outname, function(write_err) {
if (err) { if (write_err) {
callback(err); callback(write_err);
} else { } else {
callback(null); callback(null);
} }
@ -34,23 +34,25 @@ exp.extract_helm = function(rid, facefile, buffer, outname, callback) {
if (err) { if (err) {
callback(err); callback(err);
} else { } else {
lwip.open(facefile, function(err, face_img) { lwip.open(facefile, function(open_err, face_img) {
if (err) { if (open_err) {
callback(err); callback(open_err);
} else { } else {
face_img.toBuffer("png", { compression: "none" }, function(err, face_buffer) { face_img.toBuffer("png", { compression: "none" }, function(buf_err, face_buffer) {
skin_img.crop(40, 8, 47, 15, function(err, helm_img) { // FIXME: buf_err is not handled
if (err) { skin_img.crop(40, 8, 47, 15, function(crop_err, helm_img) {
callback(err); if (crop_err) {
callback(crop_err);
} else { } else {
face_img.paste(0, 0, helm_img, function(err, face_helm_img) { face_img.paste(0, 0, helm_img, function(img_err, face_helm_img) {
if (err) { if (img_err) {
callback(err); callback(img_err);
} else { } else {
face_helm_img.toBuffer("png", {compression: "none"}, function(err, face_helm_buffer) { face_helm_img.toBuffer("png", {compression: "none"}, function(buf_err2, face_helm_buffer) {
// FIXME: buf_err2 is not handled
if (face_helm_buffer.toString() !== face_buffer.toString()) { if (face_helm_buffer.toString() !== face_buffer.toString()) {
face_helm_img.writeFile(outname, function(err) { face_helm_img.writeFile(outname, function(write_err) {
callback(err); callback(write_err);
}); });
} else { } else {
logging.log(rid, "helm img == face img, not storing!"); logging.log(rid, "helm img == face img, not storing!");
@ -77,7 +79,8 @@ exp.resize_img = function(inname, size, callback) {
} else { } else {
image.batch() image.batch()
.resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur .resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
.toBuffer("png", function(err, buffer) { .toBuffer("png", function(buf_err, buffer) {
// FIXME: buf_err is not handled
callback(null, buffer); callback(null, buffer);
}); });
} }
@ -96,7 +99,7 @@ exp.default_skin = function(uuid) {
// that can be compacted to counting the LSBs of every 4th byte in the UUID // that can be compacted to counting the LSBs of every 4th byte in the UUID
// an odd sum means alex, an even sum means steve // an odd sum means alex, an even sum means steve
// XOR-ing all the LSBs gives us 1 for alex and 0 for steve // XOR-ing all the LSBs gives us 1 for alex and 0 for steve
var lsbs_even = parseInt(uuid[07], 16) ^ var lsbs_even = parseInt(uuid[ 7], 16) ^
parseInt(uuid[15], 16) ^ parseInt(uuid[15], 16) ^
parseInt(uuid[23], 16) ^ parseInt(uuid[23], 16) ^
parseInt(uuid[31], 16); parseInt(uuid[31], 16);
@ -120,14 +123,15 @@ exp.open_skin = function(rid, skinpath, callback) {
// write the image +buffer+ to the +outpath+ file // write the image +buffer+ to the +outpath+ file
// callback: error // callback: error
exp.save_image = function(buffer, outpath, callback) { exp.save_image = function(buffer, outpath, callback) {
logging.error("outpath:" + outpath);
lwip.open(buffer, "png", function(err, image) { lwip.open(buffer, "png", function(err, image) {
if (err) { if (err) {
callback(err); callback(err);
} else { } else {
image.batch() image.batch()
.writeFile(outpath, function(err) { .writeFile(outpath, function(write_err) {
if (err) { if (write_err) {
callback(err); callback(write_err);
} else { } else {
callback(null); callback(null);
} }

View File

@ -22,6 +22,20 @@ module.exports = function(req, res) {
var etag = null; var etag = null;
var rid = req.id; var rid = req.id;
function sendimage(rid, http_status, img_status, image) {
logging.log(rid, "status:", http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
// Prevent app from crashing/freezing // Prevent app from crashing/freezing
if (size < config.min_size || size > config.max_size) { if (size < config.min_size || size > config.max_size) {
// "Unprocessable Entity", valid request, but semantically erroneous: // "Unprocessable Entity", valid request, but semantically erroneous:
@ -96,18 +110,4 @@ module.exports = function(req, res) {
}); });
} }
} }
function sendimage(rid, http_status, img_status, image) {
logging.log(rid, "status:", http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
}; };

View File

@ -18,6 +18,19 @@ module.exports = function(req, res) {
var etag = null; var etag = null;
var rid = req.id; var rid = req.id;
function sendimage(rid, http_status, img_status, image) {
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
if (!helpers.id_valid(userId)) { if (!helpers.id_valid(userId)) {
res.writeHead(422, { res.writeHead(422, {
"Content-Type": "text/plain", "Content-Type": "text/plain",
@ -70,17 +83,4 @@ module.exports = function(req, res) {
}); });
res.end("500 server error"); res.end("500 server error");
} }
function sendimage(rid, http_status, img_status, image) {
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
}; };

View File

@ -1,8 +1,9 @@
var config = require("../lib/config"); var config = require("../lib/config");
var path = require("path");
var jade = require("jade"); var jade = require("jade");
// compile jade // compile jade
var index = jade.compileFile(__dirname + "/../views/index.jade"); var index = jade.compileFile(path.join(__dirname, "../views/index.jade"));
module.exports = function(req, res) { module.exports = function(req, res) {
var html = index({ var html = index({

View File

@ -41,6 +41,52 @@ module.exports = function(req, res) {
var helm = req.url.query.hasOwnProperty("helm"); var helm = req.url.query.hasOwnProperty("helm");
var etag = null; var etag = null;
function sendimage(rid, http_status, img_status, image) {
logging.log(rid, "status:", http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
// default alex/steve images can be rendered, but
// custom images will not be
function handle_default(rid, http_status, img_status, userId) {
if (def && def !== "steve" && def !== "alex") {
logging.log(rid, "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
res.end();
} else {
def = def || skins.default_skin(userId);
fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
if (err) {
// errored while loading the default image, continuing with null image
logging.error(rid, "error loading default render image:", err);
}
// we render the default skins, but not custom images
renders.draw_model(rid, buf, scale, helm, body, function(render_err, def_img) {
if (render_err) {
logging.error(rid, "error while rendering default image:", render_err);
}
sendimage(rid, http_status, img_status, def_img);
});
});
}
}
if (scale < config.min_scale || scale > config.max_scale) { if (scale < config.min_scale || scale > config.max_scale) {
res.writeHead(422, { res.writeHead(422, {
"Content-Type": "text/plain", "Content-Type": "text/plain",
@ -92,51 +138,4 @@ module.exports = function(req, res) {
logging.error(rid, "error:", e.stack); logging.error(rid, "error:", e.stack);
handle_default(rid, 500, -1, userId); handle_default(rid, 500, -1, userId);
} }
// default alex/steve images can be rendered, but
// custom images will not be
function handle_default(rid, http_status, img_status, userId) {
if (def && def !== "steve" && def !== "alex") {
logging.log(rid, "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
res.end();
} else {
def = def || skins.default_skin(userId);
fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
if (err) {
// errored while loading the default image, continuing with null image
logging.error(rid, "error loading default render image:", err);
}
// we render the default skins, but not custom images
renders.draw_model(rid, buf, scale, helm, body, function(err, def_img) {
if (err) {
logging.error(rid, "error while rendering default image:", err);
}
sendimage(rid, http_status, img_status, def_img);
});
});
}
}
function sendimage(rid, http_status, img_status, image) {
logging.log(rid, "status:", http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": human_status[img_status],
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
}; };

View File

@ -12,6 +12,44 @@ module.exports = function(req, res) {
var etag = null; var etag = null;
var rid = req.id; var rid = req.id;
function sendimage(rid, http_status, image) {
logging.log(rid, "status:", http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
function handle_default(rid, http_status, userId) {
if (def && def !== "steve" && def !== "alex") {
logging.log(rid, "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
res.end();
} else {
def = def || skins.default_skin(userId);
lwip.open("public/images/" + def + "_skin.png", function(err, image) {
// FIXME: err is not handled
image.toBuffer("png", function(buf_err, buffer) {
// FIXME: buf_err is not handled
sendimage(rid, http_status, buffer);
});
});
}
}
if (!helpers.id_valid(userId)) { if (!helpers.id_valid(userId)) {
res.writeHead(422, { res.writeHead(422, {
"Content-Type": "text/plain", "Content-Type": "text/plain",
@ -50,40 +88,4 @@ module.exports = function(req, res) {
logging.error(rid, "error:", e.stack); logging.error(rid, "error:", e.stack);
handle_default(rid, 500, userId); handle_default(rid, 500, userId);
} }
function handle_default(rid, http_status, userId) {
if (def && def !== "steve" && def !== "alex") {
logging.log(rid, "status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Location": def
});
res.end();
} else {
def = def || skins.default_skin(userId);
lwip.open("public/images/" + def + "_skin.png", function(err, image) {
image.toBuffer("png", function(err, buffer) {
sendimage(rid, http_status, buffer);
});
});
}
}
function sendimage(rid, http_status, image) {
logging.log(rid, "status:", http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
"X-Storage-Type": "downloaded",
"X-Request-ID": rid,
"Access-Control-Allow-Origin": "*",
"Etag": '"' + etag + '"'
});
res.end(http_status === 304 ? null : image);
}
}; };

View File

@ -6,7 +6,6 @@ var logging = require("../lib/logging");
var config = require("../lib/config"); var config = require("../lib/config");
var skins = require("../lib/skins"); var skins = require("../lib/skins");
var cache = require("../lib/cache"); var cache = require("../lib/cache");
var renders = require("../lib/renders");
var server = require("../server"); var server = require("../server");
var cleaner = require("../lib/cleaner"); var cleaner = require("../lib/cleaner");
var request = require("request"); var request = require("request");