mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Merge pull request #3052 from Isira-Seneviratne/HttpUrl_ProxyHelper
Use HttpUrl in ProxyHelper.
This commit is contained in:
commit
aba6dcc1d9
@ -2,51 +2,29 @@ package com.github.libretube.helpers
|
|||||||
|
|
||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import java.net.URI
|
|
||||||
import java.net.URLDecoder
|
|
||||||
import java.nio.charset.StandardCharsets
|
|
||||||
import java.util.*
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
|
|
||||||
object ProxyHelper {
|
object ProxyHelper {
|
||||||
private fun getImageProxyUrl(): String? {
|
|
||||||
val url = PreferenceHelper.getString(PreferenceKeys.IMAGE_PROXY_URL, "")
|
|
||||||
return if (url != "") url else null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setImageProxyUrl(url: String) {
|
|
||||||
PreferenceHelper.putString(PreferenceKeys.IMAGE_PROXY_URL, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun fetchProxyUrl() {
|
fun fetchProxyUrl() {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
runCatching {
|
runCatching {
|
||||||
RetrofitInstance.api.getConfig().imageProxyUrl?.let {
|
RetrofitInstance.api.getConfig().imageProxyUrl?.let {
|
||||||
setImageProxyUrl(it)
|
PreferenceHelper.putString(PreferenceKeys.IMAGE_PROXY_URL, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun rewriteUrl(url: String?): String? {
|
fun rewriteUrl(url: String?): String? {
|
||||||
url ?: return null
|
val proxyUrl = PreferenceHelper.getString(PreferenceKeys.IMAGE_PROXY_URL, "")
|
||||||
|
.toHttpUrlOrNull() ?: return url
|
||||||
|
|
||||||
val proxyUrl = getImageProxyUrl()
|
return url?.toHttpUrlOrNull()?.newBuilder()
|
||||||
proxyUrl ?: return url
|
?.host(proxyUrl.host)
|
||||||
|
?.port(proxyUrl.port)
|
||||||
runCatching {
|
?.toString()
|
||||||
val originalUri = URI(url)
|
|
||||||
val newUri = URI(
|
|
||||||
originalUri.scheme.lowercase(Locale.US),
|
|
||||||
URI(proxyUrl).authority,
|
|
||||||
originalUri.path,
|
|
||||||
originalUri.query,
|
|
||||||
originalUri.fragment
|
|
||||||
)
|
|
||||||
return URLDecoder.decode(newUri.toString(), StandardCharsets.UTF_8.toString())
|
|
||||||
}
|
|
||||||
return url
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user