Files
Nekosu/app/src/main/java/ru/risdeveau/pixeldragon/api/Server.kt
T

36 lines
1.1 KiB
Kotlin
Raw Normal View History

2025-02-21 14:44:48 +03:00
/*
2025-03-03 18:42:57 +03:00
* Created by sweetbread
2025-02-21 14:44:48 +03:00
* Copyright (c) 2025. All rights reserved.
2025-03-03 18:42:57 +03:00
* Last modified 03.03.2025, 15:40
2025-02-21 14:44:48 +03:00
*/
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
2025-02-22 18:45:55 +03:00
import ru.risdeveau.pixeldragon.homeserver
2025-02-21 14:44:48 +03:00
2025-03-03 18:42:57 +03:00
suspend fun getHomeserver(url: String): String? {
2025-02-21 14:44:48 +03:00
val r = try { client.get("https://$url/.well-known/matrix/client") }
2025-03-03 18:42:57 +03:00
catch (_: Exception) { return null }
2025-02-21 14:44:48 +03:00
2025-03-03 18:42:57 +03:00
val json = try { JSONObject(r.bodyAsText()) }
catch (_: JSONException) { return null }
2025-02-21 14:44:48 +03:00
2025-03-03 18:42:57 +03:00
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
2025-02-22 18:45:55 +03:00
}
fun mxcToUrl(mxc: String): String {
val pattern = Regex("mxc://([-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6})/([a-zA-Z]+)")
val match = pattern.find(mxc)
2025-03-03 18:42:57 +03:00
return "$homeserver/_matrix/client/v1/media/download/${match?.groupValues[1]}/${match?.groupValues[2]}"
2025-02-21 14:44:48 +03:00
}