blob: 61a94ac5a9cce0959a32ced548d1f11f796e4d80 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()
}
}
|