-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatars.js
47 lines (37 loc) · 1.56 KB
/
avatars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const fetch = require("node-fetch"),
crypto = require("crypto"),
sharp = require("sharp"),
fs = require("fs");
const config = require("./config.json");
if(!fs.existsSync("avatars")) {
fs.mkdirSync("avatars");
}
const knownAvatars = {};
const refreshAvatar = async uuid => {
const resp = await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`)
if(resp.ok) {
// get skin
const profile = await resp.json();
const skins = JSON.parse(Buffer.from(profile.properties.find(property => property.name === "textures").value, "base64"));
const skin = sharp(await (await fetch(skins.textures.SKIN.url)).buffer());
// extract avatar
const overlay = await skin.extract({left: 40, top: 8, width: 8, height: 8}).png().toBuffer();
const smallAvatar = await skin.extract({left: 8, top: 8, width: 8, height: 8}).composite([{input: overlay}]).png().toBuffer();
const avatar = await sharp(smallAvatar).resize(256, 256, {kernel: sharp.kernel.nearest}).png().toBuffer();
// cache-busting!!
const hash = crypto.createHash("md5").update(avatar).digest("hex");
const fileName = `avatars/${hash}.png`;
fs.writeFileSync(fileName, avatar);
// remember the avatar
knownAvatars[uuid] = new URL(fileName, config.baseURL).href;
}
};
module.exports = {
getAvatarURL: async uuid => {
if(!knownAvatars[uuid]) {
await refreshAvatar(uuid);
}
return knownAvatars[uuid];
},
refreshAvatar
};