LibreTube/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt

44 lines
1.4 KiB
Kotlin
Raw Normal View History

package com.github.libretube.helpers
2022-11-21 18:42:46 +05:30
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys
2022-12-19 21:28:34 +05:30
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
2023-02-12 20:03:31 +05:30
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
2022-11-21 18:42:46 +05:30
object ProxyHelper {
fun fetchProxyUrl() {
CoroutineScope(Dispatchers.IO).launch {
runCatching {
RetrofitInstance.api.getConfig().imageProxyUrl?.let {
2023-02-12 20:03:31 +05:30
PreferenceHelper.putString(PreferenceKeys.IMAGE_PROXY_URL, it)
2022-11-21 18:42:46 +05:30
}
}
}
}
fun rewriteUrl(url: String?): String? {
2023-02-12 20:03:31 +05:30
val proxyUrl = PreferenceHelper.getString(PreferenceKeys.IMAGE_PROXY_URL, "")
.toHttpUrlOrNull() ?: return url
2022-11-21 18:42:46 +05:30
2023-02-12 20:03:31 +05:30
return url?.toHttpUrlOrNull()?.newBuilder()
?.host(proxyUrl.host)
?.port(proxyUrl.port)
?.toString()
2022-11-21 18:42:46 +05:30
}
2023-03-11 20:41:43 +05:30
/**
* Load the YT url directly instead of the proxied Piped URL if enabled
*/
fun unwrapIfEnabled(url: String): String {
if (!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false)) return url
return url.toHttpUrlOrNull()?.let {
it.newBuilder()
.host(it.queryParameter("host").orEmpty())
.removeAllQueryParameters("host")
.build()
}.toString()
}
2022-11-21 18:42:46 +05:30
}