From d387b73bfebf5f135b566291857b0f3a18f231d5 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 2 Nov 2014 16:37:30 +0100 Subject: [PATCH 1/3] cache non-existing UUIDs --- modules/cache.js | 9 ++++++++- modules/helpers.js | 14 +++++++++----- modules/networking.js | 29 ++++++++++++++--------------- routes/avatars.js | 2 +- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/modules/cache.js b/modules/cache.js index 5154ef6..88d4672 100644 --- a/modules/cache.js +++ b/modules/cache.js @@ -43,7 +43,14 @@ exp.save_hash = function(uuid, hash) { // null when uuid unkown exp.get_details = function(uuid, callback) { redis.hgetall(uuid, function(err, data) { - callback(err, data); + var details = null; + if (data) { + details = { + hash: (data.h == "null" ? null : data.h), + time: data.t + }; + } + callback(err, details); }); }; diff --git a/modules/helpers.js b/modules/helpers.js index 3f0258a..815c152 100644 --- a/modules/helpers.js +++ b/modules/helpers.js @@ -15,7 +15,11 @@ function get_hash(url) { function store_images(uuid, details, callback) { // get profile for +uuid+ networking.get_profile(uuid, function(err, profile) { - if (err) { + if (err === 0) { + // uuid does not exist + cache.save_hash(uuid, null); + callback(null, null); + } else if (err) { callback(err, null); } else { var skinurl = skin_url(profile); @@ -23,7 +27,7 @@ function store_images(uuid, details, callback) { console.log(uuid + " " + skinurl); // set file paths var hash = get_hash(skinurl); - if (details && details.h == hash) { + if (details && details.hash == hash) { // hash hasn't changed console.log(uuid + " hash has not changed"); cache.update_timestamp(uuid); @@ -79,15 +83,15 @@ function get_image_hash(uuid, callback) { if (err) { callback(err, -1, null); } else { - if (details && details.t + config.local_cache_time >= new Date().getTime()) { + if (details && details.time + config.local_cache_time >= new Date().getTime()) { // uuid known + recently updated console.log(uuid + " uuid known & recently updated"); - callback(null, 1, details.h); + callback(null, (details.hash ? 1 : 0), details.hash); } else { console.log(uuid + " uuid not known or too old"); store_images(uuid, details, function(err, hash) { if (err) { - callback(err, -1, details && details.h); + callback(err, -1, details && details.hash); } else { console.log(uuid + " hash: " + hash); callback(null, (hash ? 2 : 3), hash); diff --git a/modules/networking.js b/modules/networking.js index 79853cd..caebda8 100644 --- a/modules/networking.js +++ b/modules/networking.js @@ -17,22 +17,21 @@ exp.get_profile = function(uuid, callback) { // profile downloaded successfully console.log(uuid + " profile downloaded"); callback(null, JSON.parse(body)); + } else if (error) { + callback(error, null); + } else if (response.statusCode == 204 || response.statusCode == 404) { + // we get 204 No Content when UUID doesn't exist (including 404 in case they change that) + console.log(uuid + " uuid does not exist"); + callback(0, null); + } else if (response.statusCode == 429) { + // Too Many Requests + console.warn(uuid + " Too many requests"); + console.warn(body); + callback(null, null); } else { - if (error) { - callback(error, null); - return; - } else if (response.statusCode == 204 || response.statusCode == 404) { - // we get 204 No Content when UUID doesn't exist (including 404 in case they change that) - console.log(uuid + " uuid does not exist"); - } else if (response.statusCode == 429) { - // Too Many Requests - console.warn(uuid + " Too many requests"); - console.warn(body); - } else { - console.error(uuid + " Unknown error:"); - console.error(response); - console.error(body); - } + console.error(uuid + " Unknown error:"); + console.error(response); + console.error(body); callback(null, null); } }); diff --git a/routes/avatars.js b/routes/avatars.js index dba5ec3..2ed529b 100644 --- a/routes/avatars.js +++ b/routes/avatars.js @@ -35,7 +35,7 @@ router.get('/:uuid', function(req, res) { } } else if (status == 1 || status == 2) { sendimage(200, status == 1, image); - } else if (status == 3) { + } else if (status == 0 || status == 3) { handle_404(def); } else { console.error("unexpected error/status"); From 4d806f5db2b92734cb8e49e744b43a5524eda153 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 2 Nov 2014 16:38:45 +0100 Subject: [PATCH 2/3] fix gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1750f60..28e5724 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -skins/*.png +skins/*/*.png *.log node_modules/ .DS_Store From f12b8149be5bff4990398708f9b425e025a065e8 Mon Sep 17 00:00:00 2001 From: jomo Date: Sun, 2 Nov 2014 16:55:42 +0100 Subject: [PATCH 3/3] stfu travis --- test/test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index d9ed148..960fdc1 100644 --- a/test/test.js +++ b/test/test.js @@ -5,10 +5,7 @@ var networking = require('../modules/networking'); var helpers = require('../modules/helpers'); var config = require('../modules/config'); var skins = require('../modules/skins'); -var url = require('url'); -var redisURL = url.parse(process.env.REDISCLOUD_URL); -var redis = require("redis").createClient(redisURL.port, redisURL.hostname, {no_ready_check: true}); -redis.auth(redisURL.auth.split(":")[1]); +var redis = require("redis").createClient(); var uuids = fs.readFileSync('test/uuids.txt').toString().split("\r\n"); // Get a random UUID in order to prevent rate limiting