fedimbed: upload videos if we can

This commit is contained in:
Cynthia Foxwell 2022-12-06 10:55:57 -07:00
parent d3e1cbd60a
commit bd82f1167c
2 changed files with 49 additions and 1 deletions

View file

@ -371,6 +371,19 @@ function parseHtmlEntities(str) {
});
}
const UPLOAD_LIMIT = 8388608;
const UPLOAD_LIMIT_TIER_2 = 52428800;
const UPLOAD_LIMIT_TIER_3 = 104857600;
function getUploadLimit(guild) {
if (!guild) return UPLOAD_LIMIT;
if (guild.premiumTier == 2) return UPLOAD_LIMIT_TIER_2;
if (guild.premiumTier == 3) return UPLOAD_LIMIT_TIER_3;
return UPLOAD_LIMIT;
}
module.exports = {
pastelize,
getTopColor,
@ -384,4 +397,5 @@ module.exports = {
selectionMessage,
lookupUser,
parseHtmlEntities,
getUploadLimit,
};

View file

@ -3,7 +3,7 @@ const {MessageFlags, Routes} = require("oceanic.js");
const events = require("../lib/events.js");
const logger = require("../lib/logger.js");
const {hasFlag} = require("../lib/guildSettings.js");
const {parseHtmlEntities} = require("../lib/utils.js");
const {parseHtmlEntities, getUploadLimit} = require("../lib/utils.js");
const FRIENDLY_USERAGENT =
"HiddenPhox/fedimbed (https://gitlab.com/Cynosphere/HiddenPhox)";
@ -198,16 +198,19 @@ async function processUrl(msg, url) {
videos.push({
url: attachment.url,
desc: attachment.name,
type: attachment.mediaType,
});
} else if (attachment.mediaType.startsWith("image/")) {
images.push({
url: attachment.url,
desc: attachment.name,
type: attachment.mediaType,
});
} else if (attachment.mediaType.startsWith("audio/")) {
audios.push({
url: attachment.url,
desc: attachment.name,
type: attachment.mediaType,
});
}
}
@ -352,6 +355,36 @@ async function processUrl(msg, url) {
embeds.push(baseEmbed);
}
const files = [];
if (videos.length > 0) {
for (const attachment of videos) {
const size = await fetch(attachment.url, {
method: "HEAD",
headers: {
"User-Agent": FRIENDLY_USERAGENT,
},
})
.then((res) => res.blob())
.then((blob) => blob.size);
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({
name: "video." + attachment.type.split("/")[1],
contents: file,
});
}
}
}
// NB: OceanicJS/Oceanic#32
//await msg.edit({flags: MessageFlags.SUPPRESS_EMBEDS}).catch(() => {});
await hf.bot.rest
@ -367,6 +400,7 @@ async function processUrl(msg, url) {
await msg.channel.createMessage({
content: cw && attachments.length > 0 ? `:warning: ${cw} || ${url} ||` : "",
embeds,
files,
allowedMentions: {
repliedUser: false,
},