Prevent okhttp from caching page previews
This commit is contained in:
parent
81faf22562
commit
20ae97cd98
@ -12,6 +12,7 @@ import coil.request.Options
|
||||
import coil.request.Parameters
|
||||
import eu.kanade.domain.manga.model.PagePreview
|
||||
import eu.kanade.tachiyomi.data.cache.PagePreviewCache
|
||||
import eu.kanade.tachiyomi.network.CACHE_CONTROL_NO_STORE
|
||||
import eu.kanade.tachiyomi.network.await
|
||||
import eu.kanade.tachiyomi.source.PagePreviewSource
|
||||
import eu.kanade.tachiyomi.source.SourceManager
|
||||
@ -22,14 +23,13 @@ import okhttp3.CacheControl
|
||||
import okhttp3.Call
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import okhttp3.internal.closeQuietly
|
||||
import okhttp3.internal.http.HTTP_NOT_MODIFIED
|
||||
import okio.Path.Companion.toOkioPath
|
||||
import okio.Source
|
||||
import okio.buffer
|
||||
import okio.sink
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.io.File
|
||||
import java.net.HttpURLConnection
|
||||
|
||||
/**
|
||||
* A [Fetcher] that fetches page preview image for [PagePreview] object.
|
||||
@ -95,7 +95,7 @@ class PagePreviewFetcher(
|
||||
}
|
||||
|
||||
// Read from disk cache
|
||||
snapshot = writeToDiskCache(snapshot, response)
|
||||
snapshot = writeToDiskCache(response)
|
||||
if (snapshot != null) {
|
||||
return SourceResult(
|
||||
source = snapshot.toImageSource(),
|
||||
@ -111,41 +111,34 @@ class PagePreviewFetcher(
|
||||
dataSource = if (response.cacheResponse != null) DataSource.DISK else DataSource.NETWORK,
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
responseBody.closeQuietly()
|
||||
responseBody.close()
|
||||
throw e
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
snapshot?.closeQuietly()
|
||||
snapshot?.close()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun executeNetworkRequest(): Response {
|
||||
val response = sourceLazy.value?.fetchPreviewImage(page.getPagePreviewInfo(), getCacheControl()) ?: callFactoryLazy.value.newCall(newRequest()).await()
|
||||
if (!response.isSuccessful && response.code != HttpURLConnection.HTTP_NOT_MODIFIED) {
|
||||
response.body?.closeQuietly()
|
||||
if (!response.isSuccessful && response.code != HTTP_NOT_MODIFIED) {
|
||||
response.close()
|
||||
throw HttpException(response)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
private fun getCacheControl(): CacheControl? {
|
||||
val diskRead = options.diskCachePolicy.readEnabled
|
||||
val networkRead = options.networkCachePolicy.readEnabled
|
||||
private fun getCacheControl(): CacheControl {
|
||||
return when {
|
||||
!networkRead && diskRead -> {
|
||||
CacheControl.FORCE_CACHE
|
||||
options.networkCachePolicy.readEnabled -> {
|
||||
// don't take up okhttp cache
|
||||
CACHE_CONTROL_NO_STORE
|
||||
}
|
||||
networkRead && !diskRead -> if (options.diskCachePolicy.writeEnabled) {
|
||||
CacheControl.FORCE_NETWORK
|
||||
} else {
|
||||
CACHE_CONTROL_FORCE_NETWORK_NO_CACHE
|
||||
}
|
||||
!networkRead && !diskRead -> {
|
||||
else -> {
|
||||
// This causes the request to fail with a 504 Unsatisfiable Request.
|
||||
CACHE_CONTROL_NO_NETWORK_NO_CACHE
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,10 +149,7 @@ class PagePreviewFetcher(
|
||||
// Support attaching custom data to the network request.
|
||||
.tag(Parameters::class.java, options.parameters)
|
||||
|
||||
val cacheControl = getCacheControl()
|
||||
if (cacheControl != null) {
|
||||
request.cacheControl(cacheControl)
|
||||
}
|
||||
request.cacheControl(getCacheControl())
|
||||
|
||||
return request.build()
|
||||
}
|
||||
@ -210,21 +200,12 @@ class PagePreviewFetcher(
|
||||
}
|
||||
|
||||
private fun writeToDiskCache(
|
||||
snapshot: DiskCache.Snapshot?,
|
||||
response: Response,
|
||||
): DiskCache.Snapshot? {
|
||||
if (!options.diskCachePolicy.writeEnabled) {
|
||||
snapshot?.closeQuietly()
|
||||
return null
|
||||
}
|
||||
val editor = if (snapshot != null) {
|
||||
snapshot.closeAndEdit()
|
||||
} else {
|
||||
diskCacheLazy.value.edit(diskCacheKey)
|
||||
} ?: return null
|
||||
val editor = diskCacheLazy.value.edit(diskCacheKey) ?: return null
|
||||
try {
|
||||
diskCacheLazy.value.fileSystem.write(editor.data) {
|
||||
response.body!!.source().readAll(this)
|
||||
response.body.source().readAll(this)
|
||||
}
|
||||
return editor.commitAndGet()
|
||||
} catch (e: Exception) {
|
||||
@ -262,7 +243,6 @@ class PagePreviewFetcher(
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val CACHE_CONTROL_FORCE_NETWORK_NO_CACHE = CacheControl.Builder().noCache().noStore().build()
|
||||
private val CACHE_CONTROL_NO_NETWORK_NO_CACHE = CacheControl.Builder().noCache().onlyIfCached().build()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user