diff --git a/app/src/main/kotlin/com/hippo/ehviewer/coil/DownloadThumbInterceptor.kt b/app/src/main/kotlin/com/hippo/ehviewer/coil/DownloadThumbInterceptor.kt index 488fa4e5e..5f7232f58 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/DownloadThumbInterceptor.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/DownloadThumbInterceptor.kt @@ -1,41 +1,50 @@ package com.hippo.ehviewer.coil +import coil3.Extras +import coil3.getExtra import coil3.intercept.Interceptor +import coil3.request.ImageRequest import coil3.request.ImageResult import coil3.request.SuccessResult import com.hippo.ehviewer.EhApplication.Companion.imageCache import com.hippo.ehviewer.download.downloadLocation -import com.hippo.ehviewer.spider.DownloadInfoMagics.decodeMagicRequestOrUrl import com.hippo.ehviewer.util.sendTo import com.hippo.files.isDirectory import com.hippo.files.isFile import com.hippo.files.toUri +import okio.Path +import okio.Path.Companion.toPath + +private val emptyPath = "".toPath() +private val downloadLocationKey = Extras.Key(default = emptyPath) + +fun ImageRequest.Builder.downloadLocation(path: Path) = apply { + extras[downloadLocationKey] = path +} + +val ImageRequest.downloadLocation: Path + get() = getExtra(downloadLocationKey) object DownloadThumbInterceptor : Interceptor { - private const val THUMB_FILE = "thumb.jpg" override suspend fun intercept(chain: Interceptor.Chain): ImageResult { - val magicOrUrl = chain.request.data as? String - if (magicOrUrl != null) { - val (url, location) = decodeMagicRequestOrUrl(magicOrUrl) - if (location != null) { - val thumb = downloadLocation / location / THUMB_FILE - if (thumb.isFile) { - val new = chain.request.newBuilder().data(thumb.toUri()).build() - val result = chain.withRequest(new).proceed() - if (result is SuccessResult) return result - } - val new = chain.request.newBuilder().data(url).build() + val thumb = chain.request.downloadLocation + if (thumb != emptyPath) { + if (thumb.isFile) { + val new = chain.request.newBuilder().data(thumb.toUri()).build() val result = chain.withRequest(new).proceed() - if (result is SuccessResult && thumb.parent?.isDirectory == true) { - // Accessing the recreated file immediately after deleting it throws - // FileNotFoundException, so we just overwrite the existing file. - val key = requireNotNull(chain.request.memoryCacheKey) - imageCache.read(key) { - data sendTo thumb - } - } - return result + if (result is SuccessResult) return result } + val new = chain.request.newBuilder().build() + val result = chain.withRequest(new).proceed() + if (result is SuccessResult && thumb.parent?.isDirectory == true) { + // Accessing the recreated file immediately after deleting it throws + // FileNotFoundException, so we just overwrite the existing file. + val key = requireNotNull(chain.request.memoryCacheKey) + imageCache.read(key) { + data sendTo thumb + } + } + return result } return chain.proceed() } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/coil/Extensions.kt b/app/src/main/kotlin/com/hippo/ehviewer/coil/Extensions.kt index 2810e2ef0..7b5ce38fe 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/Extensions.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/Extensions.kt @@ -7,11 +7,16 @@ import coil3.size.Size import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.client.data.GalleryPreview import com.hippo.ehviewer.client.data.V2GalleryPreview -import com.hippo.ehviewer.spider.DownloadInfoMagics.encodeMagicRequestOrUrl +import com.hippo.ehviewer.client.thumbUrl +import com.hippo.ehviewer.dao.DownloadInfo +import com.hippo.ehviewer.download.downloadLocation fun ImageRequest.Builder.ehUrl(info: GalleryInfo) = apply { val key = info.thumbKey!! - data(encodeMagicRequestOrUrl(info)) + data(info.thumbUrl) + if (info is DownloadInfo && !info.dirname.isNullOrBlank()) { + downloadLocation(downloadLocation / info.dirname / "thumb.jpg") + } memoryCacheKey(key) diskCacheKey(key) } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/spider/DownloadInfoMagics.kt b/app/src/main/kotlin/com/hippo/ehviewer/spider/DownloadInfoMagics.kt deleted file mode 100644 index caa600b7d..000000000 --- a/app/src/main/kotlin/com/hippo/ehviewer/spider/DownloadInfoMagics.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.hippo.ehviewer.spider - -import com.hippo.ehviewer.client.data.GalleryInfo -import com.hippo.ehviewer.client.thumbUrl -import com.hippo.ehviewer.dao.DownloadInfo - -object DownloadInfoMagics { - private const val DOWNLOAD_INFO_DIRNAME_URL_MAGIC = "$" - private const val DOWNLOAD_INFO_DIRNAME_URL_SEPARATOR = "|" - - fun encodeMagicRequestOrUrl(info: GalleryInfo): String { - val url = info.thumbUrl - val location = (info as? DownloadInfo)?.dirname - return if (location.isNullOrBlank()) { - url - } else { - DOWNLOAD_INFO_DIRNAME_URL_MAGIC + url + DOWNLOAD_INFO_DIRNAME_URL_SEPARATOR + location - } - } - - fun decodeMagicRequestOrUrl(encoded: String): Pair = if (encoded.startsWith(DOWNLOAD_INFO_DIRNAME_URL_MAGIC)) { - val (a, b) = encoded.removePrefix(DOWNLOAD_INFO_DIRNAME_URL_MAGIC).split(DOWNLOAD_INFO_DIRNAME_URL_SEPARATOR) - a to b - } else { - encoded to null - } -}