Generate public urls for media.discordapp.net too

This commit is contained in:
Cadence Ember 2024-09-15 00:34:53 +12:00
parent 6bc3eaf866
commit ae9acbcc52
3 changed files with 38 additions and 33 deletions

View file

@ -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. * Translate Discord attachment links into links that go via the bridge, so they last forever.
*/ */
function transformAttachmentLinks(content) { 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))
} }
/** /**

View file

@ -121,9 +121,9 @@ function timestampToSnowflakeInexact(timestamp) {
/** @param {string} url */ /** @param {string} url */
function getPublicUrlForCdn(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 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 module.exports.getPermissions = getPermissions

View file

@ -27,7 +27,8 @@ function timeUntilExpiry(url) {
return false return false
} }
as.router.get(`/download/discordcdn/:channel_id/:attachment_id/:file_name`, defineEventHandler(async event => { function defineMediaProxyHandler(domain) {
return defineEventHandler(async event => {
const params = await getValidatedRouterParams(event, schema.params.parse) const params = await getValidatedRouterParams(event, schema.params.parse)
const row = select("channel_room", "channel_id", {channel_id: params.channel_id}).get() const row = select("channel_room", "channel_id", {channel_id: params.channel_id}).get()
@ -38,7 +39,7 @@ as.router.get(`/download/discordcdn/:channel_id/:attachment_id/:file_name`, defi
}) })
} }
const url = `https://cdn.discordapp.com/attachments/${params.channel_id}/${params.attachment_id}/${params.file_name}` const url = `https://${domain}/attachments/${params.channel_id}/${params.attachment_id}/${params.file_name}`
let promise = cache.get(url) let promise = cache.get(url)
/** @type {string | undefined} */ /** @type {string | undefined} */
let refreshed let refreshed
@ -59,4 +60,8 @@ as.router.get(`/download/discordcdn/:channel_id/:attachment_id/:file_name`, defi
assert(refreshed) // will have been assigned by one of the above branches 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"))