-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/api/skins/render?skin={skin}&body={body}&feet={color}
- Loading branch information
Showing
11 changed files
with
8,769 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.