From f95ce50abcb14e28e046dcd56fac98c8d854307e Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 20 Nov 2014 20:01:39 -0600 Subject: [PATCH] Username support --- README.md | 2 +- modules/helpers.js | 19 +++++++++++++++---- modules/networking.js | 4 ++++ routes/avatars.js | 3 ++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 61ca922..86d7693 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Crafatar [![travis](https://api.travis-ci.org/Jake0oo0/Spongy.svg)](https://travis-ci.org/Jake0oo0/Spongy/) +# Crafatar [![travis](https://api.travis-ci.org/Jake0oo0/Crafatar.svg)](https://travis-ci.org/Jake0oo0/Crafatar/) https://crafatar.com diff --git a/modules/helpers.js b/modules/helpers.js index d9493ad..74cad6b 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -4,7 +4,7 @@ var cache = require('./cache'); var skins = require('./skins'); var valid_uuid = /^[0-9a-f]{32}$/; -var hash_pattern = /[0-9a-f]+$/; +var hash_pattern = /[A-za-z0-9_-]{1,16}$|((?:[A-za-z][A-za-z]*[0-9]+[A-za-z0-9]*))/; function get_hash(url) { return hash_pattern.exec(url)[0].toLowerCase(); @@ -22,7 +22,15 @@ function store_images(uuid, details, callback) { } else if (err) { callback(err, null); } else { - var skinurl = skin_url(profile); + var skinurl = null; + + // Username handling + if (uuid.length <= 16) { + skinurl = "https://skins.minecraft.net/MinecraftSkins/" + uuid + ".png" + console.log('is username') + } else { + skinurl = skin_url(profile); + } if (skinurl) { console.log(uuid + " " + skinurl); // set file paths @@ -107,7 +115,11 @@ var exp = {}; // returns true if the +uuid+ is a valid uuid // the uuid may be not exist, however exp.uuid_valid = function(uuid) { - return valid_uuid.test(uuid); + var valid = valid_uuid.test(uuid); + if (!valid && uuid.length <= 16) { + valid = true; + } + return valid; }; // handles requests for +uuid+ images with +size+ @@ -133,7 +145,6 @@ exp.get_avatar = function(uuid, helm, size, callback) { callback(err, status, null); } }); - }; module.exports = exp; \ No newline at end of file diff --git a/modules/networking.js b/modules/networking.js index caebda8..dd35b32 100644 --- a/modules/networking.js +++ b/modules/networking.js @@ -9,6 +9,10 @@ var exp = {}; // download the Mojang profile for +uuid+ // callback contains error, profile object exp.get_profile = function(uuid, callback) { + if (uuid.length <= 16) { + callback(null, null); + return; + } request.get({ url: session_url + uuid, timeout: config.http_timeout // ms diff --git a/routes/avatars.js b/routes/avatars.js index af29152..14e83fe 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -2,6 +2,7 @@ var helpers = require('../modules/helpers'); var router = require('express').Router(); var config = require('../modules/config'); var skins = require('../modules/skins'); +var networking = require('../modules/networking') /* GET avatar request. */ router.get('/:uuid.:ext?', function(req, res) { @@ -10,7 +11,7 @@ router.get('/:uuid.:ext?', function(req, res) { var def = req.query.default; var helm = req.query.hasOwnProperty('helm'); var start = new Date(); - + // Prevent app from crashing/freezing if (size < config.min_size || size > config.max_size) { // "Unprocessable Entity", valid request, but semantically erroneous: