Write library cover to library cover cache (#6883)

This commit is contained in:
Andreas 2022-04-08 04:00:17 +02:00 committed by GitHub
parent 2466a079d5
commit 3026ff241b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,8 @@ import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okhttp3.internal.closeQuietly import okhttp3.internal.closeQuietly
import okio.Path.Companion.toOkioPath import okio.Path.Companion.toOkioPath
import okio.buffer
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 import java.net.HttpURLConnection
@ -101,6 +103,11 @@ class MangaCoverFetcher(
val responseBody = checkNotNull(response.body) { "Null response source" } val responseBody = checkNotNull(response.body) { "Null response source" }
try { try {
snapshot = writeToDiskCache(snapshot, response) snapshot = writeToDiskCache(snapshot, response)
if (coverCacheFile != null) {
writeToCoverCache(coverCacheFile, response)
}
// Read from disk cache // Read from disk cache
if (snapshot != null) { if (snapshot != null) {
return SourceResult( 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 { private suspend fun executeNetworkRequest(): Response {
val client = sourceLazy.value?.client ?: callFactoryLazy.value val client = sourceLazy.value?.client ?: callFactoryLazy.value
val response = client.newCall(newRequest()).await() val response = client.newCall(newRequest()).await()
@ -167,7 +189,10 @@ class MangaCoverFetcher(
return if (options.diskCachePolicy.readEnabled) diskCacheLazy.value[diskCacheKey!!] else null 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) { if (!options.diskCachePolicy.writeEnabled) {
snapshot?.closeQuietly() snapshot?.closeQuietly()
return null return null