From adf07ad736f61578066abf17a51067427d11151c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 14 Sep 2024 01:58:17 +1200 Subject: [PATCH] Generate public urls for attachments in message --- src/d2m/converters/message-to-event.js | 8 ++++++++ src/discord/utils.js | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index bd445378..5191e6e2 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -362,6 +362,13 @@ async function messageToEvent(message, guild, options = {}, di) { return content } + /** + * Translate Discord attachment links into links that go via the bridge, so they last forever. + */ + function transformAttachmentLinks(content) { + return content.replace(/https:\/\/cdn\.discord(?:app)?\.com\/attachments\/([0-9]+)\/([0-9]+)\/([-A-Za-z0-9_.,]+)/g, url => dUtils.getPublicUrlForCdn(url)) + } + /** * Translate links and emojis and mentions and stuff. Give back the text and HTML so they can be combined into bigger events. * @param {string} content Partial or complete Discord message content @@ -370,6 +377,7 @@ async function messageToEvent(message, guild, options = {}, di) { * @param {any} customHtmlOutput */ async function transformContent(content, customOptions = {}, customParser = null, customHtmlOutput = null) { + content = transformAttachmentLinks(content) content = await transformContentMessageLinks(content) // Handling emojis that we don't know about. The emoji has to be present in the DB for it to be picked up in the emoji markdown converter. diff --git a/src/discord/utils.js b/src/discord/utils.js index 85d86cce..24a3c85c 100644 --- a/src/discord/utils.js +++ b/src/discord/utils.js @@ -121,7 +121,7 @@ function timestampToSnowflakeInexact(timestamp) { /** @param {string} url */ function getPublicUrlForCdn(url) { - const match = url.match(`https://cdn.discordapp.com/attachments/([0-9]+)/([0-9]+)/([-A-Za-z0-9_.,]+)`) + const match = url.match(/https:\/\/cdn.discordapp.com\/attachments\/([0-9]+)\/([0-9]+)\/([-A-Za-z0-9_.,]+)/) if (!match) return url return `${reg.ooye.bridge_origin}/download/discordcdn/${match[1]}/${match[2]}/${match[3]}` }