aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-01-03 23:58:46 -0500
committerAllan Wang <me@allanwang.ca>2019-01-03 23:58:46 -0500
commit04ef20177ca24b77ca207e14397766196425cb4e (patch)
tree6523039e02657daadb4e0f2a20f5f9cd3ad3344c
parenta8c734070c67d84e10f617602467339f113521b4 (diff)
downloadfrost-04ef20177ca24b77ca207e14397766196425cb4e.tar.gz
frost-04ef20177ca24b77ca207e14397766196425cb4e.tar.bz2
frost-04ef20177ca24b77ca207e14397766196425cb4e.zip
Wrap full image url in try catch, resolves #1307
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt22
-rw-r--r--app/src/test/kotlin/com/pitchedapps/frost/facebook/requests/FbFullImageTest.kt5
3 files changed, 20 insertions, 9 deletions
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<String?> = 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)
}