mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-21 20:47:03 -05:00
Write library cover to library cover cache (#6883)
This commit is contained in:
parent
2466a079d5
commit
3026ff241b
1 changed files with 26 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue