76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { Hono } from 'hono'
|
|
const app = new Hono()
|
|
const joypixels = require("emoji-toolkit");
|
|
import { mkdirSync, writeFileSync, existsSync, exists, readFileSync } from 'fs'
|
|
import { $ } from 'bun';
|
|
|
|
|
|
app.post('/render', async (c) => {
|
|
const auth = c.req.header('Authorization')
|
|
if (auth !== process.env.PASSWORD) {
|
|
return c.text('Unauthorized', 401)
|
|
}
|
|
|
|
const body = await c.req.json().catch(() => null)
|
|
if (!body?.emoji) {
|
|
return c.text('Missing emoji in body', 400)
|
|
}
|
|
|
|
let extraArgs = "-b"
|
|
|
|
if(body?.extraArgs) {
|
|
if (typeof body.extraArgs !== 'string') {
|
|
return c.text('extraArgs must be a string', 400)
|
|
}
|
|
extraArgs = body.extraArgs;
|
|
}
|
|
|
|
if(body.size) {
|
|
if (typeof body.size !== 'number' || ![32, 64, 128].includes(body.size)) {
|
|
return c.text('size must be one of 32, 64, or 128', 400)
|
|
}
|
|
joypixels.emojiSize = body.size.toString();
|
|
}
|
|
|
|
const emojiHtml = joypixels.toImage(body.emoji)
|
|
const urlMatch = emojiHtml.match(/src="([^"]*)"/)
|
|
if (!urlMatch) {
|
|
return c.text('Could not extract emoji URL', 500)
|
|
}
|
|
|
|
let emojiUrl = urlMatch[1]
|
|
console.log(emojiUrl)
|
|
const filename = "cache/" + emojiUrl.split('/').pop().split(".")[0] + `-${joypixels.emojiSize}.png`;
|
|
if(!existsSync(filename)) {
|
|
|
|
// Download the image
|
|
const res = await fetch(emojiUrl)
|
|
if (!res.ok) {
|
|
return c.text('Failed to download emoji image', 500)
|
|
}
|
|
const arrayBuffer = await res.arrayBuffer()
|
|
const buffer = Buffer.from(arrayBuffer)
|
|
|
|
// Ensure cache directory exists
|
|
if (!existsSync('cache')) {
|
|
mkdirSync('cache')
|
|
}
|
|
|
|
// Get filename from URL
|
|
writeFileSync(filename, buffer)
|
|
}
|
|
|
|
if(!existsSync(filename+".bimg")) {
|
|
await $`sanjuuni --disable-opencl ${extraArgs} -i ${filename} -o ${filename+".bimg"}`
|
|
}
|
|
|
|
const bimg = readFileSync(filename + ".bimg", "utf8");
|
|
|
|
return c.text(bimg)
|
|
})
|
|
|
|
export default {
|
|
port: 7427,
|
|
host: "127.0.0.1",
|
|
fetch: app.fetch,
|
|
}
|