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