crafatar/lib/routes/avatars.js
jomo 57115202d2 don't rely on hasOwnProperty to exist
from MDN:
JavaScript does not protect the property name hasOwnProperty; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty to get correct results
2016-11-04 16:19:12 +01:00

113 lines
3.1 KiB
JavaScript

var helpers = require("../helpers");
var config = require("../../config");
var skins = require("../skins");
var cache = require("../cache");
var path = require("path");
var url = require("url");
// handle the appropriate 'default=' response
// uses either mhf_steve or mhf_alex (based on +userId+) if no +def+ given
// callback: response object
function handle_default(img_status, userId, size, def, req, err, callback) {
def = def || skins.default_skin(userId);
var defname = def.toLowerCase();
if (defname !== "steve" && defname !== "mhf_steve" && defname !== "alex" && defname !== "mhf_alex") {
if (helpers.id_valid(def)) {
// clean up the old URL to match new image
var parsed = req.url;
delete parsed.query.default;
delete parsed.search;
parsed.path_list[1] = def;
parsed.pathname = "/" + parsed.path_list.join("/");
var newUrl = url.format(parsed);
callback({
status: img_status,
redirect: newUrl,
err: err
});
} else {
callback({
status: img_status,
redirect: def,
err: err
});
}
} else {
// handle steve and alex
def = defname;
if (def.substr(0, 4) !== "mhf_") {
def = "mhf_" + def;
}
skins.resize_img(path.join(__dirname, "..", "public", "images", def + ".png"), size, function(resize_err, image) {
callback({
status: img_status,
body: image,
type: "image/png",
hash: def,
err: resize_err || err
});
});
}
}
// GET avatar request
module.exports = function(req, callback) {
var userId = (req.url.path_list[1] || "").split(".")[0];
var size = parseInt(req.url.query.size) || config.avatars.default_size;
var def = req.url.query.default;
var overlay = Object.prototype.hasOwnProperty.call(req.url.query, "overlay") || Object.prototype.hasOwnProperty.call(req.url.query, "helm");
// check for extra paths
if (req.url.path_list.length > 2) {
callback({
status: -2,
body: "Invalid Path",
code: 404
});
return;
}
// Prevent app from crashing/freezing
if (size < config.avatars.min_size || size > config.avatars.max_size) {
// "Unprocessable Entity", valid request, but semantically erroneous:
// https://tools.ietf.org/html/rfc4918#page-78
callback({
status: -2,
body: "Invalid Size"
});
return;
} else if (!helpers.id_valid(userId)) {
callback({
status: -2,
body: "Invalid UserID"
});
return;
}
// strip dashes
userId = userId.replace(/-/g, "");
try {
helpers.get_avatar(req.id, userId, overlay, size, function(err, status, image, hash) {
if (err) {
if (err.code === "ENOENT") {
// no such file
cache.remove_hash(req.id, userId);
}
}
if (image) {
callback({
status: status,
body: image,
type: "image/png",
err: err,
hash: hash
});
} else {
handle_default(status, userId, size, def, req, err, callback);
}
});
} catch (e) {
handle_default(-1, userId, size, def, req, e, callback);
}
};