From 04ef20177ca24b77ca207e14397766196425cb4e Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Thu, 3 Jan 2019 23:58:46 -0500 Subject: Wrap full image url in try catch, resolves #1307 --- .../pitchedapps/frost/activities/ImageActivity.kt | 2 +- .../pitchedapps/frost/facebook/requests/Images.kt | 22 +++++++++++++++------- .../frost/facebook/requests/FbFullImageTest.kt | 5 ++++- 3 files changed, 20 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index 7735c034..f7dcf845 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -150,7 +150,7 @@ class ImageActivity : KauBaseActivity() { L.i { "Displaying image" } trueImageUrl = async(Dispatchers.IO) { val result = if (!imageUrl.isIndirectImageUrl) imageUrl - else cookie?.getFullSizedImageUrl(imageUrl)?.blockingGet() ?: imageUrl + else cookie?.getFullSizedImageUrl(imageUrl) ?: imageUrl if (result != imageUrl) L.v { "Launching with true url $result" } result diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt index 9e92c939..ee18e15e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt @@ -32,8 +32,10 @@ import com.pitchedapps.frost.facebook.FB_REDIRECT_URL_MATCHER import com.pitchedapps.frost.facebook.FB_URL_BASE import com.pitchedapps.frost.facebook.formattedFbUrl import com.pitchedapps.frost.facebook.get -import io.reactivex.Maybe +import com.pitchedapps.frost.utils.L +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeout import okhttp3.Call import java.io.IOException @@ -50,12 +52,18 @@ fun RequestAuth.getFullSizedImage(fbid: Long) = frostRequest(::getJsonUrl) { /** * Attempts to get the fbcdn url of the supplied image redirect url */ -fun String.getFullSizedImageUrl(url: String): Maybe = Maybe.fromCallable { - val redirect = requestBuilder().url(url).get().call() - .execute().body()?.string() ?: return@fromCallable null - return@fromCallable FB_REDIRECT_URL_MATCHER.find(redirect)[1]?.formattedFbUrl - ?: return@fromCallable null -}.onErrorComplete() +suspend fun String.getFullSizedImageUrl(url: String, timeout: Long = 3000): String? = withContext(Dispatchers.IO) { + try { + withTimeout(timeout) { + val redirect = requestBuilder().url(url).get().call() + .execute().body()?.string() ?: return@withTimeout null + FB_REDIRECT_URL_MATCHER.find(redirect)[1]?.formattedFbUrl + } + } catch (e: Exception) { + L.e(e) { "Failed to load full size image url" } + null + } +} /** * Request loader for a potentially hd version of a url diff --git a/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt index 4eb90d74..cb8dd5e1 100644 --- a/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt +++ b/app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt @@ -18,6 +18,7 @@ package com.pitchedapps.frost.facebook.requests import com.pitchedapps.frost.internal.COOKIE import com.pitchedapps.frost.internal.authDependent +import kotlinx.coroutines.runBlocking import org.junit.BeforeClass import org.junit.Test import kotlin.test.assertNotNull @@ -38,7 +39,9 @@ class FbFullImageTest { @Test fun getFullImage() { val url = "https://touch.facebook.com/photo/view_full_size/?fbid=107368839645039" - val result = COOKIE.getFullSizedImageUrl(url).blockingGet() + val result = runBlocking { + COOKIE.getFullSizedImageUrl(url) + } assertNotNull(result) println(result) } -- cgit v1.2.3