Prevent long-running requests to make the app crash when user is logging out.

This commit is contained in:
Antoine POPINEAU 2020-06-21 14:16:30 +02:00
parent 490de25b05
commit a19e500f09
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
3 changed files with 37 additions and 25 deletions

View File

@ -29,10 +29,10 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
track.downloaded = downloaded.contains(track.id) track.downloaded = downloaded.contains(track.id)
track.bestUpload()?.let { upload -> track.bestUpload()?.let { upload ->
val url = mustNormalizeUrl(upload.listen_url) maybeNormalizeUrl(upload.listen_url)?.let { url ->
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L) track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
} }
}
track track
} }

View File

@ -65,6 +65,7 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
} }
suspend fun get(url: String): Result<R, FuelError> { suspend fun get(url: String): Result<R, FuelError> {
return try {
val request = Fuel.get(mustNormalizeUrl(url)).apply { val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) { if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}") header("Authorization", "Bearer ${Settings.getAccessToken()}")
@ -77,10 +78,14 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
return retryGet(url) return retryGet(url)
} }
return result result
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
} }
private suspend fun retryGet(url: String): Result<R, FuelError> { private suspend fun retryGet(url: String): Result<R, FuelError> {
return try {
return if (HTTP.refresh()) { return if (HTTP.refresh()) {
val request = Fuel.get(mustNormalizeUrl(url)).apply { val request = Fuel.get(mustNormalizeUrl(url)).apply {
if (!Settings.isAnonymous()) { if (!Settings.isAnonymous()) {
@ -92,5 +97,8 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
} else { } else {
Result.Failure(FuelError.wrap(RefreshError)) Result.Failure(FuelError.wrap(RefreshError))
} }
} catch (e: Exception) {
Result.error(FuelError.wrap(e))
}
} }
} }

View File

@ -22,9 +22,13 @@ fun Any.log() {
} }
fun maybeNormalizeUrl(rawUrl: String?): String? { fun maybeNormalizeUrl(rawUrl: String?): String? {
try {
if (rawUrl == null || rawUrl.isEmpty()) return null if (rawUrl == null || rawUrl.isEmpty()) return null
return mustNormalizeUrl(rawUrl) return mustNormalizeUrl(rawUrl)
} catch (e: Exception) {
return null
}
} }
fun mustNormalizeUrl(rawUrl: String): String { fun mustNormalizeUrl(rawUrl: String): String {