Commit 11e41487 authored by nanahira's avatar nanahira

add fallback

parent d3426c46
...@@ -36,27 +36,30 @@ const https_server = https.createServer(https_options, (request, response) => { ...@@ -36,27 +36,30 @@ const https_server = https.createServer(https_options, (request, response) => {
//console.log("FALLBACK", username); //console.log("FALLBACK", username);
request_avatar(response, username); request_avatar(response, username);
} else { } else {
const real_user = body.user.username; request_avatar(response, encodeURIComponent(body.user.username), encodeURIComponent(body.user.name));
var real_username = encodeURIComponent(real_user);
if (real_user.match(/[a-z0-9]{20}/) || real_user.match(/[0-9]{1,6}/)) { //random username
real_username = encodeURIComponent(body.user.name);
}
request_avatar(response, real_username);
} }
}); });
}); });
function request_avatar(response, username) { function request_avatar(response, username, fallback_username) {
_request({ _request({
url: "https://api.moecube.com/accounts/users/" + username + ".avatar" url: "https://api.moecube.com/accounts/users/" + username + ".avatar"
}, (error, res, body) => { }, (error, res, body) => {
if (error) { if (error) {
response.writeHead(500); if (fallback_username) {
response.end("Request error."); request_avatar(response, fallback_username);
} else {
response.writeHead(500);
response.end("Request error.");
}
console.error("REQUEST ERROR", error); console.error("REQUEST ERROR", error);
} else if (body == "{\"message\":\"Not Found\"}" || body == "{\"message\":\"Authentication Error\"}") { } else if (body == "{\"message\":\"Not Found\"}" || body == "{\"message\":\"Authentication Error\"}") {
response.writeHead(404); if (fallback_username) {
response.end("Avatar not found."); request_avatar(response, fallback_username);
} else {
response.writeHead(404);
response.end("Avatar not found.");
}
} else { } else {
//console.log(body); //console.log(body);
_request({ _request({
...@@ -64,9 +67,13 @@ function request_avatar(response, username) { ...@@ -64,9 +67,13 @@ function request_avatar(response, username) {
encoding: null encoding: null
}, (error, res, body) => { }, (error, res, body) => {
if (error) { if (error) {
response.writeHead(500); if (fallback_username) {
response.end("Avatar error."); request_avatar(response, fallback_username);
console.error("AVATAR ERROR", error); } else {
response.writeHead(500);
response.end("Avatar error.");
console.error("AVATAR ERROR", error);
}
} else { } else {
var recv_buf = Buffer.from(body, 'binary'); var recv_buf = Buffer.from(body, 'binary');
response.writeHead(200, { "Content-Type": "image/png" }); response.writeHead(200, { "Content-Type": "image/png" });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment