From 8ae62e21a7d9793d1e77deb1ea9e7fcd480abf5c Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 1 Dec 2024 14:15:30 +0800 Subject: [PATCH] Clean up redundant v1 thumbnails --- .../ehviewer/coil/DownloadThumbInterceptor.kt | 20 +++++++++++++------ .../com/hippo/ehviewer/coil/Extensions.kt | 8 ++++---- 2 files changed, 18 insertions(+), 10 deletions(-) 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 c2e011c01..e09cc0c00 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/DownloadThumbInterceptor.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/DownloadThumbInterceptor.kt @@ -8,6 +8,7 @@ import coil3.request.ImageResult import coil3.request.SuccessResult import com.hippo.ehviewer.EhApplication.Companion.imageCache import com.hippo.ehviewer.util.sendTo +import com.hippo.files.delete import com.hippo.files.isDirectory import com.hippo.files.isFile import com.hippo.files.toUri @@ -26,22 +27,29 @@ val ImageRequest.downloadLocation: Path object DownloadThumbInterceptor : Interceptor { override suspend fun intercept(chain: Interceptor.Chain): ImageResult { - val thumb = chain.request.downloadLocation - if (thumb != emptyPath) { + val dir = chain.request.downloadLocation + if (dir != emptyPath) { + val format = (chain.request.data as String).substringAfterLast('.', "") + check(format.isNotBlank()) + val thumb = dir / "thumb.$format" + val v1Thumb = dir / "thumb.jpg" if (thumb.isFile) { val new = chain.request.newBuilder().data(thumb.toUri()).build() val result = chain.withRequest(new).proceed() - if (result is SuccessResult) return result + if (result is SuccessResult) { + if (thumb != v1Thumb) v1Thumb.delete() + return result + } } - val new = chain.request.newBuilder().build() - val result = chain.withRequest(new).proceed() - if (result is SuccessResult && thumb.parent?.isDirectory == true) { + val result = chain.proceed() + if (result is SuccessResult && dir.isDirectory) { // 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 } + if (thumb != v1Thumb) v1Thumb.delete() } return result } 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 dec26d0a5..3ef7d3a9a 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/Extensions.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/Extensions.kt @@ -12,16 +12,16 @@ import com.hippo.ehviewer.client.data.GalleryPreview import com.hippo.ehviewer.client.data.V2GalleryPreview import com.hippo.ehviewer.client.thumbUrl import com.hippo.ehviewer.dao.DownloadInfo +import com.hippo.ehviewer.download.DownloadManager import com.hippo.ehviewer.download.downloadLocation import com.hippo.ehviewer.ktbuilder.execute fun ImageRequest.Builder.ehUrl(info: GalleryInfo) = apply { val key = info.thumbKey!! data(info.thumbUrl) - val format = info.thumbUrl.substringAfterLast('.', "") - check(format.isNotBlank()) - if (info is DownloadInfo && !info.dirname.isNullOrBlank()) { - downloadLocation(downloadLocation / info.dirname / "thumb.$format") + val downloadInfo = (info as? DownloadInfo) ?: DownloadManager.getDownloadInfo(info.gid) + if (downloadInfo != null && !downloadInfo.dirname.isNullOrBlank()) { + downloadLocation(downloadLocation / downloadInfo.dirname) } memoryCacheKey(key) diskCacheKey(key)