diff options
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.kt | 124 |
1 files changed, 0 insertions, 124 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 index 1b5e8b99..0115d6fc 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 @@ -16,38 +16,17 @@ */ package com.pitchedapps.frost.facebook.requests -import com.bumptech.glide.Priority -import com.bumptech.glide.RequestBuilder -import com.bumptech.glide.load.DataSource -import com.bumptech.glide.load.Options -import com.bumptech.glide.load.data.DataFetcher -import com.bumptech.glide.load.model.ModelLoader -import com.bumptech.glide.load.model.ModelLoaderFactory -import com.bumptech.glide.load.model.MultiModelLoaderFactory -import com.bumptech.glide.request.RequestOptions -import com.bumptech.glide.request.target.Target -import com.bumptech.glide.signature.ObjectKey -import com.pitchedapps.frost.facebook.FB_IMAGE_ID_MATCHER 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 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 -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() -} /** * Attempts to get the fbcdn url of the supplied image redirect url @@ -65,106 +44,3 @@ suspend fun String.getFullSizedImageUrl(url: String, timeout: Long = 3000): Stri null } } - -/** - * Request loader for a potentially hd version of a url - * In this case, each url may potentially return an id, - * which may potentially be used to fetch a higher res image url - * The following aims to allow such loading while adhering to Glide's lifecycle - */ -data class HdImageMaybe(val url: String, val cookie: String) { - - val id: Long by lazy { FB_IMAGE_ID_MATCHER.find(url)[1]?.toLongOrNull() ?: -1 } - - val isValid: Boolean by lazy { - id != -1L && cookie.isNotBlank() - } -} - -/* - * The following was a test to see if hd image loading would work - * - * It's working and tested, though the improvements aren't really worth the extra data use - * and reload - */ - -class HdImageLoadingFactory : ModelLoaderFactory<HdImageMaybe, InputStream> { - - override fun build(multiFactory: MultiModelLoaderFactory) = HdImageLoading() - - override fun teardown() = Unit -} - -fun <T> RequestBuilder<T>.loadWithPotentialHd(model: HdImageMaybe) = - thumbnail(clone().load(model.url)) - .load(model) - .apply(RequestOptions().override(Target.SIZE_ORIGINAL)) - -class HdImageLoading : ModelLoader<HdImageMaybe, InputStream> { - - override fun buildLoadData( - model: HdImageMaybe, - width: Int, - height: Int, - options: Options - ): ModelLoader.LoadData<InputStream>? = - if (!model.isValid) null - else ModelLoader.LoadData(ObjectKey(model), HdImageFetcher(model)) - - override fun handles(model: HdImageMaybe) = model.isValid -} - -class HdImageFetcher(private val model: HdImageMaybe) : 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>) { - if (!model.isValid) return callback.fail("Model is invalid") - val result: Result<InputStream?> = runCatching { - runBlocking { - withTimeout(20000L) { - val auth = fbAuth.fetch(model.cookie).await() - if (cancelled) throw RuntimeException("Cancelled") - val url = auth.getFullSizedImage(model.id).invoke() - ?: throw RuntimeException("Null url") - if (cancelled) throw RuntimeException("Cancelled") - if (!url.contains("png") && !url.contains("jpg")) throw RuntimeException("Invalid format") - urlCall?.execute()?.body()?.byteStream() - } - } - } - if (result.isSuccess) - callback.onDataReady(result.getOrNull()) - else - callback.onLoadFailed( - result.exceptionOrNull() as? Exception ?: RuntimeException("Failed") - ) - } - - override fun cleanup() { - try { - inputStream?.close() - } catch (e: IOException) { - } finally { - inputStream = null - } - } - - override fun cancel() { - cancelled = true - urlCall?.cancel() - urlCall = null - cleanup() - } -} |