mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
skin support, fix #15
This commit is contained in:
parent
796f410248
commit
55ed86a3f6
2
app.js
2
app.js
@ -20,7 +20,7 @@ app.use(cookieParser());
|
|||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
app.use('/', routes);
|
app.use('/', routes);
|
||||||
app.use('/avatars', avatars);
|
app.use('/', avatars);
|
||||||
|
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
|
|||||||
@ -53,6 +53,16 @@ function store_images(uuid, details, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var exp = {};
|
||||||
|
|
||||||
|
// returns true if the +uuid+ is a valid uuid or username
|
||||||
|
// the uuid may be not exist, however
|
||||||
|
exp.uuid_valid = function(uuid) {
|
||||||
|
return valid_uuid.test(uuid);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// decides whether to get an image from disk or to download it
|
// decides whether to get an image from disk or to download it
|
||||||
// callback contains error, status, hash
|
// callback contains error, status, hash
|
||||||
// the status gives information about how the image was received
|
// the status gives information about how the image was received
|
||||||
@ -61,7 +71,7 @@ function store_images(uuid, details, callback) {
|
|||||||
// 1: "cached" - found on disk
|
// 1: "cached" - found on disk
|
||||||
// 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
|
// 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
|
// 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
|
||||||
function get_image_hash(uuid, callback) {
|
exp.get_image_hash = function(uuid, callback) {
|
||||||
cache.get_details(uuid, function(err, details) {
|
cache.get_details(uuid, function(err, details) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, -1, null);
|
callback(err, -1, null);
|
||||||
@ -72,9 +82,6 @@ function get_image_hash(uuid, callback) {
|
|||||||
callback(null, (details.hash ? 1 : 0), details.hash);
|
callback(null, (details.hash ? 1 : 0), details.hash);
|
||||||
} else {
|
} else {
|
||||||
logging.log(uuid + " uuid not known or too old");
|
logging.log(uuid + " uuid not known or too old");
|
||||||
logging.log("details:");
|
|
||||||
logging.log(details);
|
|
||||||
logging.log("/details");
|
|
||||||
store_images(uuid, details, function(err, hash) {
|
store_images(uuid, details, function(err, hash) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err, -1, details && details.hash);
|
callback(err, -1, details && details.hash);
|
||||||
@ -88,23 +95,16 @@ function get_image_hash(uuid, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
var exp = {};
|
|
||||||
|
|
||||||
// returns true if the +uuid+ is a valid uuid or username
|
|
||||||
// the uuid may be not exist, however
|
|
||||||
exp.uuid_valid = function(uuid) {
|
|
||||||
return valid_uuid.test(uuid);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// handles requests for +uuid+ images with +size+
|
// handles requests for +uuid+ images with +size+
|
||||||
// callback contains error, status, image buffer
|
// callback contains error, status, image buffer
|
||||||
// image is the user's face+helm when helm is true, or the face otherwise
|
// image is the user's face+helm when helm is true, or the face otherwise
|
||||||
// for status, see get_image_hash
|
// for status, see get_image_hash
|
||||||
exp.get_avatar = function(uuid, helm, size, callback) {
|
exp.get_avatar = function(uuid, helm, size, callback) {
|
||||||
logging.log("\nrequest: " + uuid);
|
logging.log("\nrequest: " + uuid);
|
||||||
get_image_hash(uuid, function(err, status, hash) {
|
exp.get_image_hash(uuid, function(err, status, hash) {
|
||||||
if (hash) {
|
if (hash) {
|
||||||
var filepath = __dirname + '/../' + (helm ? config.helms_dir : config.faces_dir) + hash + ".png";
|
var filepath = __dirname + '/../' + (helm ? config.helms_dir : config.faces_dir) + hash + ".png";
|
||||||
skins.resize_img(filepath, size, function(img_err, result) {
|
skins.resize_img(filepath, size, function(img_err, result) {
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
var networking = require('../modules/networking');
|
||||||
|
var logging = require('../modules/logging');
|
||||||
var helpers = require('../modules/helpers');
|
var helpers = require('../modules/helpers');
|
||||||
var router = require('express').Router();
|
var router = require('express').Router();
|
||||||
var config = require('../modules/config');
|
var config = require('../modules/config');
|
||||||
@ -11,8 +13,46 @@ var human_status = {
|
|||||||
"-1": "error"
|
"-1": "error"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
router.get('/skins/:uuid.:ext?', function(req, res) {
|
||||||
|
var uuid = req.params.uuid;
|
||||||
|
var start = new Date();
|
||||||
|
|
||||||
|
if (!helpers.uuid_valid(uuid)) {
|
||||||
|
res.status(422).send("422 Invalid UUID");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// strip dashes
|
||||||
|
uuid = uuid.replace(/-/g, "");
|
||||||
|
try {
|
||||||
|
helpers.get_image_hash(uuid, function(err, status, hash) {
|
||||||
|
if (hash) {
|
||||||
|
res.writeHead(301, {
|
||||||
|
'Location': "http://textures.minecraft.net/texture/" + hash,
|
||||||
|
'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
|
||||||
|
'Response-Time': new Date() - start,
|
||||||
|
'X-Storage-Type': human_status[status]
|
||||||
|
});
|
||||||
|
res.end();
|
||||||
|
} else if (!err) {
|
||||||
|
res.writeHead(404, {
|
||||||
|
'Cache-Control': 'max-age=' + config.browser_cache_time + ', public',
|
||||||
|
'Response-Time': new Date() - start,
|
||||||
|
'X-Storage-Type': human_status[status]
|
||||||
|
});
|
||||||
|
res.end("404 Not found");
|
||||||
|
} else {
|
||||||
|
res.status(500).send("500 Internal server error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch(e) {
|
||||||
|
logging.error("Error!");
|
||||||
|
logging.error(e);
|
||||||
|
res.status(500).send("500 Internal server error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/* GET avatar request. */
|
/* GET avatar request. */
|
||||||
router.get('/:uuid.:ext?', function(req, res) {
|
router.get('/avatars/:uuid.:ext?', function(req, res) {
|
||||||
var uuid = req.params.uuid;
|
var uuid = req.params.uuid;
|
||||||
var size = req.query.size || config.default_size;
|
var size = req.query.size || config.default_size;
|
||||||
var def = req.query.default;
|
var def = req.query.default;
|
||||||
@ -35,9 +75,9 @@ router.get('/:uuid.:ext?', function(req, res) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
helpers.get_avatar(uuid, helm, size, function(err, status, image) {
|
helpers.get_avatar(uuid, helm, size, function(err, status, image) {
|
||||||
console.log(uuid + " - " + human_status[status]);
|
logging.log(uuid + " - " + human_status[status]);
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
logging.error(err);
|
||||||
}
|
}
|
||||||
if (image) {
|
if (image) {
|
||||||
sendimage(err ? 503 : 200, status, image);
|
sendimage(err ? 503 : 200, status, image);
|
||||||
@ -46,8 +86,8 @@ router.get('/:uuid.:ext?', function(req, res) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error("Error!");
|
logging.error("Error!");
|
||||||
console.error(e);
|
logging.error(e);
|
||||||
handle_default(500, status);
|
handle_default(500, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user