mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
remove expressjs from renders
This commit is contained in:
parent
2f4951311d
commit
7ff1ae1b25
@ -1,4 +1,3 @@
|
|||||||
var router = require("express").Router();
|
|
||||||
var logging = require("../modules/logging");
|
var logging = require("../modules/logging");
|
||||||
var helpers = require("../modules/helpers");
|
var helpers = require("../modules/helpers");
|
||||||
var config = require("../modules/config");
|
var config = require("../modules/config");
|
||||||
@ -14,33 +13,45 @@ var human_status = {
|
|||||||
"-1": "error"
|
"-1": "error"
|
||||||
};
|
};
|
||||||
|
|
||||||
// valid types: head, body. helmet is query param
|
// valid types: head, body
|
||||||
|
// helmet is query param
|
||||||
|
// TODO: The Type logic should be two separate GET functions once response methods are extracted
|
||||||
|
|
||||||
// The Type logic should be two separate GET
|
// GET render request
|
||||||
// functions once response methods are extracted
|
module.exports = function(req, res) {
|
||||||
router.get("/:type/:uuid.:ext?", function(req, res) {
|
var start = new Date();
|
||||||
var raw_type = req.params.type;
|
var raw_type = (req.url.pathname.split("/")[2] || "");
|
||||||
|
|
||||||
// Check valid type for now
|
// validate type
|
||||||
if (raw_type != "body" && raw_type != "head") {
|
if (raw_type != "body" && raw_type != "head") {
|
||||||
res.status(404).send("404 Invalid Render Type");
|
res.writeHead(422, {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
"Response-Time": new Date() - start
|
||||||
|
});
|
||||||
|
res.end("Invalid Render Type");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var body = raw_type == "body";
|
var body = raw_type == "body";
|
||||||
var uuid = req.params.uuid;
|
var uuid = (req.url.pathname.split("/")[3] || "").split(".")[0];
|
||||||
var def = req.query.default;
|
var def = req.url.query.default;
|
||||||
var scale = parseInt(req.query.scale) || config.default_scale;
|
var scale = parseInt(req.url.query.scale) || config.default_scale;
|
||||||
var helm = req.query.hasOwnProperty("helm");
|
var helm = req.url.query.hasOwnProperty("helm");
|
||||||
var start = new Date();
|
|
||||||
var etag = null;
|
var etag = null;
|
||||||
|
|
||||||
if (scale < config.min_scale || scale > config.max_scale) {
|
if (scale < config.min_scale || scale > config.max_scale) {
|
||||||
// Preventing from OOM crashes.
|
res.writeHead(422, {
|
||||||
res.status(422).send("422 Invalid Scale");
|
"Content-Type": "text/plain",
|
||||||
|
"Response-Time": new Date() - start
|
||||||
|
});
|
||||||
|
res.end("422 Invalid Scale");
|
||||||
return;
|
return;
|
||||||
} else if (!helpers.uuid_valid(uuid)) {
|
} else if (!helpers.uuid_valid(uuid)) {
|
||||||
res.status(422).send("422 Invalid UUID");
|
res.writeHead(422, {
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
"Response-Time": new Date() - start
|
||||||
|
});
|
||||||
|
res.end("422 Invalid UUID");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +65,7 @@ router.get("/:type/:uuid.:ext?", function(req, res) {
|
|||||||
logging.error(uuid + " " + err);
|
logging.error(uuid + " " + err);
|
||||||
}
|
}
|
||||||
etag = hash && hash.substr(0, 32) || "none";
|
etag = hash && hash.substr(0, 32) || "none";
|
||||||
var matches = req.get("If-None-Match") == '"' + etag + '"';
|
var matches = req.headers["if-none-match"] == '"' + etag + '"';
|
||||||
if (image) {
|
if (image) {
|
||||||
var http_status = 200;
|
var http_status = 200;
|
||||||
if (matches) {
|
if (matches) {
|
||||||
@ -63,7 +74,7 @@ router.get("/:type/:uuid.:ext?", function(req, res) {
|
|||||||
http_status = 503;
|
http_status = 503;
|
||||||
}
|
}
|
||||||
logging.log("matches: " + matches);
|
logging.log("matches: " + matches);
|
||||||
logging.log("Etag: " + req.get("If-None-Match"));
|
logging.log("Etag: " + req.headers["if-none-match"]);
|
||||||
logging.log("status: " + http_status);
|
logging.log("status: " + http_status);
|
||||||
sendimage(http_status, status, image);
|
sendimage(http_status, status, image);
|
||||||
} else {
|
} else {
|
||||||
@ -119,6 +130,4 @@ router.get("/:type/:uuid.:ext?", function(req, res) {
|
|||||||
});
|
});
|
||||||
res.end(http_status == 304 ? null : image);
|
res.end(http_status == 304 ? null : image);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
module.exports = router;
|
|
||||||
Loading…
x
Reference in New Issue
Block a user