mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
using our own logging, allows us to do some eventual modifications
This commit is contained in:
parent
ea3f322c73
commit
dd8a3b91e2
@ -1,3 +1,4 @@
|
|||||||
|
var logging = require('./logging');
|
||||||
var config = require("./config");
|
var config = require("./config");
|
||||||
var redis = null;
|
var redis = null;
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
@ -5,7 +6,7 @@ var fs = require("fs");
|
|||||||
// sets up redis connection
|
// sets up redis connection
|
||||||
// flushes redis when running on heroku (files aren't kept between pushes)
|
// flushes redis when running on heroku (files aren't kept between pushes)
|
||||||
function connect_redis() {
|
function connect_redis() {
|
||||||
console.log("connecting to redis...");
|
logging.log("connecting to redis...");
|
||||||
if (process.env.REDISCLOUD_URL) {
|
if (process.env.REDISCLOUD_URL) {
|
||||||
var redisURL = require("url").parse(process.env.REDISCLOUD_URL);
|
var redisURL = require("url").parse(process.env.REDISCLOUD_URL);
|
||||||
redis = require("redis").createClient(redisURL.port, redisURL.hostname);
|
redis = require("redis").createClient(redisURL.port, redisURL.hostname);
|
||||||
@ -14,17 +15,17 @@ function connect_redis() {
|
|||||||
redis = require("redis").createClient();
|
redis = require("redis").createClient();
|
||||||
}
|
}
|
||||||
redis.on("ready", function() {
|
redis.on("ready", function() {
|
||||||
console.log("Redis connection established.");
|
logging.log("Redis connection established.");
|
||||||
if(process.env.HEROKU) {
|
if(process.env.HEROKU) {
|
||||||
console.log("Running on heroku, flushing redis");
|
logging.log("Running on heroku, flushing redis");
|
||||||
redis.flushall();
|
redis.flushall();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
redis.on("error", function (err) {
|
redis.on("error", function (err) {
|
||||||
console.error(err);
|
logging.error(err);
|
||||||
});
|
});
|
||||||
redis.on("end", function () {
|
redis.on("end", function () {
|
||||||
console.warn("Redis connection lost!");
|
logging.warn("Redis connection lost!");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,11 +39,11 @@ function update_file_date(hash) {
|
|||||||
var date = new Date();
|
var date = new Date();
|
||||||
fs.utimes(path, date, date, function(err){
|
fs.utimes(path, date, date, function(err){
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
logging.error(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.error("Tried to update " + path + " date, but it doesn't exist");
|
logging.error("Tried to update " + path + " date, but it doesn't exist");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -56,7 +57,7 @@ exp.get_redis = function() {
|
|||||||
|
|
||||||
// sets the timestamp for +uuid+ and its face file's date to now
|
// sets the timestamp for +uuid+ and its face file's date to now
|
||||||
exp.update_timestamp = function(uuid, hash) {
|
exp.update_timestamp = function(uuid, hash) {
|
||||||
console.log(uuid + " cache: updating timestamp");
|
logging.log(uuid + " cache: updating timestamp");
|
||||||
var time = new Date().getTime();
|
var time = new Date().getTime();
|
||||||
redis.hmset(uuid, "t", time);
|
redis.hmset(uuid, "t", time);
|
||||||
update_file_date(hash);
|
update_file_date(hash);
|
||||||
@ -64,7 +65,7 @@ exp.update_timestamp = function(uuid, hash) {
|
|||||||
|
|
||||||
// create the key +uuid+, store +hash+ and time
|
// create the key +uuid+, store +hash+ and time
|
||||||
exp.save_hash = function(uuid, hash) {
|
exp.save_hash = function(uuid, hash) {
|
||||||
console.log(uuid + " cache: saving hash");
|
logging.log(uuid + " cache: saving hash");
|
||||||
var time = new Date().getTime();
|
var time = new Date().getTime();
|
||||||
redis.hmset(uuid, "h", hash, "t", time);
|
redis.hmset(uuid, "h", hash, "t", time);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,7 +6,8 @@ var config = {
|
|||||||
browser_cache_time: 3600, // seconds until browser will request image again
|
browser_cache_time: 3600, // seconds until browser will request image again
|
||||||
http_timeout: 3000, // ms until connection to mojang is dropped
|
http_timeout: 3000, // ms until connection to mojang is dropped
|
||||||
faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/'
|
faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/'
|
||||||
helms_dir: 'skins/helms/' // directory where helms are kept. should have trailing '/'
|
helms_dir: 'skins/helms/', // directory where helms are kept. should have trailing '/'
|
||||||
|
debug_enabled: true // enables logging.debug
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = config;
|
module.exports = config;
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
var networking = require('./networking');
|
var networking = require('./networking');
|
||||||
|
var logging = require('./logging');
|
||||||
var config = require('./config');
|
var config = require('./config');
|
||||||
var cache = require('./cache');
|
var cache = require('./cache');
|
||||||
var skins = require('./skins');
|
var skins = require('./skins');
|
||||||
@ -20,17 +21,17 @@ function store_images(uuid, details, callback) {
|
|||||||
callback(err, null);
|
callback(err, null);
|
||||||
} else {
|
} else {
|
||||||
if (skin_url) {
|
if (skin_url) {
|
||||||
console.log(uuid + " " + skin_url);
|
logging.log(uuid + " " + skin_url);
|
||||||
// set file paths
|
// set file paths
|
||||||
var hash = get_hash(skin_url);
|
var hash = get_hash(skin_url);
|
||||||
if (details && details.hash == hash) {
|
if (details && details.hash == hash) {
|
||||||
// hash hasn't changed
|
// hash hasn't changed
|
||||||
console.log(uuid + " hash has not changed");
|
logging.log(uuid + " hash has not changed");
|
||||||
cache.update_timestamp(uuid, hash);
|
cache.update_timestamp(uuid, hash);
|
||||||
callback(null, hash);
|
callback(null, hash);
|
||||||
} else {
|
} else {
|
||||||
// hash has changed
|
// hash has changed
|
||||||
console.log(uuid + " new hash: " + hash);
|
logging.log(uuid + " new hash: " + hash);
|
||||||
var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
|
var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
|
||||||
var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
|
var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
|
||||||
// download skin, extract face/helm
|
// download skin, extract face/helm
|
||||||
@ -67,18 +68,18 @@ function get_image_hash(uuid, callback) {
|
|||||||
} else {
|
} else {
|
||||||
if (details && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
|
if (details && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
|
||||||
// uuid known + recently updated
|
// uuid known + recently updated
|
||||||
console.log(uuid + " uuid known & recently updated");
|
logging.log(uuid + " uuid known & recently updated");
|
||||||
callback(null, (details.hash ? 1 : 0), details.hash);
|
callback(null, (details.hash ? 1 : 0), details.hash);
|
||||||
} else {
|
} else {
|
||||||
console.log(uuid + " uuid not known or too old");
|
logging.log(uuid + " uuid not known or too old");
|
||||||
console.log("details:");
|
logging.log("details:");
|
||||||
console.log(details);
|
logging.log(details);
|
||||||
console.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);
|
||||||
} else {
|
} else {
|
||||||
console.log(uuid + " hash: " + hash);
|
logging.log(uuid + " hash: " + hash);
|
||||||
var oldhash = details && details.hash;
|
var oldhash = details && details.hash;
|
||||||
var status = hash !== oldhash ? 2 : 3;
|
var status = hash !== oldhash ? 2 : 3;
|
||||||
callback(null, status, hash);
|
callback(null, status, hash);
|
||||||
@ -102,7 +103,7 @@ exp.uuid_valid = function(uuid) {
|
|||||||
// 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) {
|
||||||
console.log("\nrequest: " + uuid);
|
logging.log("\nrequest: " + uuid);
|
||||||
get_image_hash(uuid, function(err, status, hash) {
|
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";
|
||||||
|
|||||||
16
modules/logging.js
Normal file
16
modules/logging.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
var config = require("./config");
|
||||||
|
|
||||||
|
var exp = {};
|
||||||
|
|
||||||
|
function debug() {
|
||||||
|
if (config.debug_enabled) {
|
||||||
|
console.log(Array.prototype.slice.call(arguments).join(" "));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exp.log = console.log;
|
||||||
|
exp.warn = console.warn;
|
||||||
|
exp.error = console.error;
|
||||||
|
exp.debug = debug;
|
||||||
|
|
||||||
|
module.exports = exp;
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
var logging = require('./logging');
|
||||||
var request = require('request');
|
var request = require('request');
|
||||||
var config = require('./config');
|
var config = require('./config');
|
||||||
var skins = require('./skins');
|
var skins = require('./skins');
|
||||||
@ -32,24 +33,24 @@ var get_username_url = function(name, callback) {
|
|||||||
}, function(error, response, body) {
|
}, function(error, response, body) {
|
||||||
if (!error && response.statusCode == 301) {
|
if (!error && response.statusCode == 301) {
|
||||||
// skin_url received successfully
|
// skin_url received successfully
|
||||||
console.log(name + " skin url received");
|
logging.log(name + " skin url received");
|
||||||
callback(null, response.headers.location);
|
callback(null, response.headers.location);
|
||||||
} else if (error) {
|
} else if (error) {
|
||||||
callback(error, null);
|
callback(error, null);
|
||||||
} else if (response.statusCode == 404) {
|
} else if (response.statusCode == 404) {
|
||||||
// skin doesn't exist
|
// skin doesn't exist
|
||||||
console.log(name + " has no skin");
|
logging.log(name + " has no skin");
|
||||||
callback(0, null);
|
callback(0, null);
|
||||||
} else if (response.statusCode == 429) {
|
} else if (response.statusCode == 429) {
|
||||||
// Too Many Requests
|
// Too Many Requests
|
||||||
// Never got this, seems like skins aren't limited
|
// Never got this, seems like skins aren't limited
|
||||||
console.warn(name + " Too many requests");
|
logging.warn(name + " Too many requests");
|
||||||
console.warn(body);
|
logging.warn(body);
|
||||||
callback(null, null);
|
callback(null, null);
|
||||||
} else {
|
} else {
|
||||||
console.error(name + " Unknown error:");
|
logging.error(name + " Unknown error:");
|
||||||
console.error(response);
|
logging.error(response);
|
||||||
console.error(body);
|
logging.error(body);
|
||||||
callback(null, null);
|
callback(null, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -64,23 +65,23 @@ var get_uuid_url = function(uuid, callback) {
|
|||||||
}, function (error, response, body) {
|
}, function (error, response, body) {
|
||||||
if (!error && response.statusCode == 200) {
|
if (!error && response.statusCode == 200) {
|
||||||
// profile downloaded successfully
|
// profile downloaded successfully
|
||||||
console.log(uuid + " profile downloaded");
|
logging.log(uuid + " profile downloaded");
|
||||||
callback(null, extract_skin_url(JSON.parse(body)));
|
callback(null, extract_skin_url(JSON.parse(body)));
|
||||||
} else if (error) {
|
} else if (error) {
|
||||||
callback(error, null);
|
callback(error, null);
|
||||||
} else if (response.statusCode == 204 || response.statusCode == 404) {
|
} 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)
|
// we get 204 No Content when UUID doesn't exist (including 404 in case they change that)
|
||||||
console.log(uuid + " uuid does not exist");
|
logging.log(uuid + " uuid does not exist");
|
||||||
callback(0, null);
|
callback(0, null);
|
||||||
} else if (response.statusCode == 429) {
|
} else if (response.statusCode == 429) {
|
||||||
// Too Many Requests
|
// Too Many Requests
|
||||||
console.warn(uuid + " Too many requests");
|
logging.warn(uuid + " Too many requests");
|
||||||
console.warn(body);
|
logging.warn(body);
|
||||||
callback(null, null);
|
callback(null, null);
|
||||||
} else {
|
} else {
|
||||||
console.error(uuid + " Unknown error:");
|
logging.error(uuid + " Unknown error:");
|
||||||
console.error(response);
|
logging.error(response);
|
||||||
console.error(body);
|
logging.error(body);
|
||||||
callback(null, null);
|
callback(null, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -108,7 +109,7 @@ exp.get_skin_url = function(uuid, callback) {
|
|||||||
// callback contains error
|
// callback contains error
|
||||||
exp.skin_file = function(url, facename, helmname, callback) {
|
exp.skin_file = function(url, facename, helmname, callback) {
|
||||||
if (fs.existsSync(facename) && fs.existsSync(facename)) {
|
if (fs.existsSync(facename) && fs.existsSync(facename)) {
|
||||||
console.log("Images already exist, not downloading.");
|
logging.log("Images already exist, not downloading.");
|
||||||
callback(null);
|
callback(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -119,32 +120,32 @@ exp.skin_file = function(url, facename, helmname, callback) {
|
|||||||
}, function (error, response, body) {
|
}, function (error, response, body) {
|
||||||
if (!error && response.statusCode == 200) {
|
if (!error && response.statusCode == 200) {
|
||||||
// skin downloaded successfully
|
// skin downloaded successfully
|
||||||
console.log(url + " skin downloaded");
|
logging.log(url + " skin downloaded");
|
||||||
skins.extract_face(body, facename, function(err) {
|
skins.extract_face(body, facename, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
} else {
|
} else {
|
||||||
console.log(facename + " face extracted");
|
logging.log(facename + " face extracted");
|
||||||
skins.extract_helm(facename, body, helmname, function(err) {
|
skins.extract_helm(facename, body, helmname, function(err) {
|
||||||
console.log(helmname + " helm extracted.");
|
logging.log(helmname + " helm extracted.");
|
||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error("Error downloading '" + url + "': " + error);
|
logging.error("Error downloading '" + url + "': " + error);
|
||||||
} else if (response.statusCode == 404) {
|
} else if (response.statusCode == 404) {
|
||||||
console.warn("texture not found (404): " + url);
|
logging.warn("texture not found (404): " + url);
|
||||||
} else if (response.statusCode == 429) {
|
} else if (response.statusCode == 429) {
|
||||||
// Too Many Requests
|
// Too Many Requests
|
||||||
// Never got this, seems like textures aren't limited
|
// Never got this, seems like textures aren't limited
|
||||||
console.warn("too many requests for " + url);
|
logging.warn("too many requests for " + url);
|
||||||
console.warn(body);
|
logging.warn(body);
|
||||||
} else {
|
} else {
|
||||||
console.error("unknown error for " + url);
|
logging.error("unknown error for " + url);
|
||||||
console.error(response);
|
logging.error(response);
|
||||||
console.error(body);
|
logging.error(body);
|
||||||
error = "unknown error"; // Error needs to be set, otherwise null in callback
|
error = "unknown error"; // Error needs to be set, otherwise null in callback
|
||||||
}
|
}
|
||||||
callback(error);
|
callback(error);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user