From ae9acbcc52a97ca3c3394a16b53f1ef45dcd3318 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sun, 15 Sep 2024 00:34:53 +1200 Subject: [PATCH] Generate public urls for media.discordapp.net too --- src/d2m/converters/message-to-event.js | 2 +- src/discord/utils.js | 4 +- src/web/routes/download-discord.js | 65 ++++++++++++++------------ 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/d2m/converters/message-to-event.js b/src/d2m/converters/message-to-event.js index 5191e6e2..154c4611 100644 --- a/src/d2m/converters/message-to-event.js +++ b/src/d2m/converters/message-to-event.js @@ -366,7 +366,7 @@ async function messageToEvent(message, guild, options = {}, di) { * 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)) + return content.replace(/https:\/\/(cdn|media)\.discordapp\.(?:com|net)\/attachments\/([0-9]+)\/([0-9]+)\/([-A-Za-z0-9_.,]+)/g, url => dUtils.getPublicUrlForCdn(url)) } /** diff --git a/src/discord/utils.js b/src/discord/utils.js index 24a3c85c..8cb241b0 100644 --- a/src/discord/utils.js +++ b/src/discord/utils.js @@ -121,9 +121,9 @@ 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|media)\.discordapp\.(?:com|net)\/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]}` + return `${reg.ooye.bridge_origin}/download/discord${match[1]}/${match[2]}/${match[3]}/${match[4]}` } module.exports.getPermissions = getPermissions diff --git a/src/web/routes/download-discord.js b/src/web/routes/download-discord.js index 9bac9f3e..047dd944 100644 --- a/src/web/routes/download-discord.js +++ b/src/web/routes/download-discord.js @@ -27,36 +27,41 @@ function timeUntilExpiry(url) { return false } -as.router.get(`/download/discordcdn/:channel_id/:attachment_id/:file_name`, defineEventHandler(async event => { - const params = await getValidatedRouterParams(event, schema.params.parse) +function defineMediaProxyHandler(domain) { + return defineEventHandler(async event => { + const params = await getValidatedRouterParams(event, schema.params.parse) - const row = select("channel_room", "channel_id", {channel_id: params.channel_id}).get() - if (row == null) { - throw createError({ - status: 403, - data: `The file you requested isn't permitted by this media proxy.` - }) - } + const row = select("channel_room", "channel_id", {channel_id: params.channel_id}).get() + if (row == null) { + throw createError({ + status: 403, + data: `The file you requested isn't permitted by this media proxy.` + }) + } - const url = `https://cdn.discordapp.com/attachments/${params.channel_id}/${params.attachment_id}/${params.file_name}` - let promise = cache.get(url) - /** @type {string | undefined} */ - let refreshed - if (promise) { - refreshed = await promise - if (!timeUntilExpiry(refreshed)) promise = undefined - } - if (!promise) { - promise = discord.snow.channel.refreshAttachmentURLs([url]).then(x => x.refreshed_urls[0].refreshed) - cache.set(url, promise) - refreshed = await promise - const time = timeUntilExpiry(refreshed) - assert(time) // the just-refreshed URL will always be in the future - setTimeout(() => { - cache.delete(url) - }, time).unref() - } - assert(refreshed) // will have been assigned by one of the above branches + const url = `https://${domain}/attachments/${params.channel_id}/${params.attachment_id}/${params.file_name}` + let promise = cache.get(url) + /** @type {string | undefined} */ + let refreshed + if (promise) { + refreshed = await promise + if (!timeUntilExpiry(refreshed)) promise = undefined + } + if (!promise) { + promise = discord.snow.channel.refreshAttachmentURLs([url]).then(x => x.refreshed_urls[0].refreshed) + cache.set(url, promise) + refreshed = await promise + const time = timeUntilExpiry(refreshed) + assert(time) // the just-refreshed URL will always be in the future + setTimeout(() => { + cache.delete(url) + }, time).unref() + } + assert(refreshed) // will have been assigned by one of the above branches - return sendRedirect(event, refreshed) -})) + return sendRedirect(event, refreshed) + }) +} + +as.router.get(`/download/discordcdn/:channel_id/:attachment_id/:file_name`, defineMediaProxyHandler("cdn.discordapp.com")) +as.router.get(`/download/discordmedia/:channel_id/:attachment_id/:file_name`, defineMediaProxyHandler("media.discordapp.net"))