add imageUrl , better size config and fix extraargs
This commit is contained in:
parent
4f0a778b9a
commit
eefc7092f5
1 changed files with 86 additions and 38 deletions
68
index.ts
68
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 = ""
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
let extraArgs = "-b"
|
||||
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,11 +43,14 @@ 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();
|
||||
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)
|
||||
|
|
@ -40,7 +61,7 @@ app.post('/render', async (c) => {
|
|||
|
||||
let emojiUrl = urlMatch[1]
|
||||
console.log(emojiUrl)
|
||||
const filename = "cache/" + emojiUrl.split('/').pop().split(".")[0] + `-${joypixels.emojiSize}.png`;
|
||||
filename = "cache/" + emojiUrl.split('/').pop().split(".")[0] + `-${size ? body.size : joypixels.emojiSize}.png`;
|
||||
if(!existsSync(filename)) {
|
||||
|
||||
// Download the image
|
||||
|
|
@ -59,9 +80,36 @@ app.post('/render', async (c) => {
|
|||
// 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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue