|
@@ -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()
|
|
|
}
|
|
|
}
|