Generate public urls for attachments in message

This commit is contained in:
Cadence Ember 2024-09-14 01:58:17 +12:00
parent c6175e09f8
commit adf07ad736
2 changed files with 9 additions and 1 deletions

View file

@ -362,6 +362,13 @@ async function messageToEvent(message, guild, options = {}, di) {
return content 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. * 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 * @param {string} content Partial or complete Discord message content
@ -370,6 +377,7 @@ async function messageToEvent(message, guild, options = {}, di) {
* @param {any} customHtmlOutput * @param {any} customHtmlOutput
*/ */
async function transformContent(content, customOptions = {}, customParser = null, customHtmlOutput = null) { async function transformContent(content, customOptions = {}, customParser = null, customHtmlOutput = null) {
content = transformAttachmentLinks(content)
content = await transformContentMessageLinks(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. // 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.

View file

@ -121,7 +121,7 @@ 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.discordapp.com\/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/discordcdn/${match[1]}/${match[2]}/${match[3]}`
} }