2023-01-31 21:13:39 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
}
|