Add support for userIds as defaults, ccloses #115

This commit is contained in:
Jake 2015-06-24 01:23:22 -05:00
parent f1fd92f1cf
commit 8c39d0c017
4 changed files with 118 additions and 74 deletions

View File

@ -4,16 +4,32 @@ var config = require("../config");
var skins = require("../skins"); var skins = require("../skins");
var cache = require("../cache"); var cache = require("../cache");
var path = require("path"); var path = require("path");
var url = require("url");
function handle_default(img_status, userId, size, def, err, callback) { function handle_default(img_status, userId, size, def, req, err, callback) {
if (def && def !== "steve" && def !== "alex") { def = def || skins.default_skin(userId);
if (def !== "steve" && def !== "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.pathname = parsed.pathname.replace(userId, def);
var newUrl = url.format(parsed);
callback({
status: img_status,
redirect: newUrl,
err: err
});
} else {
callback({ callback({
status: img_status, status: img_status,
redirect: def, redirect: def,
err: err err: err
}); });
}
} else { } else {
def = def || skins.default_skin(userId); // handle steve and alex
skins.resize_img(path.join(__dirname, "..", "public", "images", def + ".png"), size, function(resize_err, image) { skins.resize_img(path.join(__dirname, "..", "public", "images", def + ".png"), size, function(resize_err, image) {
callback({ callback({
status: img_status, status: img_status,
@ -80,10 +96,10 @@ module.exports = function(req, callback) {
hash: hash hash: hash
}); });
} else { } else {
handle_default(status, userId, size, def, err, callback); handle_default(status, userId, size, def, req, err, callback);
} }
}); });
} catch(e) { } catch (e) {
logging.error(req.id, "error:", e.stack); logging.error(req.id, "error:", e.stack);
handle_default(-1, userId, size, def, e, callback); handle_default(-1, userId, size, def, e, callback);
} }

View File

@ -11,17 +11,30 @@ var fs = require("fs");
// helmet is query param // helmet is query param
// TODO: The Type logic should be two separate GET functions once response methods are extracted // TODO: The Type logic should be two separate GET functions once response methods are extracted
// default alex/steve images can be rendered, but function handle_default(img_status, userId, size, def, req, err, callback) {
// custom images will not be def = def || skins.default_skin(userId);
function handle_default(rid, scale, helm, body, img_status, userId, size, def, err, callback) { if (def !== "steve" && def !== "alex") {
if (def && def !== "steve" && def !== "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.pathname = parsed.pathname.replace(userId, def);
var newUrl = url.format(parsed);
callback({
status: img_status,
redirect: newUrl,
err: err
});
} else {
callback({ callback({
status: img_status, status: img_status,
redirect: def, redirect: def,
err: err err: err
}); });
}
} else { } else {
def = def || skins.default_skin(userId); // handle steve and alex
fs.readFile(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function (fs_err, buf) { fs.readFile(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function (fs_err, buf) {
// we render the default skins, but not custom images // we render the default skins, but not custom images
renders.draw_model(rid, buf, scale, helm, body, function(render_err, def_img) { renders.draw_model(rid, buf, scale, helm, body, function(render_err, def_img) {

View File

@ -4,15 +4,30 @@ var skins = require("../skins");
var path = require("path"); var path = require("path");
var lwip = require("lwip"); var lwip = require("lwip");
function handle_default(img_status, userId, def, err, callback) { function handle_default(img_status, userId, size, def, req, err, callback) {
if (def && def !== "steve" && def !== "alex") { def = def || skins.default_skin(userId);
if (def !== "steve" && def !== "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.pathname = parsed.pathname.replace(userId, def);
var newUrl = url.format(parsed);
callback({
status: img_status,
redirect: newUrl,
err: err
});
} else {
callback({ callback({
status: img_status, status: img_status,
redirect: def, redirect: def,
err: err err: err
}); });
}
} else { } else {
def = def || skins.default_skin(userId); // handle steve and alex
lwip.open(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function(lwip_err, image) { lwip.open(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function(lwip_err, image) {
if (image) { if (image) {
image.toBuffer("png", function(buf_err, buffer) { image.toBuffer("png", function(buf_err, buffer) {

View File

@ -342,17 +342,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: 1623808067 crc32: 1623808067
}, },
"avatar with not existing username": { "avatar with non-existent username": {
url: "http://localhost:3000/avatars/0?size=16", url: "http://localhost:3000/avatars/0?size=16",
etag: '"steve"', etag: '"steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"avatar with not existing username defaulting to alex": { "avatar with non-existent username defaulting to alex": {
url: "http://localhost:3000/avatars/0?size=16&default=alex", url: "http://localhost:3000/avatars/0?size=16&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"avatar with not existing username defaulting to url": { "avatar with non-existent username defaulting to url": {
url: "http://localhost:3000/avatars/0?size=16&default=http://example.com", url: "http://localhost:3000/avatars/0?size=16&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -362,17 +362,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: 646871998 crc32: 646871998
}, },
"helm avatar with not existing username": { "helm avatar with non-existent username": {
url: "http://localhost:3000/avatars/0?size=16&helm", url: "http://localhost:3000/avatars/0?size=16&helm",
etag: '"steve"', etag: '"steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"helm avatar with not existing username defaulting to alex": { "helm avatar with non-existent username defaulting to alex": {
url: "http://localhost:3000/avatars/0?size=16&helm&default=alex", url: "http://localhost:3000/avatars/0?size=16&helm&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"helm avatar with not existing username defaulting to url": { "helm avatar with non-existent username defaulting to url": {
url: "http://localhost:3000/avatars/0?size=16&helm&default=http://example.com", url: "http://localhost:3000/avatars/0?size=16&helm&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -382,17 +382,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: 1623808067 crc32: 1623808067
}, },
"avatar with not existing uuid": { "avatar with non-existent uuid": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16",
etag: '"steve"', etag: '"steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"avatar with not existing uuid defaulting to alex": { "avatar with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=alex", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"avatar with not existing uuid defaulting to url": { "avatar with non-existent uuid defaulting to url": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http://example.com", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -402,17 +402,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: 646871998 crc32: 646871998
}, },
"helm avatar with not existing uuid": { "helm avatar with non-existent uuid": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm",
etag: '"steve"', etag: '"steve"',
crc32: [2416827277, 1243826040] crc32: [2416827277, 1243826040]
}, },
"helm avatar with not existing uuid defaulting to alex": { "helm avatar with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=alex", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [862751081, 809395677] crc32: [862751081, 809395677]
}, },
"helm avatar with not existing uuid defaulting to url": { "helm avatar with non-existent uuid defaulting to url": {
url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=http://example.com", url: "http://localhost:3000/avatars/00000000000000000000000000000000?size=16&helm&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -422,11 +422,11 @@ describe("Crafatar", function() {
etag: '"3f688e0e69"', etag: '"3f688e0e69"',
crc32: [989800403, 1901140141] crc32: [989800403, 1901140141]
}, },
"cape with not existing username": { "cape with non-existent username": {
url: "http://localhost:3000/capes/0", url: "http://localhost:3000/capes/0",
crc32: 0 crc32: 0
}, },
"cape with not existing username defaulting to url": { "cape with non-existent username defaulting to url": {
url: "http://localhost:3000/capes/0?default=http://example.com", url: "http://localhost:3000/capes/0?default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -436,11 +436,11 @@ describe("Crafatar", function() {
etag: '"3f688e0e69"', etag: '"3f688e0e69"',
crc32: [989800403, 1901140141] crc32: [989800403, 1901140141]
}, },
"cape with not existing uuid": { "cape with non-existent uuid": {
url: "http://localhost:3000/capes/00000000000000000000000000000000", url: "http://localhost:3000/capes/00000000000000000000000000000000",
crc32: 0 crc32: 0
}, },
"cape with not existing uuid defaulting to url": { "cape with non-existent uuid defaulting to url": {
url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http://example.com", url: "http://localhost:3000/capes/00000000000000000000000000000000?default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -450,17 +450,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: 26500336 crc32: 26500336
}, },
"skin with not existing username": { "skin with non-existent username": {
url: "http://localhost:3000/skins/0", url: "http://localhost:3000/skins/0",
etag: '"steve"', etag: '"steve"',
crc32: 981937087 crc32: 981937087
}, },
"skin with not existing username defaulting to alex": { "skin with non-existent username defaulting to alex": {
url: "http://localhost:3000/skins/0?default=alex", url: "http://localhost:3000/skins/0?default=alex",
etag: '"alex"', etag: '"alex"',
crc32: 2298915739 crc32: 2298915739
}, },
"skin with not existing username defaulting to url": { "skin with non-existent username defaulting to url": {
url: "http://localhost:3000/skins/0?default=http://example.com", url: "http://localhost:3000/skins/0?default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -470,17 +470,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: 26500336 crc32: 26500336
}, },
"skin with not existing uuid": { "skin with non-existent uuid": {
url: "http://localhost:3000/skins/00000000000000000000000000000000", url: "http://localhost:3000/skins/00000000000000000000000000000000",
etag: '"steve"', etag: '"steve"',
crc32: 981937087 crc32: 981937087
}, },
"skin with not existing uuid defaulting to alex": { "skin with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/skins/00000000000000000000000000000000?default=alex", url: "http://localhost:3000/skins/00000000000000000000000000000000?default=alex",
etag: '"alex"', etag: '"alex"',
crc32: 2298915739 crc32: 2298915739
}, },
"skin with not existing uuid defaulting to url": { "skin with non-existent uuid defaulting to url": {
url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http://example.com", url: "http://localhost:3000/skins/00000000000000000000000000000000?default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -490,17 +490,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [353633671, 370672768] crc32: [353633671, 370672768]
}, },
"head render with not existing username": { "head render with non-existent username": {
url: "http://localhost:3000/renders/head/0?scale=2", url: "http://localhost:3000/renders/head/0?scale=2",
etag: '"steve"', etag: '"steve"',
crc32: [883439147, 433083528] crc32: [883439147, 433083528]
}, },
"head render with not existing username defaulting to alex": { "head render with non-existent username defaulting to alex": {
url: "http://localhost:3000/renders/head/0?scale=2&default=alex", url: "http://localhost:3000/renders/head/0?scale=2&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [1240086237, 1108800327] crc32: [1240086237, 1108800327]
}, },
"head render with not existing username defaulting to url": { "head render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/head/0?scale=2&default=http://example.com", url: "http://localhost:3000/renders/head/0?scale=2&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -510,17 +510,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [3456497067, 3490318764] crc32: [3456497067, 3490318764]
}, },
"helm head render with not existing username": { "helm head render with non-existent username": {
url: "http://localhost:3000/renders/head/0?scale=2&helm", url: "http://localhost:3000/renders/head/0?scale=2&helm",
etag: '"steve"', etag: '"steve"',
crc32: [1858563554, 2647471936] crc32: [1858563554, 2647471936]
}, },
"helm head render with not existing username defaulting to alex": { "helm head render with non-existent username defaulting to alex": {
url: "http://localhost:3000/renders/head/0?scale=2&helm&default=alex", url: "http://localhost:3000/renders/head/0?scale=2&helm&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [2963161105, 1769904825] crc32: [2963161105, 1769904825]
}, },
"helm head render with not existing username defaulting to url": { "helm head render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/head/0?scale=2&helm&default=http://example.com", url: "http://localhost:3000/renders/head/0?scale=2&helm&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -530,17 +530,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [353633671, 370672768] crc32: [353633671, 370672768]
}, },
"head render with not existing uuid": { "head render with non-existent uuid": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2",
etag: '"steve"', etag: '"steve"',
crc32: [883439147, 433083528] crc32: [883439147, 433083528]
}, },
"head render with not existing uuid defaulting to alex": { "head render with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=alex", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [1240086237, 1108800327] crc32: [1240086237, 1108800327]
}, },
"head render with not existing uuid defaulting to url": { "head render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http://example.com", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -550,17 +550,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [3456497067, 3490318764] crc32: [3456497067, 3490318764]
}, },
"helm head render with not existing uuid": { "helm head render with non-existent uuid": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm",
etag: '"steve"', etag: '"steve"',
crc32: [1858563554, 2647471936] crc32: [1858563554, 2647471936]
}, },
"helm head render with not existing uuid defaulting to alex": { "helm head render with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=alex", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [2963161105, 1769904825] crc32: [2963161105, 1769904825]
}, },
"helm head render with not existing uuid defaulting to url": { "helm head render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=http://example.com", url: "http://localhost:3000/renders/head/00000000000000000000000000000000?scale=2&helm&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -570,17 +570,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [1291941229, 2628108474] crc32: [1291941229, 2628108474]
}, },
"body render with not existing username": { "body render with non-existent username": {
url: "http://localhost:3000/renders/body/0?scale=2", url: "http://localhost:3000/renders/body/0?scale=2",
etag: '"steve"', etag: '"steve"',
crc32: [2652947188, 2115706574] crc32: [2652947188, 2115706574]
}, },
"body render with not existing username defaulting to alex": { "body render with non-existent username defaulting to alex": {
url: "http://localhost:3000/renders/body/0?scale=2&default=alex", url: "http://localhost:3000/renders/body/0?scale=2&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [407932087, 2516216042] crc32: [407932087, 2516216042]
}, },
"body render with not existing username defaulting to url": { "body render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/body/0?scale=2&default=http://example.com", url: "http://localhost:3000/renders/body/0?scale=2&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -590,17 +590,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [3556188297, 4269754007] crc32: [3556188297, 4269754007]
}, },
"helm body render with not existing username": { "helm body render with non-existent username": {
url: "http://localhost:3000/renders/body/0?scale=2&helm", url: "http://localhost:3000/renders/body/0?scale=2&helm",
etag: '"steve"', etag: '"steve"',
crc32: [272191039, 542896675] crc32: [272191039, 542896675]
}, },
"helm body render with not existing username defaulting to alex": { "helm body render with non-existent username defaulting to alex": {
url: "http://localhost:3000/renders/body/0?scale=2&helm&default=alex", url: "http://localhost:3000/renders/body/0?scale=2&helm&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [737759773, 66512449] crc32: [737759773, 66512449]
}, },
"helm body render with not existing username defaulting to url": { "helm body render with non-existent username defaulting to url": {
url: "http://localhost:3000/renders/body/0?scale=2&helm&default=http://example.com", url: "http://localhost:3000/renders/body/0?scale=2&helm&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -610,17 +610,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [1291941229, 2628108474] crc32: [1291941229, 2628108474]
}, },
"body render with not existing uuid": { "body render with non-existent uuid": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2",
etag: '"steve"', etag: '"steve"',
crc32: [2652947188, 2115706574] crc32: [2652947188, 2115706574]
}, },
"body render with not existing uuid defaulting to alex": { "body render with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=alex", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [407932087, 2516216042] crc32: [407932087, 2516216042]
}, },
"body render with not existing uuid defaulting to url": { "body render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http://example.com", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"
@ -630,17 +630,17 @@ describe("Crafatar", function() {
etag: '"a846b82963"', etag: '"a846b82963"',
crc32: [3556188297, 4269754007] crc32: [3556188297, 4269754007]
}, },
"helm body render with not existing uuid": { "helm body render with non-existent uuid": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm",
etag: '"steve"', etag: '"steve"',
crc32: [272191039, 542896675] crc32: [272191039, 542896675]
}, },
"helm body render with not existing uuid defaulting to alex": { "helm body render with non-existent uuid defaulting to alex": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=alex", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=alex",
etag: '"alex"', etag: '"alex"',
crc32: [737759773, 66512449] crc32: [737759773, 66512449]
}, },
"helm body render with not existing uuid defaulting to url": { "helm body render with non-existent uuid defaulting to url": {
url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=http://example.com", url: "http://localhost:3000/renders/body/00000000000000000000000000000000?scale=2&helm&default=http://example.com",
crc32: 0, crc32: 0,
redirect: "http://example.com" redirect: "http://example.com"