mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
Mojang has disabled their legacy skins API: https://twitter.com/MojangSupport/status/964511258601865216 With their API rate limits, it's now practially impossible for us to support usernames. Fixes #142. The default parameter allows using: - UUID - URL - MHF_Alex - MHF_Steve - Alex - Steve Contrary to UUIDs, using alex/steve doesn't redirect and instead provides the skin from a locally stored file.
113 lines
3.1 KiB
JavaScript
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 UUID",
|
|
});
|
|
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);
|
|
}
|
|
}; |