From 4bdd5c1aa0b4e8dece4873bfca3ad09d7aca1646 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 12 Feb 2023 20:03:31 +0530 Subject: [PATCH] Use HttpUrl in ProxyHelper. --- .../github/libretube/helpers/ProxyHelper.kt | 38 ++++--------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt index a1f05117d..175fbd740 100644 --- a/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ProxyHelper.kt @@ -2,51 +2,29 @@ package com.github.libretube.helpers import com.github.libretube.api.RetrofitInstance 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.Dispatchers import kotlinx.coroutines.launch +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull 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() { CoroutineScope(Dispatchers.IO).launch { runCatching { RetrofitInstance.api.getConfig().imageProxyUrl?.let { - setImageProxyUrl(it) + PreferenceHelper.putString(PreferenceKeys.IMAGE_PROXY_URL, it) } } } } fun rewriteUrl(url: String?): String? { - url ?: return null + val proxyUrl = PreferenceHelper.getString(PreferenceKeys.IMAGE_PROXY_URL, "") + .toHttpUrlOrNull() ?: return url - val proxyUrl = getImageProxyUrl() - proxyUrl ?: return url - - runCatching { - 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 + return url?.toHttpUrlOrNull()?.newBuilder() + ?.host(proxyUrl.host) + ?.port(proxyUrl.port) + ?.toString() } }