Skip to content

Commit

Permalink
loading
Browse files Browse the repository at this point in the history
/api/skins/render?skin={skin}&body={body}&feet={color}
  • Loading branch information
ByFox213 committed Jul 25, 2024
1 parent f2a4220 commit b7d298d
Show file tree
Hide file tree
Showing 11 changed files with 8,769 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# SkinRenderApi

# Install
```bash
npm i
```

# Run
```bash
node --env-file=.env SkinRender.js
```
68 changes: 68 additions & 0 deletions SkinRender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const fs = require("fs");
const path = require("path");
const Skin = require("./node_modules/teeworlds-utilities/build/main/asset/skin.js").default;
const { ColorCode } = require("./node_modules/teeworlds-utilities/build/main/color.js");

const fastify = require("fastify")({
logger: true,
});

const SKIN_PATH = process.env.SKIN_PATH || path.join(__dirname, "skins");
const OUTPUT_PATH = process.env.OUTPUT_PATH || path.join(__dirname, "render");
const BASEURL = `https://${process.env.BASEURL || 'domain.com'}/api/skins/render`;


class SkinManager {
static async loadSkin(skinPath) {
const skin = new Skin();
return await skin.load(skinPath);
}

static async saveGameskinPart(skin, body, feet, outPath) {
if (body !== null) {
skin.colorBody(new ColorCode(body))
}

if (feet !== null) {
skin.colorBody(new ColorCode(feet))
}
skin
.render()
.saveRenderAs(outPath);
}
}

// Define the API endpoint
fastify.get("/api/render", async (request, reply) => {
try {
const { skin: skinName, body = null, feet = null} = request.query;
if (!skinName) {
return { err: "not_skin" };
}

const skinPath = path.join(SKIN_PATH, `${skinName}.png`);
if (!fs.existsSync(skinPath)) {
return { err: "skin not found" };
}

const outSkinName = `${skinName}_${body}_${feet}.png`;
const outSkinPath = path.join(OUTPUT_PATH, outSkinName);

if (fs.existsSync(outSkinPath)) {
return { url: `${BASEURL}${outSkinName}` };
}

const skin = await SkinManager.loadSkin(skinPath);
await SkinManager.saveGameskinPart(skin, body, feet, outSkinPath);
reply.type("application/json").code(200);
return { url: `${BASEURL}${outSkinName}` };
} catch (error) {
console.error("Error:", error);
}
});

// Start the server
fastify.listen({ host: "0.0.0.0", port: Number(process.env.PORT || 1541) }, (err, address) => {
if (err) throw err;
console.log(address);
});
18 changes: 18 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
upstream skins{
server 127.0.0.1:1;
server 127.0.0.1:0;
}

server {
server_name domain; # TODO
charset utf-8;
root /usr/share/nginx/html;
location / {
proxy_pass http://skins;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
access_log /var/log/nginx/domain.access.log; // TODO
error_log /var/log/nginx/domain.error.log; // TODO
}
Loading

0 comments on commit b7d298d

Please sign in to comment.