Clean up redundant v1 thumbnails

This commit is contained in:
FooIbar committed 2024-12-01 17:45:47 +08:00
1 parent 14a59262af
commit 8ae62e21a7
2 files changed
+18 -10

No files matched your search

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