mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
use new response module for capes + more
- made sure that get_cape returns a status - response.js returns 404 if body is empty - 'X-Storage-Type: undefined' is no longer returned when status is `null`
This commit is contained in:
parent
8971e3c02b
commit
13169be774
@ -210,12 +210,7 @@ exp.id_valid = function(userId) {
|
|||||||
|
|
||||||
// decides whether to get a +type+ image for +userId+ from disk or to download it
|
// decides whether to get a +type+ image for +userId+ from disk or to download it
|
||||||
// callback: error, status, hash
|
// callback: error, status, hash
|
||||||
// the status gives information about how the image was received
|
// for status, see response.js
|
||||||
// -1: "error"
|
|
||||||
// 0: "none" - cached as null
|
|
||||||
// 1: "cached" - found on disk
|
|
||||||
// 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
|
|
||||||
// 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 = null;
|
var cached_hash = null;
|
||||||
@ -357,11 +352,11 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// handles requests for +userId+ capes
|
// handles requests for +userId+ capes
|
||||||
// callback: error, cape hash, image buffer
|
// callback: error, cape hash, status, image buffer
|
||||||
exp.get_cape = function(rid, userId, callback) {
|
exp.get_cape = function(rid, userId, callback) {
|
||||||
exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
|
exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
|
||||||
if (!cape_hash) {
|
if (!cape_hash) {
|
||||||
callback(err, null, null);
|
callback(err, null, null, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
|
var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
|
||||||
@ -369,14 +364,14 @@ exp.get_cape = function(rid, userId, callback) {
|
|||||||
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(skin_err, img) {
|
skins.open_skin(rid, capepath, function(skin_err, img) {
|
||||||
callback(skin_err, cape_hash, img);
|
callback(skin_err || err, cape_hash, status, img);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
networking.save_texture(rid, cape_hash, capepath, function(net_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(net_err, cape_hash, null);
|
callback(net_err, cape_hash, status, null);
|
||||||
} else {
|
} else {
|
||||||
callback(net_err, cape_hash, img);
|
callback(net_err, cape_hash, status, img);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,7 @@ module.exports = function(request, response, result) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
response.on("finish", function() {
|
response.on("finish", function() {
|
||||||
logging.log(request.id, response.statusCode, "(" + human_status[result.status] + ")");
|
logging.log(request.id, response.statusCode, "(" + (human_status[result.status] || "-") + ")");
|
||||||
});
|
});
|
||||||
|
|
||||||
response.on("error", function(err) {
|
response.on("error", function(err) {
|
||||||
@ -51,7 +51,8 @@ module.exports = function(request, response, result) {
|
|||||||
result.status = -1;
|
result.status = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.status !== undefined) {
|
if (result.status !== undefined && result.status !== null) {
|
||||||
|
logging.debug("status: " + result.status);
|
||||||
headers["X-Storage-Type"] = human_status[result.status];
|
headers["X-Storage-Type"] = human_status[result.status];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ module.exports = function(request, response, result) {
|
|||||||
response.writeHead(500, headers);
|
response.writeHead(500, headers);
|
||||||
response.end(result.body);
|
response.end(result.body);
|
||||||
} else {
|
} else {
|
||||||
response.writeHead(200, headers);
|
response.writeHead(result.body ? 200 : 404, headers);
|
||||||
response.end(result.body);
|
response.end(result.body);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1,52 +1,25 @@
|
|||||||
var logging = require("../logging");
|
var logging = require("../logging");
|
||||||
var helpers = require("../helpers");
|
var helpers = require("../helpers");
|
||||||
var config = require("../config");
|
|
||||||
var cache = require("../cache");
|
var cache = require("../cache");
|
||||||
|
|
||||||
var human_status = {
|
|
||||||
0: "none",
|
|
||||||
1: "cached",
|
|
||||||
2: "downloaded",
|
|
||||||
3: "checked",
|
|
||||||
"-1": "error"
|
|
||||||
};
|
|
||||||
|
|
||||||
// GET cape request
|
// GET cape request
|
||||||
module.exports = function(req, res) {
|
module.exports = function(req, callback) {
|
||||||
var start = new Date();
|
|
||||||
var userId = (req.url.pathname.split("/")[2] || "").split(".")[0];
|
var userId = (req.url.pathname.split("/")[2] || "").split(".")[0];
|
||||||
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, {
|
callback({
|
||||||
"Content-Type": "text/plain",
|
status: -2,
|
||||||
"Response-Time": new Date() - start
|
body: "Invalid userid"
|
||||||
});
|
});
|
||||||
res.end("Invalid ID");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip dashes
|
// strip dashes
|
||||||
userId = userId.replace(/-/g, "");
|
userId = userId.replace(/-/g, "");
|
||||||
logging.debug(rid, "userid:", userId);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
helpers.get_cape(rid, userId, function(err, status, image, hash) {
|
helpers.get_cape(rid, userId, function(err, hash, status, image) {
|
||||||
logging.log(rid, "storage type:", human_status[status]);
|
|
||||||
if (err) {
|
if (err) {
|
||||||
logging.error(rid, err);
|
logging.error(rid, err);
|
||||||
if (err.code === "ENOENT") {
|
if (err.code === "ENOENT") {
|
||||||
@ -54,38 +27,18 @@ module.exports = function(req, res) {
|
|||||||
cache.remove_hash(rid, userId);
|
cache.remove_hash(rid, userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
etag = hash && hash.substr(0, 32) || "none";
|
callback({
|
||||||
var matches = req.headers["if-none-match"] === '"' + etag + '"';
|
status: status,
|
||||||
if (image) {
|
body: image,
|
||||||
var http_status = 200;
|
type: image ? "image/png" : undefined,
|
||||||
if (err) {
|
hash: hash,
|
||||||
http_status = 503;
|
err: err
|
||||||
}
|
|
||||||
logging.debug(rid, "etag:", req.headers["if-none-match"]);
|
|
||||||
logging.debug(rid, "matches:", matches);
|
|
||||||
logging.log(rid, "status:", http_status);
|
|
||||||
sendimage(rid, matches ? 304 : http_status, status, image);
|
|
||||||
} else if (matches) {
|
|
||||||
res.writeHead(304, {
|
|
||||||
"Etag": '"' + etag + '"',
|
|
||||||
"Response-Time": new Date() - start
|
|
||||||
});
|
});
|
||||||
res.end();
|
|
||||||
} else {
|
|
||||||
res.writeHead(404, {
|
|
||||||
"Content-Type": "text/plain",
|
|
||||||
"Etag": '"' + etag + '"',
|
|
||||||
"Response-Time": new Date() - start
|
|
||||||
});
|
|
||||||
res.end("404 not found");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
logging.error(rid, "error:" + e.stack);
|
callback({
|
||||||
res.writeHead(500, {
|
status: -1,
|
||||||
"Content-Type": "text/plain",
|
err: e
|
||||||
"Response-Time": new Date() - start
|
|
||||||
});
|
});
|
||||||
res.end("500 server error");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -90,7 +90,9 @@ function requestHandler(req, res) {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "capes":
|
case "capes":
|
||||||
routes.capes(req, res);
|
routes.capes(req, function(result) {
|
||||||
|
response(req, res, result);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
asset_request(req, res);
|
asset_request(req, res);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user