From 041bafcceadbd5203e95f2692899ac903dd2e883 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Fri, 29 Dec 2017 23:37:10 -0500 Subject: Feature/image retrieval (#581) * Refactor * Attempt new content * Clean up to make compile friendly * Update docs --- .../pitchedapps/frost/facebook/requests/Images.kt | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Images.kt') 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 { + + @Volatile private var cancelled: Boolean = false + private var urlCall: Call? = null + private var inputStream: InputStream? = null + + private fun DataFetcher.DataCallback.fail(msg: String) { + onLoadFailed(RuntimeException(msg)) + } + + override fun getDataClass(): Class = InputStream::class.java + + override fun getDataSource(): DataSource = DataSource.REMOTE + + override fun loadData(priority: Priority, callback: DataFetcher.DataCallback) { + 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() + } +} -- cgit v1.2.3