fedimbed: support posts with >4 images (up to 18)

This commit is contained in:
Cynthia Foxwell 2023-11-29 01:21:16 -07:00
parent c4f8d53321
commit 4bbe1a89d2

View file

@ -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);