aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt67
1 files changed, 67 insertions, 0 deletions
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
new file mode 100644
index 00000000..61a94ac5
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt
@@ -0,0 +1,67 @@
+package com.pitchedapps.frost.facebook.requests
+
+import com.bumptech.glide.Priority
+import com.bumptech.glide.load.DataSource
+import com.bumptech.glide.load.data.DataFetcher
+import com.pitchedapps.frost.facebook.FB_URL_BASE
+import okhttp3.Call
+import okhttp3.Request
+import java.io.IOException
+import java.io.InputStream
+
+/**
+ * Created by Allan Wang on 29/12/17.
+ */
+fun RequestAuth.getFullSizedImage(fbid: Long) = frostRequest(::getJsonUrl) {
+ url("${FB_URL_BASE}photo/view_full_size/?fbid=$fbid&__ajax__=&__user=$userId")
+ get()
+}
+
+class ImageFbidFetcher(private val fbid: Long,
+ private val cookie: String) : DataFetcher<InputStream> {
+
+ @Volatile private var cancelled: Boolean = false
+ private var urlCall: Call? = null
+ private var inputStream: InputStream? = null
+
+ private fun DataFetcher.DataCallback<in InputStream>.fail(msg: String) {
+ onLoadFailed(RuntimeException(msg))
+ }
+
+ override fun getDataClass(): Class<InputStream> = InputStream::class.java
+
+ override fun getDataSource(): DataSource = DataSource.REMOTE
+
+ override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
+ cookie.fbRequest(fail = { callback.fail("Invalid auth") }) {
+ if (cancelled) return@fbRequest callback.fail("Cancelled")
+ val url = getFullSizedImage(fbid).invoke() ?: return@fbRequest callback.fail("Null url")
+ if (cancelled) return@fbRequest callback.fail("Cancelled")
+ urlCall = Request.Builder().url(url).get().call()
+
+ inputStream = try {
+ urlCall?.execute()?.body()?.byteStream()
+ } catch (e: IOException) {
+ null
+ }
+
+ callback.onDataReady(inputStream)
+ }
+ }
+
+ override fun cleanup() {
+ try {
+ inputStream?.close()
+ } catch (e: IOException) {
+ } finally {
+ inputStream = null
+ }
+ }
+
+ override fun cancel() {
+ cancelled = true
+ urlCall?.cancel()
+ urlCall = null
+ cleanup()
+ }
+}