mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
Don't create a new HTTP client for each head request
This commit is contained in:
parent
aa537455b8
commit
ada94e0bb4
@ -62,7 +62,7 @@ data class Streams(
|
|||||||
fileName = "${name}_$subCode.srt",
|
fileName = "${name}_$subCode.srt",
|
||||||
path = Paths.get(""),
|
path = Paths.get(""),
|
||||||
url = subtitles.find {
|
url = subtitles.find {
|
||||||
it.code == subtitleCode
|
it.code == subCode
|
||||||
}?.url?.let { ProxyHelper.unwrapUrl(it) },
|
}?.url?.let { ProxyHelper.unwrapUrl(it) },
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package com.github.libretube.helpers
|
package com.github.libretube.helpers
|
||||||
|
|
||||||
|
import com.github.libretube.api.CronetHelper
|
||||||
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.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
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 kotlinx.coroutines.withContext
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
import okhttp3.OkHttpClient
|
|
||||||
import okhttp3.Request
|
|
||||||
|
|
||||||
object ProxyHelper {
|
object ProxyHelper {
|
||||||
fun fetchProxyUrl() {
|
fun fetchProxyUrl() {
|
||||||
@ -33,15 +35,16 @@ object ProxyHelper {
|
|||||||
/**
|
/**
|
||||||
* Detect whether the proxy should be used or not for a given stream URL based on user preferences
|
* Detect whether the proxy should be used or not for a given stream URL based on user preferences
|
||||||
*/
|
*/
|
||||||
fun unwrapStreamUrl(url: String): String {
|
suspend fun unwrapStreamUrl(url: String): String {
|
||||||
return if (shouldDisableProxy(url)) unwrapUrl(url) else url
|
return if (shouldDisableProxy(url)) unwrapUrl(url) else url
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shouldDisableProxy(url: String) = when {
|
suspend fun shouldDisableProxy(url: String) = when {
|
||||||
!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false) -> false
|
!PreferenceHelper.getBoolean(PreferenceKeys.DISABLE_VIDEO_IMAGE_PROXY, false) -> false
|
||||||
PreferenceHelper.getBoolean(PreferenceKeys.FALLBACK_PIPED_PROXY, true) -> isUrlUsable(
|
PreferenceHelper.getBoolean(PreferenceKeys.FALLBACK_PIPED_PROXY, true) -> isUrlUsable(
|
||||||
unwrapUrl(url)
|
unwrapUrl(url)
|
||||||
)
|
)
|
||||||
|
|
||||||
else -> true
|
else -> true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,11 +67,13 @@ object ProxyHelper {
|
|||||||
/**
|
/**
|
||||||
* Parse the Piped url to a YouTube url (or not) based on preferences
|
* Parse the Piped url to a YouTube url (or not) based on preferences
|
||||||
*/
|
*/
|
||||||
private fun isUrlUsable(url: String): Boolean {
|
private suspend fun isUrlUsable(url: String): Boolean = withContext(Dispatchers.IO) {
|
||||||
val client = OkHttpClient.Builder().build()
|
return@withContext runCatching {
|
||||||
val request = Request.Builder().url(url).method("HEAD", null).build()
|
val connection = CronetHelper.cronetEngine.openConnection(URL(url)) as HttpURLConnection
|
||||||
return runCatching {
|
connection.requestMethod = "HEAD"
|
||||||
client.newCall(request).execute().code == 200
|
val isSuccess = connection.responseCode == 200
|
||||||
|
connection.disconnect()
|
||||||
|
return@runCatching isSuccess
|
||||||
}.getOrDefault(false)
|
}.getOrDefault(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user