convert/index.ts

124 lines
3.8 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)
let extraArgs = ""
let filename;
let size;
if (body.size) {
if (typeof body.size !== 'string' || !/^\d{1,4}x\d{1,4}$/.test(body.size)) {
return c.text('size must be a string in the format <number>x<number>', 400);
}
const [w, h] = body.size.split('x').map(Number);
if (
isNaN(w) || isNaN(h) ||
w < 1 || h < 1 ||
w > 1000 || h > 1000
) {
return c.text('size must be in the format <number>x<number> with max 1000x1000', 400);
}
size = { width: w, height: h };
extraArgs += ` -W ${w} -H ${h}`;
}
extraArgs += " -b"
if(body?.extraArgs) {
if (typeof body.extraArgs !== 'string') {
return c.text('extraArgs must be a string', 400)
}
extraArgs = body.extraArgs;
}
if(body.emoji) {
if(size) {
const inputSize = Math.min(size.width, size.height);
const allowedSizes = [32, 64, 128];
let closest = allowedSizes.reduce((prev, curr) =>
Math.abs(curr - inputSize) < Math.abs(prev - inputSize) ? curr : prev
);
joypixels.emojiSize = closest;
}
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)
filename = "cache/" + emojiUrl.split('/').pop().split(".")[0] + `-${size ? body.size : 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)
}
} else if(body.imageUrl) {
const imageUrl = body.imageUrl;
filename = `cache/${imageUrl.split('/').pop()}-${size ? body.size : "original"}`;
if (!existsSync(filename)) {
const res = await fetch(imageUrl);
if (!res.ok) {
return c.text('Failed to download image', 500);
}
const arrayBuffer = await res.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
if (!existsSync('cache')) {
mkdirSync('cache');
}
writeFileSync(filename, buffer);
}
} else {
return c.text('Please provide either emoji or imageUrl', 400);
}
if(!existsSync(filename+".bimg")) {
let sanjuuniPath = "sanjuuni";
if(existsSync("./sanjuuni")) {
sanjuuniPath = "./sanjuuni";
}
console.log(`${sanjuuniPath} --disable-opencl ${extraArgs} -i ${filename} -o ${filename+".bimg"}`)
await $`${sanjuuniPath} --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,
}