/
HttpUriFetcher.kt
271 lines (244 loc) · 9.78 KB
/
HttpUriFetcher.kt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (C) 2022 panpf <panpfpanpf@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("FoldInitializerAndIfToElvis", "UnnecessaryVariable") // for debug
package com.github.panpf.sketch.fetch
import androidx.annotation.WorkerThread
import androidx.core.net.toUri
import com.github.panpf.sketch.Sketch
import com.github.panpf.sketch.cache.isReadOrWrite
import com.github.panpf.sketch.datasource.ByteArrayDataSource
import com.github.panpf.sketch.datasource.DataFrom.DOWNLOAD_CACHE
import com.github.panpf.sketch.datasource.DataFrom.NETWORK
import com.github.panpf.sketch.datasource.DiskCacheDataSource
import com.github.panpf.sketch.fetch.internal.copyToWithActive
import com.github.panpf.sketch.fetch.internal.getMimeType
import com.github.panpf.sketch.http.HttpStack.Response
import com.github.panpf.sketch.request.Depth
import com.github.panpf.sketch.request.DepthException
import com.github.panpf.sketch.request.ImageRequest
import com.github.panpf.sketch.util.requiredWorkThread
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.isActive
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream
import java.io.IOException
/**
* Support 'http://pexels.com/sample.jpg', 'https://pexels.com/sample.jpg' uri
*/
open class HttpUriFetcher(
val sketch: Sketch,
val request: ImageRequest,
val url: String
) : Fetcher {
companion object {
const val SCHEME = "http"
const val SCHEME_HTTPS = "https"
@Suppress("unused")
@Deprecated("Use SCHEME_HTTPS instead", ReplaceWith("SCHEME_HTTPS"))
const val SCHEME1 = SCHEME_HTTPS
const val MIME_TYPE_TEXT_PLAIN = "text/plain"
}
private val cacheKey = request.uriString
private val downloadCacheLockKey = request.uriString
@WorkerThread
override suspend fun fetch(): Result<FetchResult> {
requiredWorkThread()
val result = if (request.downloadCachePolicy.isReadOrWrite) {
lockDownloadCache {
request.downloadCachePolicy
.takeIf { it.readEnabled }
?.let { readCache() }
?: executeFetch()
}
} else {
executeFetch()
}
return result
}
private suspend fun executeFetch(): Result<FetchResult> {
/* Check depth */
val depth = request.depth
if (depth >= Depth.LOCAL) {
val message = "Request depth limited to $depth. ${request.uriString}"
return Result.failure(DepthException(message))
}
/* execute download */
return withContext(sketch.networkTaskDispatcher) {
// intercept cancel
if (!isActive) {
return@withContext Result.failure(CancellationException())
}
// open connection
val response = try {
sketch.httpStack.getResponse(request, url)
} catch (e: Throwable) {
return@withContext Result.failure(e)
}
// intercept cancel
if (!isActive) {
return@withContext Result.failure(CancellationException())
}
// check code
val httpCode = response.code
if (httpCode != 200) {
val httpMessage = response.message
val message =
"HTTP status must be 200. $httpCode $httpMessage. ${request.uriString}"
return@withContext Result.failure(IOException(message))
}
// Save to download cache
val mimeType = getMimeType(request.uriString, response.contentType)
if (request.downloadCachePolicy.writeEnabled) {
val result = writeCache(response, mimeType)
if (result != null) {
return@withContext result
}
}
// Save to ByteArray
val result = readBytes(response, mimeType)
return@withContext result
}
}
@WorkerThread
private fun readCache(): Result<FetchResult>? {
val downloadCache = sketch.downloadCache
try {
val snapshot = downloadCache[cacheKey] ?: return null
val contentType = try {
snapshot.newMetadataInputStream()
.use { it.bufferedReader().readText() }
.takeIf { it.isNotEmpty() && it.isNotBlank() }
?: throw IOException("contentType disk cache text empty")
} catch (e: Throwable) {
e.printStackTrace()
sketch.logger.w("HttpUriFetcher") {
"Read contentType disk cache failed, removed cache file. " +
"message='${e.message}', '${request.uriString}'"
}
null
}
val mimeType = getMimeType(request.uriString, contentType)
val dataSource =
DiskCacheDataSource(sketch, request, DOWNLOAD_CACHE, snapshot)
return Result.success(FetchResult(dataSource, mimeType))
} catch (e: Throwable) {
return Result.failure(e)
}
}
private fun CoroutineScope.writeCache(
response: Response,
mimeType: String?
): Result<FetchResult>? {
val downloadCache = sketch.downloadCache
// Save image data
val editor = downloadCache.edit(cacheKey) ?: return null
try {
val contentLength = response.contentLength
val readLength = response.content.use { inputStream ->
editor.newOutputStream().buffered().use { outputStream ->
copyToWithActive(request, inputStream, outputStream, contentLength)
}
}
// 'Transform-Encoding: chunked' contentLength is -1
if (contentLength > 0 && readLength != contentLength) {
throw IOException("readLength error. readLength=$readLength, contentLength=$contentLength. ${request.uriString}")
}
// Save contentType
val contentType = response.contentType?.takeIf { it.isNotEmpty() && it.isNotBlank() }
if (contentType != null) {
editor.newMetadataOutputStream().bufferedWriter().use {
it.write(contentType)
}
}
editor.commit()
} catch (e: Throwable) {
editor.abort()
return Result.failure(e)
}
// Build FetchResult
val snapshot = downloadCache[cacheKey]
if (snapshot == null) {
return Result.failure(IOException("Disk cache loss after write. dataKey='$cacheKey'. ${request.uriString}"))
}
val dataSource = if (request.downloadCachePolicy.readEnabled) {
DiskCacheDataSource(sketch, request, NETWORK, snapshot)
} else {
try {
snapshot.newInputStream()
.use { it.readBytes() }
.let { ByteArrayDataSource(sketch, request, NETWORK, it) }
} catch (e: Throwable) {
return Result.failure(e)
}
}
val result = FetchResult(dataSource, mimeType)
return Result.success(result)
}
private fun CoroutineScope.readBytes(
response: Response,
mimeType: String?
): Result<FetchResult> {
val contentLength = response.contentLength
val byteArrayOutputStream = ByteArrayOutputStream()
val readLength = byteArrayOutputStream.use { outputStream ->
response.content.use { inputStream ->
copyToWithActive(request, inputStream, outputStream, contentLength)
}
}
if (contentLength > 0 && readLength != contentLength) {
val message =
"readLength error. readLength=$readLength, contentLength=$contentLength. ${request.uriString}"
return Result.failure(IOException(message))
}
val bytes = byteArrayOutputStream.toByteArray()
val dataSource = ByteArrayDataSource(sketch, request, NETWORK, bytes)
val result = FetchResult(dataSource, mimeType)
return Result.success(result)
}
private suspend fun <R> lockDownloadCache(block: suspend () -> R): R {
val lock: Mutex = sketch.downloadCache.editLock(downloadCacheLockKey)
lock.lock()
try {
return block()
} finally {
lock.unlock()
}
}
class Factory : Fetcher.Factory {
override fun create(sketch: Sketch, request: ImageRequest): HttpUriFetcher? {
val scheme = request.uriString.toUri().scheme
return if (
SCHEME.equals(scheme, ignoreCase = true)
|| SCHEME_HTTPS.equals(scheme, ignoreCase = true)
) {
HttpUriFetcher(sketch, request, request.uriString)
} else {
null
}
}
override fun toString(): String = "HttpUriFetcher"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
return true
}
override fun hashCode(): Int {
return javaClass.hashCode()
}
}
}