Move calls from getPublicUrlForMxc to getMedia

This commit is contained in:
Cadence Ember 2024-09-13 17:19:42 +12:00
commit c0d92ea66d
7 changed files with 64 additions and 52 deletions

View file

@ -297,6 +297,7 @@ async function setUserPowerCascade(roomID, mxid, power) {
}
async function ping() {
// not using mreq so that we can read the status code
const res = await fetch(`${mreq.baseUrl}/client/v1/appservice/${reg.id}/ping`, {
method: "POST",
headers: {
@ -312,6 +313,21 @@ async function ping() {
}
}
/**
* @param {string} mxc
* @param {RequestInit} [init]
*/
function getMedia(mxc, init = {}) {
const mediaParts = mxc?.match(/^mxc:\/\/([^/]+)\/(\w+)$/)
assert(mediaParts)
return fetch(`${mreq.baseUrl}/client/v1/media/download/${mediaParts[1]}/${mediaParts[2]}`, {
headers: {
Authorization: `Bearer ${reg.as_token}`
},
...init
})
}
module.exports.path = path
module.exports.register = register
module.exports.createRoom = createRoom
@ -336,3 +352,4 @@ module.exports.profileSetAvatarUrl = profileSetAvatarUrl
module.exports.setUserPower = setUserPower
module.exports.setUserPowerCascade = setUserPowerCascade
module.exports.ping = ping
module.exports.getMedia = getMedia

View file

@ -217,9 +217,8 @@ const commands = [{
} else {
// Upload it to Discord and have the bridge sync it back to Matrix again
for (const e of toUpload) {
const publicUrl = mxUtils.getPublicUrlForMxc(e.url)
// @ts-ignore
const resizeInput = await fetch(publicUrl, {agent: false}).then(res => res.arrayBuffer())
const resizeInput = await api.getMedia(e.url, {agent: false}).then(res => res.arrayBuffer())
const resizeOutput = await sharp(resizeInput)
.resize(EMOJI_SIZE, EMOJI_SIZE, {fit: "inside", withoutEnlargement: true, background: {r: 0, g: 0, b: 0, alpha: 0}})
.png()