mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-22 07:51:17 +01:00
add ?default=alex|steve; more error handling
This commit is contained in:
parent
77bcf9dbe7
commit
507e0e9220
@ -9,29 +9,54 @@ var valid_uuid = /^[0-9a-f]{32}$/;
|
|||||||
router.get('/:uuid/:size?', function(req, res) {
|
router.get('/:uuid/:size?', function(req, res) {
|
||||||
var uuid = req.param('uuid');
|
var uuid = req.param('uuid');
|
||||||
var size = req.param('size') || 180;
|
var size = req.param('size') || 180;
|
||||||
|
var def = req.query.default;
|
||||||
// Prevent app from crashing/freezing
|
// Prevent app from crashing/freezing
|
||||||
if (size <= 0 || size > 512) size = 180;
|
if (size <= 0 || size > 512) size = 180;
|
||||||
console.log(uuid);
|
|
||||||
if (valid_uuid.test(uuid)) {
|
if (valid_uuid.test(uuid)) {
|
||||||
var filename = uuid + ".png";
|
var filename = uuid + ".png";
|
||||||
if (fs.existsSync("skins/" + filename)) {
|
if (fs.existsSync("skins/" + filename)) {
|
||||||
skins.resize_img(filename, size, function(data) {
|
console.log('found ' + filename);
|
||||||
res.writeHead(200, {'Content-Type': 'image/png'});
|
skins.resize_img("skins/" + filename, size, function(data) {
|
||||||
|
// tell browser to cache image locally for 10 minutes
|
||||||
|
res.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
|
||||||
res.end(data);
|
res.end(data);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
console.log(filename + ' not found, downloading profile..');
|
||||||
skins.get_profile(uuid, function(profile) {
|
skins.get_profile(uuid, function(profile) {
|
||||||
var skinurl = skins.skin_url(profile);
|
var skinurl = skins.skin_url(profile);
|
||||||
if (skinurl) {
|
if (skinurl) {
|
||||||
|
console.log('got profile, skin url is "' + skinurl + '" downloading..');
|
||||||
skins.skin_file(skinurl, filename, function() {
|
skins.skin_file(skinurl, filename, function() {
|
||||||
skins.resize_img(filename, size, function(data) {
|
console.log('got skin');
|
||||||
res.writeHead(200, {'Content-Type': 'image/png'});
|
skins.resize_img("skins/" + filename, size, function(data) {
|
||||||
|
// tell browser to cache image locally for 10 minutes
|
||||||
|
res.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
|
||||||
res.end(data);
|
res.end(data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.status(404) // HTTP status 404: NotFound
|
console.log('no skin url found');
|
||||||
.send('404 Not found');
|
switch (def) {
|
||||||
|
case "alex":
|
||||||
|
skins.resize_img("public/images/alex.png", size, function(data) {
|
||||||
|
// tell browser to cache image locally for 10 minutes
|
||||||
|
res.writeHead(404, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
|
||||||
|
res.end(data);
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case "steve":
|
||||||
|
skins.resize_img("public/images/steve.png", size, function(data) {
|
||||||
|
// tell browser to cache image locally for 10 minutes
|
||||||
|
res.writeHead(404, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=600, public'});
|
||||||
|
res.end(data);
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res.status(404)
|
||||||
|
.send('404 Not found');
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
11
skins.js
11
skins.js
@ -37,8 +37,8 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}).on('error', function(e) {
|
}).on('error', function(err) {
|
||||||
console.error(e);
|
throw err;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ module.exports = {
|
|||||||
if (prop.name == 'textures') {
|
if (prop.name == 'textures') {
|
||||||
var json = Buffer(prop.value, 'base64').toString();
|
var json = Buffer(prop.value, 'base64').toString();
|
||||||
var props = JSON.parse(json);
|
var props = JSON.parse(json);
|
||||||
url = props.textures.SKIN.url;
|
url = props && props.textures && props.textures.SKIN && props.textures.SKIN.url;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -72,11 +72,14 @@ module.exports = {
|
|||||||
callback(); // outside unlink callback cause we don't have to wait until it's deleted
|
callback(); // outside unlink callback cause we don't have to wait until it's deleted
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}).on('error', function(err) {
|
||||||
|
throw err;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
resize_img: function(inname, size, callback) {
|
resize_img: function(inname, size, callback) {
|
||||||
lwip.open("skins/" + inname, function(err, image) {
|
lwip.open(inname, function(err, image) {
|
||||||
|
if (err) throw err;
|
||||||
image.batch()
|
image.batch()
|
||||||
.resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
|
.resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
|
||||||
.toBuffer('png', function(err, buffer) {
|
.toBuffer('png', function(err, buffer) {
|
||||||
|
|||||||
@ -1,36 +1,38 @@
|
|||||||
extends layout
|
extends layout
|
||||||
|
|
||||||
block content
|
block content
|
||||||
.container(style= "margin-top: 30px;")
|
.container(style= "margin-top: 30px;")
|
||||||
.row
|
.row
|
||||||
.col-md-10
|
.col-md-10
|
||||||
h1 Crafatar
|
h1 Crafatar
|
||||||
hr
|
hr
|
||||||
p Welcome to Crafatar, a website for server Minecraft skins and heads!
|
p Welcome to Crafatar, an API for Minecraft's faces!
|
||||||
h2 API
|
h2 API
|
||||||
hr
|
hr
|
||||||
p
|
p
|
||||||
| Replace
|
| Replace
|
||||||
mark.green uuid
|
mark.green uuid
|
||||||
| with a Mojang UUID to get the related head.
|
| with a Mojang UUID to get the related head.
|
||||||
.well
|
.well
|
||||||
| <img src="https://skins.jake0oo0.me/avatars/
|
| <img src="https://skins.jake0oo0.me/avatars/
|
||||||
mark.green uuid
|
mark.green uuid
|
||||||
| ">
|
| ">
|
||||||
p(style="margin-top: 10px;") Example: UUID: '2d5aa9cdaeb049189930461fc9b91cc5', Username: Jake0oo0
|
p(style="margin-top: 10px;") Example: UUID: '2d5aa9cdaeb049189930461fc9b91cc5', Username: Jake0oo0
|
||||||
.well <img src="https://skins.jake0oo0.me/avatars/2d5aa9cdaeb049189930461fc9b91cc5">
|
.well <img src="https://skins.jake0oo0.me/avatars/2d5aa9cdaeb049189930461fc9b91cc5">
|
||||||
p(style="margin-top: 10px;") There is also an option to include a size, default is 180x180px. The size may not be larger than 512px.
|
p(style="margin-top: 10px;") There is also an option to include a size, default is 180x180px. The size may not be larger than 512px.
|
||||||
.well
|
.well
|
||||||
| <img src="https://skins.jake0oo0.me/avatars/
|
| <img src="https://skins.jake0oo0.me/avatars/
|
||||||
mark.green uuid
|
mark.green uuid
|
||||||
| /
|
| /
|
||||||
mark.green size
|
mark.green size
|
||||||
| ">
|
| ">
|
||||||
p(style="margin-top: 10px;") Example: UUID: 'ae795aa86327408e92ab25c8a59f3ba1', Size: 250px, Username: redstone_sheep
|
p(style="margin-top: 10px;") Example: UUID: 'ae795aa86327408e92ab25c8a59f3ba1', Size: 250px, Username: redstone_sheep
|
||||||
.well <img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250">
|
.well <img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250">
|
||||||
.col-md-2
|
p(style="margin-top: 10px;") By default, a 404 text is returned when the avatar was not found. You can change that to the avatar of steve or alex:
|
||||||
img(src= "/avatars/2d5aa9cdaeb049189930461fc9b91cc5")
|
.well <img src="https://skins.jake0oo0.me/avatars/ae795aa86327408e92ab25c8a59f3ba1/250?default=alex">
|
||||||
img(src= "/avatars/ae795aa86327408e92ab25c8a59f3ba1")
|
.col-md-2
|
||||||
img(src= "/avatars/dab0a06256934c28bf0211142b388c33")
|
img(src= "/avatars/2d5aa9cdaeb049189930461fc9b91cc5")
|
||||||
img(src= "/avatars/3c7db14dac4b4e35b2c63b2237f382be")
|
img(src= "/avatars/ae795aa86327408e92ab25c8a59f3ba1")
|
||||||
img(src= "/avatars/62c41f0e133d4192ad33f0896f47d23a")
|
img(src= "/avatars/dab0a06256934c28bf0211142b388c33")
|
||||||
|
img(src= "/avatars/3c7db14dac4b4e35b2c63b2237f382be")
|
||||||
|
img(src= "/avatars/62c41f0e133d4192ad33f0896f47d23a")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user