From 52fa28c16a4f9771dd4214aed1772fde1185d93a Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Mon, 12 Sep 2022 06:00:07 +0800 Subject: [PATCH] Prevent okhttp from caching covers and chapter images (#7967) --- .../kanade/tachiyomi/data/cache/CoverCache.kt | 1 - .../tachiyomi/data/coil/MangaCoverFetcher.kt | 40 ++++++------------- .../eu/kanade/tachiyomi/network/Requests.kt | 1 + .../tachiyomi/source/online/HttpSource.kt | 8 +++- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt index 271b90f740..3ce2983d64 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/CoverCache.kt @@ -11,7 +11,6 @@ import eu.kanade.domain.manga.model.Manga as DomainManga /** * Class used to create cover cache. * It is used to store the covers of the library. - * Makes use of Glide (which can avoid repeating requests) to download covers. * Names of files are created with the md5 of the thumbnail URL. * * @param context the application context. diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt b/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt index 157e77a692..33c3e55b14 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt @@ -14,6 +14,7 @@ import eu.kanade.domain.manga.model.MangaCover import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.coil.MangaCoverFetcher.Companion.USE_CUSTOM_COVER import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.network.CACHE_CONTROL_NO_STORE import eu.kanade.tachiyomi.network.await import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.online.HttpSource @@ -23,14 +24,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 import eu.kanade.domain.manga.model.Manga as DomainManga /** @@ -125,7 +125,7 @@ class MangaCoverFetcher( } // Read from disk cache - snapshot = writeToDiskCache(snapshot, response) + snapshot = writeToDiskCache(response) if (snapshot != null) { return SourceResult( source = snapshot.toImageSource(), @@ -141,11 +141,11 @@ class MangaCoverFetcher( 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 } } @@ -153,8 +153,8 @@ class MangaCoverFetcher( private suspend fun executeNetworkRequest(): Response { val client = sourceLazy.value?.client ?: callFactoryLazy.value val response = client.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 @@ -167,18 +167,12 @@ class MangaCoverFetcher( // Support attaching custom data to the network request. .tag(Parameters::class.java, options.parameters) - val diskRead = options.diskCachePolicy.readEnabled - val networkRead = options.networkCachePolicy.readEnabled when { - !networkRead && diskRead -> { - request.cacheControl(CacheControl.FORCE_CACHE) + options.networkCachePolicy.readEnabled -> { + // don't take up okhttp cache + request.cacheControl(CACHE_CONTROL_NO_STORE) } - networkRead && !diskRead -> if (options.diskCachePolicy.writeEnabled) { - request.cacheControl(CacheControl.FORCE_NETWORK) - } else { - request.cacheControl(CACHE_CONTROL_FORCE_NETWORK_NO_CACHE) - } - !networkRead && !diskRead -> { + else -> { // This causes the request to fail with a 504 Unsatisfiable Request. request.cacheControl(CACHE_CONTROL_NO_NETWORK_NO_CACHE) } @@ -234,18 +228,9 @@ class MangaCoverFetcher( } 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) @@ -349,7 +334,6 @@ class MangaCoverFetcher( companion object { const val USE_CUSTOM_COVER = "use_custom_cover" - 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() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/Requests.kt b/app/src/main/java/eu/kanade/tachiyomi/network/Requests.kt index 8fb5ec2aa8..694f7f47e9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/Requests.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/Requests.kt @@ -10,6 +10,7 @@ import java.util.concurrent.TimeUnit.MINUTES private val DEFAULT_CACHE_CONTROL = CacheControl.Builder().maxAge(10, MINUTES).build() private val DEFAULT_HEADERS = Headers.Builder().build() private val DEFAULT_BODY: RequestBody = FormBody.Builder().build() +internal val CACHE_CONTROL_NO_STORE = CacheControl.Builder().noStore().build() fun GET( url: String, diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt index 3586524166..1d83198c7a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/online/HttpSource.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.source.online +import eu.kanade.tachiyomi.network.CACHE_CONTROL_NO_STORE import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.asObservableSuccess @@ -15,7 +16,6 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import rx.Observable -import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.net.URI import java.net.URISyntaxException @@ -301,7 +301,11 @@ abstract class HttpSource : CatalogueSource { * @param page the page whose source image has to be downloaded. */ fun fetchImage(page: Page): Observable { - return client.newCallWithProgress(imageRequest(page), page) + val request = imageRequest(page).newBuilder() + // images will be cached or saved manually, so don't take up network cache + .cacheControl(CACHE_CONTROL_NO_STORE) + .build() + return client.newCallWithProgress(request, page) .asObservableSuccess() }