start work on #45

This commit is contained in:
jomo 2015-04-12 19:53:34 +02:00
parent 9d2fe0c454
commit fce58722c8
2 changed files with 35 additions and 60 deletions

View File

@ -12,81 +12,48 @@ var human_status = {
"-1": "error" "-1": "error"
}; };
function handle_default(http_status, img_status, userId, size, def, callback) {
if (def && def !== "steve" && def !== "alex") {
callback(http_status, img_status, def);
} else {
def = def || skins.default_skin(userId);
skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
callback(http_status, img_status, image);
});
}
}
// GET avatar request // GET avatar request
module.exports = function(req, res) { module.exports = function(req, callback) {
var start = new Date();
var userId = (req.url.path_list[2] || "").split(".")[0]; var userId = (req.url.path_list[2] || "").split(".")[0];
var size = parseInt(req.url.query.size) || config.default_size; var size = parseInt(req.url.query.size) || config.default_size;
var def = req.url.query.default; var def = req.url.query.default;
var helm = req.url.query.hasOwnProperty("helm"); var helm = req.url.query.hasOwnProperty("helm");
var etag = null; var etag = null;
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);
}
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);
skins.resize_img("public/images/" + def + ".png", size, function(err, image) {
sendimage(rid, http_status, img_status, 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:
// https://tools.ietf.org/html/rfc4918#page-78 // https://tools.ietf.org/html/rfc4918#page-78
res.writeHead(422, { callback(422, 0, "Invalid Size");
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("Invalid Size");
return; return;
} else if (!helpers.id_valid(userId)) { } else if (!helpers.id_valid(userId)) {
res.writeHead(422, { callback(422, 0, "Invalid ID");
"Content-Type": "text/plain",
"Response-Time": new Date() - start
});
res.end("Invalid ID");
return; return;
} }
// strip dashes // strip dashes
userId = userId.replace(/-/g, ""); userId = userId.replace(/-/g, "");
logging.debug(rid, "userid:", userId); logging.debug(req.id, "userid:", userId);
try { try {
helpers.get_avatar(rid, userId, helm, size, function(err, status, image, hash) { helpers.get_avatar(req.id, userId, helm, size, function(err, status, image, hash) {
logging.log(rid, "storage type:", human_status[status]); logging.log(req.id, "storage type:", human_status[status]);
if (err) { if (err) {
logging.error(rid, err); logging.error(req.id, err);
if (err.code === "ENOENT") { if (err.code === "ENOENT") {
// no such file // no such file
cache.remove_hash(rid, userId); cache.remove_hash(req.id, userId);
} }
} }
etag = image && hash && hash.substr(0, 32) || "none"; etag = image && hash && hash.substr(0, 32) || "none";
@ -96,15 +63,15 @@ module.exports = function(req, res) {
if (err) { if (err) {
http_status = 503; http_status = 503;
} }
logging.debug(rid, "etag:", req.headers["if-none-match"]); logging.debug(req.id, "etag:", req.headers["if-none-match"]);
logging.debug(rid, "matches:", matches); logging.debug(req.id, "matches:", matches);
sendimage(rid, matches ? 304 : http_status, status, image); callback(matches ? 304 : http_status, status, image);
} else { } else {
handle_default(rid, matches ? 304 : 200, status, userId); handle_default(matches ? 304 : 200, status, userId, size, def, callback);
} }
}); });
} catch(e) { } catch(e) {
logging.error(rid, "error:", e.stack); logging.error(req.id, "error:", e.stack);
handle_default(rid, 500, -1, userId); handle_default(500, -1, userId, size, def, callback);
} }
}; };

View File

@ -21,7 +21,10 @@ function asset_request(req, res) {
var filename = path.join(__dirname, "public", req.url.path_list.join("/")); var filename = path.join(__dirname, "public", req.url.path_list.join("/"));
fs.exists(filename, function(exists) { fs.exists(filename, function(exists) {
if (exists) { if (exists) {
res.writeHead(200, { "Content-type": mime.lookup(filename) }); res.writeHead(200, {
"Content-type": mime.lookup(filename),
"Cache-Control": "max-age=7200, public", // cache for 2 hours
});
fs.createReadStream(filename).pipe(res); fs.createReadStream(filename).pipe(res);
} else { } else {
res.writeHead(404, { res.writeHead(404, {
@ -48,6 +51,8 @@ function requestHandler(req, res) {
// generate 12 character random string // generate 12 character random string
request.id = Math.random().toString(36).substring(2, 14); request.id = Math.random().toString(36).substring(2, 14);
res.start = new Date();
var local_path = request.url.path_list[1]; var local_path = request.url.path_list[1];
logging.log(request.id, request.method, request.url.href); logging.log(request.id, request.method, request.url.href);
if (request.method === "GET" || request.method === "HEAD") { if (request.method === "GET" || request.method === "HEAD") {
@ -57,7 +62,10 @@ function requestHandler(req, res) {
routes.index(request, res); routes.index(request, res);
break; break;
case "avatars": case "avatars":
routes.avatars(request, res); routes.avatars(request, function(http_status, img_status, body) {
res.writeHead(http_status, {});
res.end(body);
});
break; break;
case "skins": case "skins":
routes.skins(request, res); routes.skins(request, res);