From eefc7092f5a91590386c1fe12817867aeb5e53de Mon Sep 17 00:00:00 2001 From: yourfriendoss Date: Fri, 11 Jul 2025 14:25:20 +0300 Subject: [PATCH] add imageUrl , better size config and fix extraargs --- index.ts | 124 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 38 deletions(-) diff --git a/index.ts b/index.ts index bf5c546..e819d40 100644 --- a/index.ts +++ b/index.ts @@ -12,12 +12,30 @@ app.post('/render', async (c) => { } const body = await c.req.json().catch(() => null) - if (!body?.emoji) { - return c.text('Missing emoji in body', 400) - } - - let extraArgs = "-b" - + + 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 x', 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 x 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) @@ -25,43 +43,73 @@ app.post('/render', async (c) => { 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') + 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; } - // Get filename from URL - writeFileSync(filename, buffer) + 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")) { - await $`sanjuuni --disable-opencl ${extraArgs} -i ${filename} -o ${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");