Minor cleanup

This commit is contained in:
tarsin committed 2023-03-07 10:32:36 +08:00
1 parent 659afd9245
commit a65a1a05f0
3 files changed
+18 -20

No files matched your search

@@ -18,14 +18,12 @@ package com.hippo.ehviewer.client
private const val URL_PREFIX_THUMB_E = "https://ehgt.org/" private const val URL_PREFIX_THUMB_E = "https://ehgt.org/"
private const val URL_PREFIX_THUMB_EX = "https://exhentai.org/" private const val URL_PREFIX_THUMB_EX = "https://exhentai.org/"
object EhCacheKeyFactory { fun getImageKey(gid: Long, index: Int): String {
fun getImageKey(gid: Long, index: Int): String { return "image:$gid:$index"
return "image:$gid:$index" }
}
fun getPreviewThumbKey(url: String): String { fun getPreviewThumbKey(url: String): String {
return url.removePrefix(URL_PREFIX_THUMB_E) return url.removePrefix(URL_PREFIX_THUMB_E)
.removePrefix(URL_PREFIX_THUMB_EX) .removePrefix(URL_PREFIX_THUMB_EX)
.removePrefix("t/") .removePrefix("t/")
} }
}
@@ -2,10 +2,10 @@ package com.hippo.ehviewer.coil
import android.content.Context import android.content.Context
import coil.request.ImageRequest import coil.request.ImageRequest
import com.hippo.ehviewer.client.EhCacheKeyFactory import com.hippo.ehviewer.client.getPreviewThumbKey
fun ImageRequest.Builder.ehUrl(url: String) = apply { fun ImageRequest.Builder.ehUrl(url: String) = apply {
val key = EhCacheKeyFactory.getPreviewThumbKey(url) val key = getPreviewThumbKey(url)
data(url) data(url)
memoryCacheKey(key) memoryCacheKey(key)
diskCacheKey(key) diskCacheKey(key)
@@ -22,9 +22,9 @@ import coil.disk.DiskCache
import com.hippo.ehviewer.EhApplication import com.hippo.ehviewer.EhApplication
import com.hippo.ehviewer.EhDB import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.Settings import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.EhCacheKeyFactory
import com.hippo.ehviewer.client.EhUtils.getSuitableTitle import com.hippo.ehviewer.client.EhUtils.getSuitableTitle
import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.getImageKey
import com.hippo.ehviewer.client.referer import com.hippo.ehviewer.client.referer
import com.hippo.ehviewer.coil.edit import com.hippo.ehviewer.coil.edit
import com.hippo.ehviewer.coil.read import com.hippo.ehviewer.coil.read
@@ -89,7 +89,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
} }
private fun containInCache(index: Int): Boolean { private fun containInCache(index: Int): Boolean {
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
return sCache[key]?.apply { close() } != null return sCache[key]?.apply { close() } != null
} }
@@ -108,7 +108,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
private fun copyFromCacheToDownloadDir(index: Int): Boolean { private fun copyFromCacheToDownloadDir(index: Int): Boolean {
val dir = downloadDir ?: return false val dir = downloadDir ?: return false
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
return runCatching { return runCatching {
sCache.read(key) { sCache.read(key) {
val extension = fixExtension("." + metadata.toFile().readText()) val extension = fixExtension("." + metadata.toFile().readText())
@@ -142,7 +142,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
} }
private fun removeFromCache(index: Int): Boolean { private fun removeFromCache(index: Int): Boolean {
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
return sCache.remove(key) return sCache.remove(key)
} }
@@ -208,7 +208,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
// Read Mode, allow save to cache // Read Mode, allow save to cache
if (mMode == SpiderQueen.MODE_READ) { if (mMode == SpiderQueen.MODE_READ) {
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
var received: Long = 0 var received: Long = 0
runSuspendCatching { runSuspendCatching {
sCache.edit(key) { sCache.edit(key) {
@@ -227,7 +227,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
fun saveToUniFile(index: Int, file: UniFile): Boolean { fun saveToUniFile(index: Int, file: UniFile): Boolean {
file.openFileDescriptor("w").use { toFd -> file.openFileDescriptor("w").use { toFd ->
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
// Read from diskCache first // Read from diskCache first
sCache[key]?.use { snapshot -> sCache[key]?.use { snapshot ->
@@ -264,7 +264,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
} }
fun getExtension(index: Int): String? { fun getExtension(index: Int): String? {
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
return sCache[key]?.use { it.metadata.toNioPath().readText() } return sCache[key]?.use { it.metadata.toNioPath().readText() }
?: downloadDir?.let { findImageFile(it, index) } ?: downloadDir?.let { findImageFile(it, index) }
?.name.let { FileUtils.getExtensionFromFilename(it) } ?.name.let { FileUtils.getExtensionFromFilename(it) }
@@ -272,7 +272,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
fun getImageSource(index: Int): CloseableSource? { fun getImageSource(index: Int): CloseableSource? {
if (mMode == SpiderQueen.MODE_READ) { if (mMode == SpiderQueen.MODE_READ) {
val key = EhCacheKeyFactory.getImageKey(mGid, index) val key = getImageKey(mGid, index)
val snapshot: DiskCache.Snapshot? = sCache[key] val snapshot: DiskCache.Snapshot? = sCache[key]
if (snapshot != null) { if (snapshot != null) {
val source = ImageDecoder.createSource(snapshot.data.toFile()) val source = ImageDecoder.createSource(snapshot.data.toFile())