-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (102 loc) · 3.06 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use strict";
const https = require("https");
const fs = require("fs");
const _request = require("request");
const url = require("url");
const gm = require("gm");
const https_options = {
cert: fs.readFileSync("./certs/fullchain.pem"),
key: fs.readFileSync("./certs/privkey.pem")
};
const https_server = https.createServer(https_options, (request, response) => {
const u = url.parse(request.url, false);
const path_match = u.pathname.match(/^\/user_avatar\/ygobbs.com\/(.+)\/(\d+)\/\d+_2\.png$/);
if (!path_match) {
response.writeHead(403);
response.end("Invalid pathname.");
return;
}
//const username = encodeURIComponent(path_match[1]);
const username = path_match[1];
const size = path_match[2];
if (size < 1 || size > 600) {
response.writeHead(403);
response.end("Invalid size.");
return;
}
//console.log("REQUEST", username);
_request({
url: "https://ygobbs.com/users/" + username + ".json",
json: true
}, (error, res, body) => {
if (error) {
response.writeHead(500);
response.end("Username error.");
console.error("USERNAME ERROR", error)
} else if (typeof (body) === "string") {
response.writeHead(500);
response.end("Bad username JSON.");
console.error("BAD USERNAME JSON", body);
} else if (!body.users) {
//console.log("FALLBACK", username);
request_avatar(response, username);
} else {
request_avatar(response, encodeURIComponent(body.user.username), encodeURIComponent(body.user.name));
}
});
});
function request_avatar(response, size, username, fallback_username) {
_request({
url: "https://api.moecube.com/accounts/users/" + username + ".avatar"
}, (error, res, body) => {
if (error) {
if (fallback_username) {
request_avatar(response, size, fallback_username);
} else {
response.writeHead(500);
response.end("Request error.");
}
console.error("REQUEST ERROR", error);
} else if (!body.startsWith("http")) {
if (fallback_username) {
request_avatar(response, size, fallback_username);
} else {
response.writeHead(404);
response.end("Avatar not found.");
}
} else {
//console.log(body);
_request({
url: body,
encoding: null
}, (error, res, body) => {
if (error) {
if (fallback_username) {
request_avatar(response, size, fallback_username);
} else {
response.writeHead(500);
response.end("Avatar error.");
console.error("AVATAR ERROR", error);
}
} else {
var recv_buf = Buffer.from(body, 'binary');
gm(recv_buf).resize(size, size).toBuffer("PNG", (error, dst_buf) => {
if (error) {
if (fallback_username) {
request_avatar(response, size, fallback_username);
} else {
response.writeHead(500);
response.end("Convert error.");
console.error("CONVERT ERROR", error);
}
} else {
response.writeHead(200, { "Content-Type": "image/png" });
response.end(dst_buf);
}
});
}
});
}
});
}
https_server.listen(parseInt(process.argv[2]));