Refine cache key for v2 gallery preview

This commit is contained in:
FooIbar committed 2024-10-29 21:17:45 +08:00
1 parent 0c00ea9292
commit ded11a81a5
7 files changed
+24 -19

No files matched your search

@@ -28,14 +28,19 @@ const val URL_PREFIX_THUMB_E = "https://ehgt.org/"
const val URL_PREFIX_THUMB_EX = "https://s.exhentai.org/"
const val URL_SIGNATURE_THUMB_NORMAL = ".hath.network/c"
private const val URL_PREFIX_V1_THUMB_EX = URL_PREFIX_THUMB_EX + "t/"
private val V2PreviewKeyRegex = Regex("/c([m12]/)[^/]+/(\\d+-\\d+)")
fun getImageKey(gid: Long, index: Int): String = "image:$gid:$index"
fun getThumbKey(url: String): String = url.removePrefix(URL_PREFIX_THUMB_E).removePrefix(URL_PREFIX_V1_THUMB_EX).removePrefix(URL_PREFIX_THUMB_EX)
fun getNormalPreviewKey(url: String) = "$$url"
fun getV2PreviewKey(url: String) = "$".plus(
V2PreviewKeyRegex.find(url)?.let {
it.groupValues[1] + it.groupValues[2]
} ?: url,
)
val String.isNormalPreviewKey
val String.isV2PreviewKey
get() = startsWith('$')
val GalleryInfo.thumbUrl
@@ -26,7 +26,7 @@ sealed interface GalleryPreview : Parcelable {
}
@Parcelize
data class LargeGalleryPreview(
data class V1GalleryPreview(
override val imageKey: String,
override val position: Int,
) : GalleryPreview {
@@ -34,7 +34,7 @@ data class LargeGalleryPreview(
}
@Parcelize
data class NormalGalleryPreview(
data class V2GalleryPreview(
override val url: String,
override val imageKey: String,
override val position: Int,
@@ -29,15 +29,15 @@ import com.hippo.ehviewer.client.data.GalleryDetail
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
import com.hippo.ehviewer.client.data.GalleryTagGroup
import com.hippo.ehviewer.client.data.LargeGalleryPreview
import com.hippo.ehviewer.client.data.NormalGalleryPreview
import com.hippo.ehviewer.client.data.TagNamespace
import com.hippo.ehviewer.client.data.V1GalleryPreview
import com.hippo.ehviewer.client.data.V2GalleryPreview
import com.hippo.ehviewer.client.exception.EhException
import com.hippo.ehviewer.client.exception.OffensiveException
import com.hippo.ehviewer.client.exception.ParseException
import com.hippo.ehviewer.client.exception.PiningException
import com.hippo.ehviewer.client.getNormalPreviewKey
import com.hippo.ehviewer.client.getThumbKey
import com.hippo.ehviewer.client.getV2PreviewKey
import com.hippo.ehviewer.util.toEpochMillis
import com.hippo.ehviewer.util.toFloatOrDefault
import com.hippo.ehviewer.util.toIntOrDefault
@@ -430,12 +430,12 @@ object GalleryDetailParser {
val offset = it.groupValues[6]
if (offset.isEmpty()) {
val imageKey = getThumbKey(url)
LargeGalleryPreview(imageKey, position)
V1GalleryPreview(imageKey, position)
} else {
val imageKey = getNormalPreviewKey(url)
val imageKey = getV2PreviewKey(url)
val width = it.groupValues[3].toInt()
val height = it.groupValues[4].toInt()
NormalGalleryPreview(url, imageKey, position, offset.toInt(), width, height)
V2GalleryPreview(url, imageKey, position, offset.toInt(), width, height)
} to pageUrl
}.run {
first.toList().apply {
@@ -15,7 +15,7 @@ import com.hippo.ehviewer.client.CHROME_ACCEPT
import com.hippo.ehviewer.client.CHROME_ACCEPT_LANGUAGE
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.client.data.NormalGalleryPreview
import com.hippo.ehviewer.client.data.V2GalleryPreview
import com.hippo.ehviewer.spider.DownloadInfoMagics.encodeMagicRequestOrUrl
import io.ktor.http.HttpHeaders
@@ -37,7 +37,7 @@ fun ImageRequest.Builder.ehPreview(preview: GalleryPreview) = with(preview) {
data(url)
memoryCacheKey(imageKey)
diskCacheKey(imageKey)
if (preview is NormalGalleryPreview) size(Size.ORIGINAL)
if (preview is V2GalleryPreview) size(Size.ORIGINAL)
httpHeaders(header)
}
@@ -5,7 +5,7 @@ import coil3.intercept.Interceptor
import coil3.request.ImageResult
import coil3.request.SuccessResult
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.isNormalPreviewKey
import com.hippo.ehviewer.client.isV2PreviewKey
import moe.tarsin.coroutines.NamedMutex
import moe.tarsin.coroutines.withLockNeedSuspend
@@ -14,7 +14,7 @@ object MergeInterceptor : Interceptor {
override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
val req = chain.request
val key = req.memoryCacheKey?.takeIf { it.isNormalPreviewKey || Settings.preloadThumbAggressively }
val key = req.memoryCacheKey?.takeIf { it.isV2PreviewKey || Settings.preloadThumbAggressively }
return if (key != null) {
val (result, suspended) = mutex.withLockNeedSuspend(key) { chain.proceed() }
when (result) {
@@ -24,7 +24,7 @@ import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePainter.State
import coil3.request.ImageRequest
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.client.data.NormalGalleryPreview
import com.hippo.ehviewer.client.data.V2GalleryPreview
import com.hippo.ehviewer.ktbuilder.imageRequest
import com.hippo.ehviewer.ui.tools.CrystalCard
import com.hippo.ehviewer.ui.tools.shouldCrop
@@ -47,7 +47,7 @@ fun EhAsyncPreview(
contentDescription = null,
modifier = modifier,
transform = {
if (it is State.Success && model is NormalGalleryPreview) {
if (it is State.Success && model is V2GalleryPreview) {
with(model) {
it.copy(
painter = BitmapPainter(
@@ -63,7 +63,7 @@ fun EhAsyncPreview(
},
onState = {
if (it is State.Success) {
if (model is NormalGalleryPreview) {
if (model is V2GalleryPreview) {
if (model.shouldCrop) {
contentScale = ContentScale.Crop
}
@@ -1,14 +1,14 @@
package com.hippo.ehviewer.ui.tools
import coil3.Image
import com.hippo.ehviewer.client.data.NormalGalleryPreview
import com.hippo.ehviewer.client.data.V2GalleryPreview
private const val CROP_MIN_ASPECT = 0.5F
private const val CROP_MAX_ASPECT = 0.8f
private val CROP_GOOD_RANGE = CROP_MIN_ASPECT..CROP_MAX_ASPECT
private fun shouldCrop(width: Int, height: Int) = (width.toFloat() / height) in CROP_GOOD_RANGE
val NormalGalleryPreview.shouldCrop: Boolean
val V2GalleryPreview.shouldCrop: Boolean
get() = shouldCrop(clipWidth, clipHeight)
val Image.shouldCrop: Boolean