Replace id with uuid, closes #62

This commit is contained in:
Jake
2015-01-29 20:39:18 -06:00
parent ffcd023c05
commit 6e03f0f55d
10 changed files with 214 additions and 213 deletions

View File

@@ -15,7 +15,7 @@ var human_status = {
// GET avatar request
module.exports = function(req, res) {
var start = new Date();
var uuid = (req.url.path_list[2] || "").split(".")[0];
var id = (req.url.path_list[2] || "").split(".")[0];
var size = parseInt(req.url.query.size) || config.default_size;
var def = req.url.query.default;
var helm = req.url.query.hasOwnProperty("helm");
@@ -31,25 +31,25 @@ module.exports = function(req, res) {
});
res.end("Invalid Size");
return;
} else if (!helpers.uuid_valid(uuid)) {
} else if (!helpers.id_valid(id)) {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("Invalid UUID");
res.end("Invalid ID");
return;
}
// strip dashes
uuid = uuid.replace(/-/g, "");
id = id.replace(/-/g, "");
try {
helpers.get_avatar(uuid, helm, size, function(err, status, image, hash) {
logging.log(uuid + " - " + human_status[status]);
helpers.get_avatar(id, helm, size, function(err, status, image, hash) {
logging.log(id + " - " + human_status[status]);
if (err) {
logging.error(uuid + " " + err);
logging.error(id + " " + err);
if (err.code === "ENOENT") {
cache.remove_hash(uuid);
cache.remove_hash(id);
}
}
etag = image && hash && hash.substr(0, 32) || "none";
@@ -61,21 +61,21 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
logging.debug(uuid + " matches: " + matches);
sendimage(http_status, status, image, uuid);
logging.debug(id + " etag: " + req.headers["if-none-match"]);
logging.debug(id + " matches: " + matches);
sendimage(http_status, status, image, id);
} else {
handle_default(404, status, uuid);
handle_default(404, status, id);
}
});
} catch(e) {
logging.error(uuid + " error: " + e);
handle_default(500, status, uuid);
logging.error(id + " error: " + e);
handle_default(500, status, id);
}
function handle_default(http_status, img_status, uuid) {
function handle_default(http_status, img_status, id) {
if (def && def !== "steve" && def !== "alex") {
logging.log(uuid + " status: 301");
logging.log(id + " status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
@@ -85,15 +85,15 @@ module.exports = function(req, res) {
});
res.end();
} else {
def = def || skins.default_skin(uuid);
def = def || skins.default_skin(id);
skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
sendimage(http_status, img_status, image, uuid);
sendimage(http_status, img_status, image, id);
});
}
}
function sendimage(http_status, img_status, image, uuid) {
logging.log(uuid + " status: " + http_status);
function sendimage(http_status, img_status, image, id) {
logging.log(id + " status: " + http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",

View File

@@ -13,10 +13,10 @@ var human_status = {
// GET cape request
module.exports = function(req, res) {
var start = new Date();
var uuid = (req.url.pathname.split("/")[2] || "").split(".")[0];
var id = (req.url.pathname.split("/")[2] || "").split(".")[0];
var etag = null;
if (!helpers.uuid_valid(uuid)) {
if (!helpers.id_valid(id)) {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
@@ -26,13 +26,13 @@ module.exports = function(req, res) {
}
// strip dashes
uuid = uuid.replace(/-/g, "");
id = id.replace(/-/g, "");
try {
helpers.get_cape(uuid, function(err, status, image, hash) {
logging.log(uuid + " - " + human_status[status]);
helpers.get_cape(id, function(err, status, image, hash) {
logging.log(id + " - " + human_status[status]);
if (err) {
logging.error(uuid + " " + err);
logging.error(id + " " + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
@@ -56,7 +56,7 @@ module.exports = function(req, res) {
}
});
} catch(e) {
logging.error(uuid + " error:");
logging.error(id + " error:");
logging.error(e);
res.writeHead(500, {
"Content-Type": "text/plain",

View File

@@ -33,7 +33,7 @@ module.exports = function(req, res) {
}
var body = raw_type === "body";
var uuid = (req.url.path_list[3] || "").split(".")[0];
var id = (req.url.path_list[3] || "").split(".")[0];
var def = req.url.query.default;
var scale = parseInt(req.url.query.scale) || config.default_scale;
var helm = req.url.query.hasOwnProperty("helm");
@@ -46,23 +46,23 @@ module.exports = function(req, res) {
});
res.end("422 Invalid Scale");
return;
} else if (!helpers.uuid_valid(uuid)) {
} else if (!helpers.id_valid(id)) {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("422 Invalid UUID");
res.end("422 Invalid ID");
return;
}
// strip dashes
uuid = uuid.replace(/-/g, "");
id = id.replace(/-/g, "");
try {
helpers.get_render(uuid, scale, helm, body, function(err, status, hash, image) {
logging.log(uuid + " - " + human_status[status]);
helpers.get_render(id, scale, helm, body, function(err, status, hash, image) {
logging.log(id + " - " + human_status[status]);
if (err) {
logging.error(uuid + " " + err);
logging.error(id + " " + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
@@ -73,25 +73,25 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
logging.debug(uuid + " matches: " + matches);
logging.debug(id + " etag: " + req.headers["if-none-match"]);
logging.debug(id + " matches: " + matches);
sendimage(http_status, status, image, uuid);
} else {
logging.log(uuid + " image not found, using default.");
logging.log(id + " image not found, using default.");
handle_default(404, status, uuid);
}
});
} catch(e) {
logging.error(uuid + " error: " + e);
logging.error(id + " error: " + e);
handle_default(500, status, uuid);
}
// default alex/steve images can be rendered, but
// custom images will not be
function handle_default(http_status, img_status, uuid) {
function handle_default(http_status, img_status, id) {
if (def && def !== "steve" && def !== "alex") {
logging.log(uuid + " status: 301");
logging.log(id + " status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
@@ -101,25 +101,25 @@ module.exports = function(req, res) {
});
res.end();
} else {
def = def || skins.default_skin(uuid);
def = def || skins.default_skin(id);
fs.readFile("public/images/" + def + "_skin.png", function (err, buf) {
if (err) {
// errored while loading the default image, continuing with null image
logging.error(uuid + "error loading default render image: " + err);
logging.error(id + "error loading default render image: " + err);
}
// we render the default skins, but not custom images
renders.draw_model(uuid, buf, scale, helm, body, function(err, def_img) {
renders.draw_model(id, buf, scale, helm, body, function(err, def_img) {
if (err) {
logging.log(uuid + "error while rendering default image: " + err);
logging.log(id + "error while rendering default image: " + err);
}
sendimage(http_status, img_status, def_img, uuid);
sendimage(http_status, img_status, def_img, id);
});
});
}
}
function sendimage(http_status, img_status, image, uuid) {
logging.log(uuid + " status: " + http_status);
function sendimage(http_status, img_status, image, id) {
logging.log(id + " status: " + http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",

View File

@@ -7,27 +7,27 @@ var lwip = require("lwip");
// GET skin request
module.exports = function(req, res) {
var start = new Date();
var uuid = (req.url.path_list[2] || "").split(".")[0];
var id = (req.url.path_list[2] || "").split(".")[0];
var def = req.url.query.default;
var etag = null;
if (!helpers.uuid_valid(uuid)) {
if (!helpers.id_valid(id)) {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("Invalid UUID");
res.end("Invalid ID");
return;
}
// strip dashes
uuid = uuid.replace(/-/g, "");
id = id.replace(/-/g, "");
try {
helpers.get_skin(uuid, function(err, hash, image) {
logging.log(uuid);
helpers.get_skin(id, function(err, hash, image) {
logging.log(id);
if (err) {
logging.error(uuid + " " + err);
logging.error(id + " " + err);
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
@@ -38,21 +38,21 @@ module.exports = function(req, res) {
} else if (err) {
http_status = 503;
}
logging.debug(uuid + " etag: " + req.headers["if-none-match"]);
logging.debug(uuid + " matches: " + matches);
sendimage(http_status, image, uuid);
logging.debug(id + " etag: " + req.headers["if-none-match"]);
logging.debug(id + " matches: " + matches);
sendimage(http_status, image, id);
} else {
handle_default(404, uuid);
handle_default(404, id);
}
});
} catch(e) {
logging.error(uuid + " error: " + e);
handle_default(500, uuid);
logging.error(id + " error: " + e);
handle_default(500, id);
}
function handle_default(http_status, uuid) {
function handle_default(http_status, id) {
if (def && def !== "steve" && def !== "alex") {
logging.log(uuid + " status: 301");
logging.log(id + " status: 301");
res.writeHead(301, {
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",
"Response-Time": new Date() - start,
@@ -62,17 +62,17 @@ module.exports = function(req, res) {
});
res.end();
} else {
def = def || skins.default_skin(uuid);
def = def || skins.default_skin(id);
lwip.open("public/images/" + def + "_skin.png", function(err, image) {
image.toBuffer("png", function(err, buffer) {
sendimage(http_status, buffer, uuid);
sendimage(http_status, buffer, id);
});
});
}
}
function sendimage(http_status, image, uuid) {
logging.log(uuid + " status: " + http_status);
function sendimage(http_status, image, id) {
logging.log(id + " status: " + http_status);
res.writeHead(http_status, {
"Content-Type": "image/png",
"Cache-Control": "max-age=" + config.browser_cache_time + ", public",