From bd82f1167ce8674dbda5217d1613d6eda875d994 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 6 Dec 2022 10:55:57 -0700 Subject: [PATCH] fedimbed: upload videos if we can --- src/lib/utils.js | 14 ++++++++++++++ src/modules/fedimbed.js | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/utils.js b/src/lib/utils.js index b37eb0f..91f51e2 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -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, }; diff --git a/src/modules/fedimbed.js b/src/modules/fedimbed.js index 0675c61..1afbdd3 100644 --- a/src/modules/fedimbed.js +++ b/src/modules/fedimbed.js @@ -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, },