Rework how getMedia does thumbnails

This commit is contained in:
Cadence Ember
2026-05-29 20:10:32 +12:00
parent aecfde54c8
commit 16867d57fb
2 changed files with 19 additions and 5 deletions
+17 -5
View File
@@ -463,17 +463,29 @@ async function ping() {
}
/**
* Given an mxc:// URL, and an optional height for thumbnailing, get the file from the content repository. Returns res.
* Given an mxc:// URL, and optional parameters for thumbnailing, get the file from the content repository. Returns res.
*
* Note that Synapse currently doesn't support animated thumbnails: https://github.com/element-hq/synapse/pull/18831
* @see https://spec.matrix.org/v1.18/client-server-api/#get_matrixclientv1mediathumbnailservernamemediaid
* @param {string} mxc
* @param {RequestInit & {height?: number | string}} [init]
* @param {RequestInit & {thumbnail?: {height?: number | string, width?: number | string, animated?: boolean, method?: "crop" | "scale"}}} [init]
* @return {Promise<Response & {body: streamWeb.ReadableStream<Uint8Array>}>}
*/
async function getMedia(mxc, init = {}) {
init = {...init}
const mediaParts = mxc?.match(/^mxc:\/\/([^/]+)\/(\w+)$/)
assert(mediaParts)
const downloadOrThumbnail = init.height ? "thumbnail" : "download"
let url = `${mreq.baseUrl}/client/v1/media/${downloadOrThumbnail}/${mediaParts[1]}/${mediaParts[2]}`
if (init.height) url += "?" + new URLSearchParams({height: String(init.height), width: String(init.height)})
let route = "download"
let query = ""
if (init.thumbnail) {
route = "thumbnail"
query = "?" + new URLSearchParams(Object.keys(init.thumbnail).map(k => [k, String(init.thumbnail?.[k])]))
}
let url = `${mreq.baseUrl}/client/v1/media/${route}/${mediaParts[1]}/${mediaParts[2]}${query}`
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${reg.as_token}`
+2
View File
@@ -21,6 +21,8 @@ const speedbump = sync.require("./d2m/actions/speedbump")
const ks = sync.require("./matrix/kstate")
const setPresence = sync.require("./d2m/actions/set-presence")
const channelWebhook = sync.require("./m2d/actions/channel-webhook")
const dUtils = sync.require("./discord/utils")
const mUtils = sync.require("./matrix/utils")
const guildID = "112760669178241024"
async function ping() {