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.
*/
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 */
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

View file

@ -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"))