/* * Created by sweetbread * Copyright (c) 2025. All rights reserved. */ package ru.risdeveau.pixeldragon.api import io.ktor.client.request.get import io.ktor.client.statement.bodyAsText import org.json.JSONException import org.json.JSONObject import ru.risdeveau.pixeldragon.client import ru.risdeveau.pixeldragon.homeserver suspend fun getHomeserver(url: String): String? { val r = try { client.get("https://$url/.well-known/matrix/client") } catch (_: Exception) { return null } val json = try { JSONObject(r.bodyAsText()) } catch (_: JSONException) { return null } if (!json.has("m.homeserver")) return null var homeserver = json.getJSONObject("m.homeserver").getString("base_url") if (homeserver.endsWith("/")) homeserver = homeserver.dropLast(1) return homeserver } fun mxcToUrl(mxc: String): String? { val pattern = Regex("mxc://([-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6})/([^#?]+)") val match = pattern.find(mxc) if ((match?.groupValues[1] == null) or (match?.groupValues[2] == null)) return null return "$homeserver/_matrix/client/v1/media/download/${match?.groupValues[1]}/${match?.groupValues[2]}" }