Commit b92a8ba0 authored by nanahira's avatar nanahira

update

parent f3d997d4
FROM node FROM node
COPY . /usr/src/app COPY . /usr/src/app
WORKDIR /usr/src/app WORKDIR /usr/src/app
RUN npm ci RUN npm install
CMD node index.js 443 CMD node index.js 443
...@@ -19,7 +19,7 @@ const https_server = https.createServer(https_options, (request, response) => { ...@@ -19,7 +19,7 @@ const https_server = https.createServer(https_options, (request, response) => {
} }
const username = path_match[1]; const username = path_match[1];
_request({ _request({
url: "https://ygobbs.com/users/" + username + ".json", url: "https://ygobbs.com/users/" + encodeURIComponent(username) + ".json",
json: true json: true
}, (error, res, body) => { }, (error, res, body) => {
if (error) { if (error) {
...@@ -31,41 +31,44 @@ const https_server = https.createServer(https_options, (request, response) => { ...@@ -31,41 +31,44 @@ const https_server = https.createServer(https_options, (request, response) => {
response.end("Bad username JSON."); response.end("Bad username JSON.");
console.log("BAD USERNAME JSON", body); console.log("BAD USERNAME JSON", body);
} else if (!body.users) { } else if (!body.users) {
response.writeHead(404); request_avatar(username);
response.end("User not found.");
} else { } else {
const real_username = encodeURIComponent(body.users[0].username); const real_username = body.users[0].username;
request_avatar(real_username);
}
});
});
function request_avatar(response, username) {
_request({
url: "https://api.moecube.com/accounts/users/" + encodeURIComponent(username) + ".avatar"
}, (error, res, body) => {
if (error) {
response.writeHead(500);
response.end("Request error.");
console.error("REQUEST ERROR", error);
} else if (body == "{\"message\":\"Not Found\"}") {
response.writeHead(404);
response.end("Avatar not found.");
} else {
//console.log(body);
_request({ _request({
url: "https://api.moecube.com/accounts/users/" + real_username + ".avatar" url: body,
}, (error, res, body) => { encoding: null
}, (error, res, body) => {
if (error) { if (error) {
response.writeHead(500); response.writeHead(500);
response.end("Request error."); response.end("Avatar error.");
console.error("REQUEST ERROR", error); console.error("AVATAR ERROR", error);
} else if (body == "{\"message\":\"Not Found\"}") { } else {
response.writeHead(404); var recv_buf = Buffer.from(body, 'binary');
response.end("Avatar not found."); response.writeHead(200, { "Content-Type": "image/png" });
} else { fs.writeFileSync("./test.png", recv_buf);
//console.log(body); response.end(recv_buf);
_request({
url: body,
encoding: null
}, (error, res, body) => {
if (error) {
response.writeHead(500);
response.end("Avatar error.");
console.error("AVATAR ERROR", error);
} else {
var recv_buf = Buffer.from(body, 'binary');
response.writeHead(200, { "Content-Type": "image/png" });
fs.writeFileSync("./test.png", recv_buf);
response.end(recv_buf);
}
});
} }
}); });
} }
}); });
}); }
https_server.listen(parseInt(process.argv[2])); https_server.listen(parseInt(process.argv[2]));
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