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

37 lines
1.2 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.
*/
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
}
2025-11-04 16:01:14 +03:00
fun mxcToUrl(mxc: String): String? {
2025-11-04 23:32:22 +03:00
val pattern = Regex("mxc://([-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6})/([^#?]+)")
2025-02-22 18:45:55 +03:00
val match = pattern.find(mxc)
2025-11-04 16:01:14 +03:00
if ((match?.groupValues[1] == null) or (match?.groupValues[2] == null)) return null
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
}