rewrite proxied URLs

This commit is contained in:
Bnyro 2022-11-21 14:12:46 +01:00
parent ef20253db4
commit 592f1b5648
10 changed files with 83 additions and 11 deletions

View File

@ -16,6 +16,7 @@ import com.github.libretube.util.ExceptionHandler
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NotificationHelper import com.github.libretube.util.NotificationHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper
class LibreTubeApp : Application() { class LibreTubeApp : Application() {
override fun onCreate() { override fun onCreate() {
@ -57,6 +58,11 @@ class LibreTubeApp : Application() {
existingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP existingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP
) )
/**
* Fetch the image proxy URL for local playlists and the watch history
*/
ProxyHelper.fetchProxyUrl()
/** /**
* Handler for uncaught exceptions * Handler for uncaught exceptions
*/ */

View File

@ -6,6 +6,7 @@ import com.github.libretube.api.obj.CommentsPage
import com.github.libretube.api.obj.DeleteUserRequest import com.github.libretube.api.obj.DeleteUserRequest
import com.github.libretube.api.obj.Login import com.github.libretube.api.obj.Login
import com.github.libretube.api.obj.Message import com.github.libretube.api.obj.Message
import com.github.libretube.api.obj.PipedConfig
import com.github.libretube.api.obj.Playlist import com.github.libretube.api.obj.Playlist
import com.github.libretube.api.obj.PlaylistId import com.github.libretube.api.obj.PlaylistId
import com.github.libretube.api.obj.Playlists import com.github.libretube.api.obj.Playlists
@ -24,6 +25,9 @@ import retrofit2.http.Path
import retrofit2.http.Query import retrofit2.http.Query
interface PipedApi { interface PipedApi {
@GET("config")
suspend fun getConfig(): PipedConfig
@GET("trending") @GET("trending")
suspend fun getTrending(@Query("region") region: String): List<StreamItem> suspend fun getTrending(@Query("region") region: String): List<StreamItem>

View File

@ -15,6 +15,7 @@ import com.github.libretube.extensions.toLocalPlaylistItem
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.toastFromMainThread import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -56,7 +57,7 @@ object PlaylistsHelper {
}.first { it.playlist.id.toString() == playlistId } }.first { it.playlist.id.toString() == playlistId }
return Playlist( return Playlist(
name = relation.playlist.name, name = relation.playlist.name,
thumbnailUrl = relation.playlist.thumbnailUrl, thumbnailUrl = ProxyHelper.rewriteUrl(relation.playlist.thumbnailUrl),
videos = relation.videos.size, videos = relation.videos.size,
relatedStreams = relation.videos.map { it.toStreamItem() } relatedStreams = relation.videos.map { it.toStreamItem() }
) )

View File

@ -0,0 +1,7 @@
package com.github.libretube.api.obj
data class PipedConfig(
val donationUrl: String? = null,
val statusPageUrl: String? = null,
val imageProxyUrl: String? = null
)

View File

@ -10,6 +10,7 @@ object PreferenceKeys {
const val AUTH_PREF_FILE = "auth" const val AUTH_PREF_FILE = "auth"
const val TOKEN = "token" const val TOKEN = "token"
const val USERNAME = "username" const val USERNAME = "username"
const val IMAGE_PROXY_URL = "image_proxy_url"
/** /**
* General * General

View File

@ -11,7 +11,7 @@ data class WatchHistoryItem(
@ColumnInfo val uploadDate: String? = null, @ColumnInfo val uploadDate: String? = null,
@ColumnInfo val uploader: String? = null, @ColumnInfo val uploader: String? = null,
@ColumnInfo val uploaderUrl: String? = null, @ColumnInfo val uploaderUrl: String? = null,
@ColumnInfo val uploaderAvatar: String? = null, @ColumnInfo var uploaderAvatar: String? = null,
@ColumnInfo val thumbnailUrl: String? = null, @ColumnInfo var thumbnailUrl: String? = null,
@ColumnInfo val duration: Long? = null @ColumnInfo val duration: Long? = null
) )

View File

@ -1,8 +1,10 @@
package com.github.libretube.extensions package com.github.libretube.extensions
import android.util.Log
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Streams import com.github.libretube.api.obj.Streams
import com.github.libretube.db.obj.LocalPlaylistItem import com.github.libretube.db.obj.LocalPlaylistItem
import com.github.libretube.util.ProxyHelper
fun Streams.toStreamItem(videoId: String): StreamItem { fun Streams.toStreamItem(videoId: String): StreamItem {
return StreamItem( return StreamItem(
@ -22,13 +24,14 @@ fun Streams.toStreamItem(videoId: String): StreamItem {
} }
fun LocalPlaylistItem.toStreamItem(): StreamItem { fun LocalPlaylistItem.toStreamItem(): StreamItem {
Log.e("thumb", ProxyHelper.rewriteUrl(thumbnailUrl).toString())
return StreamItem( return StreamItem(
url = videoId, url = videoId,
title = title, title = title,
thumbnail = thumbnailUrl, thumbnail = ProxyHelper.rewriteUrl(thumbnailUrl),
uploaderName = uploader, uploaderName = uploader,
uploaderUrl = uploaderUrl, uploaderUrl = uploaderUrl,
uploaderAvatar = uploaderAvatar, uploaderAvatar = ProxyHelper.rewriteUrl(uploaderAvatar),
uploadedDate = uploadDate, uploadedDate = uploadDate,
uploaded = null, uploaded = null,
duration = duration duration = duration

View File

@ -12,6 +12,7 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
import com.github.libretube.ui.adapters.WatchHistoryAdapter import com.github.libretube.ui.adapters.WatchHistoryAdapter
import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.util.ProxyHelper
class WatchHistoryFragment : BaseFragment() { class WatchHistoryFragment : BaseFragment() {
private lateinit var binding: FragmentWatchHistoryBinding private lateinit var binding: FragmentWatchHistoryBinding
@ -34,6 +35,11 @@ class WatchHistoryFragment : BaseFragment() {
if (watchHistory.isEmpty()) return if (watchHistory.isEmpty()) return
watchHistory.forEach {
it.thumbnailUrl = ProxyHelper.rewriteUrl(it.thumbnailUrl)
it.uploaderAvatar = ProxyHelper.rewriteUrl(it.uploaderAvatar)
}
// reversed order // reversed order
binding.watchHistoryRecView.layoutManager = LinearLayoutManager(requireContext()).apply { binding.watchHistoryRecView.layoutManager = LinearLayoutManager(requireContext()).apply {
reverseLayout = true reverseLayout = true

View File

@ -33,8 +33,8 @@ object PreferenceHelper {
editor.putString(key, value).commit() editor.putString(key, value).commit()
} }
fun getString(key: String?, defValue: String?): String { fun getString(key: String?, defValue: String): String {
return settings.getString(key, defValue)!! return settings.getString(key, defValue) ?: defValue
} }
fun getBoolean(key: String?, defValue: Boolean): Boolean { fun getBoolean(key: String?, defValue: Boolean): Boolean {
@ -49,10 +49,6 @@ object PreferenceHelper {
editor.clear().apply() editor.clear().apply()
} }
fun removePreference(value: String?) {
editor.remove(value).apply()
}
fun getToken(): String { fun getToken(): String {
return authSettings.getString(PreferenceKeys.TOKEN, "")!! return authSettings.getString(PreferenceKeys.TOKEN, "")!!
} }

View File

@ -0,0 +1,48 @@
package com.github.libretube.util
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.net.URI
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)
}
}
}
}
fun rewriteUrl(url: String?): String? {
url ?: return null
val proxyUrl = getImageProxyUrl()
proxyUrl ?: return url
runCatching {
val originalUri = URI(url)
return URI(
originalUri.scheme.lowercase(),
proxyUrl,
originalUri.path,
originalUri.query,
originalUri.fragment
).toString()
}
return url
}
}