From 4bbe1a89d278987ae87e9779a04ad31b9ee14151 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 29 Nov 2023 01:21:16 -0700 Subject: [PATCH] fedimbed: support posts with >4 images (up to 18) --- src/modules/fedimbed.js | 107 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 6 deletions(-) diff --git a/src/modules/fedimbed.js b/src/modules/fedimbed.js index 67f7640..f3d5d0f 100644 --- a/src/modules/fedimbed.js +++ b/src/modules/fedimbed.js @@ -637,12 +637,107 @@ async function processUrl(msg, url, spoiler = false) { const embeds = []; if (images.length > 0) { - for (const attachment of images) { - const embed = Object.assign({}, baseEmbed); - embed.image = { - url: attachment.url, - }; - embeds.push(embed); + if (images.length == 4) { + for (const attachment of images) { + const embed = Object.assign({}, baseEmbed); + embed.image = { + url: attachment.url, + }; + embeds.push(embed); + } + } else if (images.length > 4 && images.length <= 10) { + for (const attachment of images) { + const size = await fetch(attachment.url, { + method: "HEAD", + headers: { + "User-Agent": FRIENDLY_USERAGENT, + }, + }).then((res) => Number(res.headers.get("Content-Length"))); + + if (size <= getUploadLimit(msg.channel.guild)) { + const file = await fetch(attachment.url, { + headers: { + "User-Agent": FRIENDLY_USERAGENT, + }, + }) + .then((res) => res.arrayBuffer()) + .then((buf) => Buffer.from(buf)); + + files.push({ + filename: + (cw != "" || spoiler ? "SPOILER_" : "") + + (attachment.type.indexOf("/") > -1 + ? attachment.type.replace("/", ".") + : attachment.type + + "." + + (url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? "png")), + file, + description: attachment.desc, + }); + } + } + embeds.push(baseEmbed); + } else { + const ten = images.slice(0, 10); + + for (const attachment of ten) { + const size = await fetch(attachment.url, { + method: "HEAD", + headers: { + "User-Agent": FRIENDLY_USERAGENT, + }, + }).then((res) => Number(res.headers.get("Content-Length"))); + + if (size <= getUploadLimit(msg.channel.guild)) { + const file = await fetch(attachment.url, { + headers: { + "User-Agent": FRIENDLY_USERAGENT, + }, + }) + .then((res) => res.arrayBuffer()) + .then((buf) => Buffer.from(buf)); + + files.push({ + filename: + (cw != "" || spoiler ? "SPOILER_" : "") + + (attachment.type.indexOf("/") > -1 + ? attachment.type.replace("/", ".") + : attachment.type + + "." + + (url.match(/\.([a-z0-9]{3,4})$/)?.[0] ?? "png")), + file, + description: attachment.desc, + }); + } + + if (images.length <= 14) { + const fourteen = images.slice(10, Math.min(images.length, 14)); + + for (const attachment of fourteen) { + const embed = Object.assign({}, baseEmbed); + embed.image = { + url: attachment.url, + }; + embeds.push(embed); + } + } + + if (images.length <= 18) { + const eighteen = images.slice(14, Math.min(images.length, 18)); + const _embed = { + url: baseEmbed.url + "?_", + title: "Additional Images", + }; + + for (const attachment of eighteen) { + const embed = Object.assign({}, _embed); + embed.image = { + url: attachment.url, + }; + embeds.push(embed); + } + } + } } } else { embeds.push(baseEmbed);