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 151dce1b6..0c8b6f43c 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 @@ -22,6 +22,8 @@ import okhttp3.Request import okhttp3.Response import okhttp3.internal.closeQuietly import okio.Path.Companion.toOkioPath +import okio.buffer +import okio.sink import uy.kohesive.injekt.injectLazy import java.io.File import java.net.HttpURLConnection @@ -101,6 +103,11 @@ class MangaCoverFetcher( val responseBody = checkNotNull(response.body) { "Null response source" } try { snapshot = writeToDiskCache(snapshot, response) + + if (coverCacheFile != null) { + writeToCoverCache(coverCacheFile, response) + } + // Read from disk cache if (snapshot != null) { return SourceResult( @@ -126,6 +133,21 @@ class MangaCoverFetcher( } } + private fun writeToCoverCache(cacheFile: File, response: Response) { + if (!options.diskCachePolicy.writeEnabled) return + try { + response.body!!.source().use { input -> + cacheFile.parentFile?.mkdirs() + if (cacheFile.exists()) { + cacheFile.delete() + } + cacheFile.sink().buffer().use { output -> + output.writeAll(input) + } + } + } catch (_: Exception) {} + } + private suspend fun executeNetworkRequest(): Response { val client = sourceLazy.value?.client ?: callFactoryLazy.value val response = client.newCall(newRequest()).await() @@ -167,7 +189,10 @@ class MangaCoverFetcher( return if (options.diskCachePolicy.readEnabled) diskCacheLazy.value[diskCacheKey!!] else null } - private fun writeToDiskCache(snapshot: DiskCache.Snapshot?, response: Response): DiskCache.Snapshot? { + private fun writeToDiskCache( + snapshot: DiskCache.Snapshot?, + response: Response, + ): DiskCache.Snapshot? { if (!options.diskCachePolicy.writeEnabled) { snapshot?.closeQuietly() return null