mirror of
https://github.com/azures04/crafatar.git
synced 2026-05-06 19:10:38 +02:00
Implement Skeleton for Returning Capes (Currently returns skins)
Align capes.js codestyle to the rest of the project get_cape_hash helper (not working - wip) clean up capes.js
This commit is contained in:
@@ -137,4 +137,4 @@ exp.get_details = function(uuid, callback) {
|
||||
};
|
||||
|
||||
connect_redis();
|
||||
module.exports = exp;
|
||||
module.exports = exp;
|
||||
|
||||
@@ -13,10 +13,11 @@ var config = {
|
||||
helms_dir: "images/helms/", // directory where helms are kept. should have trailing "/"
|
||||
skins_dir: "images/skins/", // directory where skins are kept. should have trailing "/"
|
||||
renders_dir: "images/renders/",// Directory where rendered skins are kept. should have trailing "/"
|
||||
capes_dir: "images/capes/", // directory where capes are kept. should have trailing "/"
|
||||
debug_enabled: false, // enables logging.debug
|
||||
min_scale: 1, // for renders
|
||||
max_scale: 10, // for renders; too big values might lead to slow response time or DoS
|
||||
default_scale: 6 // for renders; scale to be used when no scale given
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
module.exports = config;
|
||||
|
||||
@@ -75,6 +75,45 @@ function store_images(uuid, details, callback) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
networking.get_cape_url(uuid, function(err, cape_url) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
if (cape_url) {
|
||||
logging.log(uuid + " " + cape_url);
|
||||
// set file paths
|
||||
var hash = get_hash(cape_url);
|
||||
if (details && details.hash == hash) {
|
||||
// hash hasn't changed
|
||||
logging.log(uuid + " hash has not changed");
|
||||
cache.update_timestamp(uuid, hash);
|
||||
callback(null, hash);
|
||||
} else {
|
||||
// hash has changed
|
||||
logging.log(uuid + " new hash: " + hash);
|
||||
var capepath = __dirname + "/../" + config.capes_dir + hash + ".png";
|
||||
|
||||
if (fs.existsSync(capepath)) {
|
||||
logging.log(uuid + " Cape already exists, not downloading");
|
||||
cache.save_hash(uuid, hash);
|
||||
callback(null, hash);
|
||||
} else {
|
||||
// download cape
|
||||
networking.get_cape(cape_url, function(err, img) {
|
||||
if (err || !img) {
|
||||
callback(err, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// profile found, but has no cape
|
||||
cache.save_hash(uuid, null);
|
||||
callback(null, null);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -127,6 +166,19 @@ exp.get_image_hash = function(uuid, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
exp.get_cape_hash = function(uuid, callback) {
|
||||
cache.get_details(uuid, function(err, details) {
|
||||
if (err) {
|
||||
callback(err, -1, null);
|
||||
} else {
|
||||
if (details && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
|
||||
logging.log(uuid + " uuid cached & recently updated");
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
// handles requests for +uuid+ avatars with +size+
|
||||
// callback contains error, status, image buffer, hash
|
||||
@@ -224,4 +276,23 @@ exp.get_render = function(uuid, scale, helm, body, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = exp;
|
||||
exp.get_cape = function(uuid, callback) {
|
||||
logging.log(uuid + " cape request");
|
||||
exp.get_image_hash(uuid, function(err, status, hash) {
|
||||
if (hash) {
|
||||
var capeurl = "http://textures.minecraft.net/texture/" + hash;
|
||||
networking.get_cape(capeurl, function(err, img) {
|
||||
if (err) {
|
||||
logging.error("error while downloading cape");
|
||||
callback(err, hash, null);
|
||||
} else {
|
||||
callback(null, hash, img);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback(err, null, null);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = exp;
|
||||
|
||||
@@ -6,6 +6,7 @@ var fs = require("fs");
|
||||
|
||||
var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
|
||||
var capes_url = "https://skins.minecraft.net/MinecraftCloaks/";
|
||||
|
||||
// exracts the skin url of a +profile+ object
|
||||
// returns null when no url found (user has no skin)
|
||||
@@ -23,6 +24,20 @@ function extract_skin_url(profile) {
|
||||
return url;
|
||||
}
|
||||
|
||||
function extract_cape_url(profile) {
|
||||
var url = null;
|
||||
if (profile && profile.properties) {
|
||||
profile.properties.forEach(function(prop) {
|
||||
if (prop.name == "textures") {
|
||||
var json = Buffer(prop.value, "base64").toString();
|
||||
var props = JSON.parse(json);
|
||||
url = props && props.textures && props.textures.CAPE && props.textures.CAPE.url || null;
|
||||
}
|
||||
});
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
// make a request to skins.miencraft.net
|
||||
// the skin url is taken from the HTTP redirect
|
||||
var get_username_url = function(name, callback) {
|
||||
@@ -104,6 +119,18 @@ exp.get_skin_url = function(uuid, callback) {
|
||||
}
|
||||
};
|
||||
|
||||
exp.get_cape_url = function(uuid, callback) {
|
||||
if (uuid.length <= 16) {
|
||||
get_username_url(uuid, function(err, url) {
|
||||
callback(err, url);
|
||||
});
|
||||
} else {
|
||||
get_uuid_url(uuid, function(err, url) {
|
||||
callback(err, url);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// downloads skin file from +url+
|
||||
// callback contains error, image
|
||||
exp.get_skin = function(url, uuid, callback) {
|
||||
@@ -162,4 +189,37 @@ exp.save_skin = function(uuid, hash, outpath, callback) {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = exp;
|
||||
exp.get_cape = function(url, callback) {
|
||||
request.get({
|
||||
url: url,
|
||||
headers: {
|
||||
"User-Agent": "https://crafatar.com"
|
||||
},
|
||||
encoding: null, // encoding must be null so we get a buffer
|
||||
timeout: config.http_timeout // ms
|
||||
}, function (error, response, body) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
// cape downloaded successfully
|
||||
logging.log("downloaded cape");
|
||||
logging.debug(url);
|
||||
callback(null, body);
|
||||
} else {
|
||||
if (error) {
|
||||
logging.error("Error downloading '" + url + "': " + error);
|
||||
} else if (response.statusCode == 404) {
|
||||
logging.warn("texture not found (404): " + url);
|
||||
} else if (response.statusCode == 429) {
|
||||
logging.warn("too many requests for " + url);
|
||||
logging.warn(body);
|
||||
} else {
|
||||
logging.error("unknown error for " + url);
|
||||
logging.error(response);
|
||||
logging.error(body);
|
||||
error = "unknown error"; // Error needs to be set, otherwise null in callback
|
||||
}
|
||||
callback(error, null);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = exp;
|
||||
|
||||
Reference in New Issue
Block a user