use new response module for renders + bug fixes

This commit is contained in:
jomo
2015-04-21 00:57:14 +02:00
parent a947b02c87
commit 8971e3c02b
7 changed files with 85 additions and 113 deletions

View File

@@ -19,6 +19,7 @@ function handle_default(img_status, userId, size, def, err, callback) {
status: img_status,
body: image,
type: "image/png",
hash: def,
err: resize_err || err
});
});

View File

@@ -7,100 +7,66 @@ var skins = require("../skins");
var path = require("path");
var fs = require("fs");
var human_status = {
0: "none",
1: "cached",
2: "downloaded",
3: "checked",
"-1": "error"
};
// valid types: head, body
// helmet is query param
// TODO: The Type logic should be two separate GET functions once response methods are extracted
// default alex/steve images can be rendered, but
// custom images will not be
function handle_default(rid, scale, helm, body, img_status, userId, size, def, err, callback) {
if (def && def !== "steve" && def !== "alex") {
callback({
status: img_status,
redirect: def,
err: err
});
} else {
def = def || skins.default_skin(userId);
fs.readFile(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function (fs_err, buf) {
// we render the default skins, but not custom images
renders.draw_model(rid, buf, scale, helm, body, function(render_err, def_img) {
callback({
status: img_status,
body: def_img,
type: "image/png",
hash: def,
err: render_err || fs_err || err
});
});
});
}
}
// GET render request
module.exports = function(req, res) {
var start = new Date();
module.exports = function(req, callback) {
var raw_type = (req.url.path_list[1] || "");
var rid = req.id;
// validate type
if (raw_type !== "body" && raw_type !== "head") {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("Invalid Render Type");
return;
}
var body = raw_type === "body";
var userId = (req.url.path_list[2] || "").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");
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 + '"'
// validate type
if (raw_type !== "body" && raw_type !== "head") {
callback({
status: -2,
body: "Invalid Render Type"
});
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(path.join(__dirname, "..", "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);
});
});
}
return;
}
if (scale < config.min_scale || scale > config.max_scale) {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
callback({
status: -2,
body: "422 Invalid Scale"
});
res.end("422 Invalid Scale");
return;
} else if (!helpers.id_valid(userId)) {
res.writeHead(422, {
"Content-Type": "text/plain",
"Response-Time": new Date() - start
callback({
status: -2,
body: "422 Invalid ID"
});
res.end("422 Invalid ID");
return;
}
@@ -110,7 +76,6 @@ module.exports = function(req, res) {
try {
helpers.get_render(rid, userId, scale, helm, body, function(err, status, hash, image) {
logging.log(rid, "storage type:", human_status[status]);
if (err) {
logging.error(rid, err);
if (err.code === "ENOENT") {
@@ -118,23 +83,21 @@ module.exports = function(req, res) {
cache.remove_hash(rid, userId);
}
}
etag = hash && hash.substr(0, 32) || "none";
var matches = req.headers["if-none-match"] === '"' + etag + '"';
if (image) {
var http_status = 200;
if (err) {
http_status = 503;
}
logging.debug(rid, "etag:", req.headers["if-none-match"]);
logging.debug(rid, "matches:", matches);
sendimage(rid, matches ? 304 : http_status, status, image);
callback({
status: status,
body: image,
type: "image/png",
hash: hash,
err: err
});
} else {
logging.log(rid, "image not found, using default.");
handle_default(rid, matches ? 304 : 200, status, userId);
handle_default(rid, scale, helm, body, status, userId, scale, def, err, callback);
}
});
} catch(e) {
logging.error(rid, "error:", e.stack);
handle_default(rid, 500, -1, userId);
handle_default(rid, scale, helm, body, -1, userId, scale, def, e, callback);
}
};

View File

@@ -20,6 +20,7 @@ function handle_default(img_status, userId, def, err, callback) {
status: img_status,
body: buffer,
type: "image/png",
hash: def,
err: buf_err || lwip_err || err
});
});
@@ -52,11 +53,13 @@ module.exports = function(req, callback) {
logging.debug(rid, "userid:", userId);
try {
helpers.get_skin(rid, userId, function(err, hash, image) {
helpers.get_skin(rid, userId, function(err, hash, status, image) {
if (image) {
callback({
status: status,
body: image,
type: "image/png",
hash: hash,
err: err
});
} else {