From 149b549d8d27d9731a0b82b8c5e3bb1c5c6f160c Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 15 Jun 2025 23:15:10 +0200 Subject: [PATCH] Handle chapter marked as downloaded without downloaded files (#1448) In case a chapter was marked as downloaded but did not have any downloaded files, an uncaught exception was thrown --- .../manga/impl/chapter/ChapterForDownload.kt | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt index f7c44e7a..b1852d86 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt @@ -68,19 +68,37 @@ private class ChapterForDownload( suspend fun asDownloadReady(): ChapterDataClass { val log = KotlinLogging.logger("${logger.name}::asDownloadReady") + val downloadPageCount = + try { + ChapterDownloadHelper.getImageCount(mangaId, chapterId) + } catch (_: Exception) { + 0 + } val isMarkedAsDownloaded = chapterEntry[ChapterTable.isDownloaded] - val doesFirstPageExist = firstPageExists() - val isDownloaded = isMarkedAsDownloaded || doesFirstPageExist + val dbPageCount = chapterEntry[ChapterTable.pageCount] + val doesDownloadExist = downloadPageCount != 0 + val doPageCountsMatch = dbPageCount == downloadPageCount - log.debug { "isDownloaded= $isDownloaded (isMarkedAsDownloaded= $isMarkedAsDownloaded, doesFirstPageExist= $doesFirstPageExist)" } + log.debug { "isMarkedAsDownloaded= $isMarkedAsDownloaded, dbPageCount= $dbPageCount, downloadPageCount= $downloadPageCount" } - if (!isDownloaded) { + if (!doesDownloadExist) { log.debug { "reset download status and fetch page list" } - markAsNotDownloaded() - + updateDownloadStatus(false) updatePageList() - } else { + + return asDataClass() + } + + if (!isMarkedAsDownloaded) { + log.debug { "mark as downloaded" } + + updateDownloadStatus(true) + } + + if (!doPageCountsMatch) { + log.debug { "use page count of downloaded chapter" } + updatePageCount(ChapterDownloadHelper.getImageCount(mangaId, chapterId)) } @@ -150,11 +168,10 @@ private class ChapterForDownload( ) } - private fun markAsNotDownloaded() { - // chapter may be downloaded but if we are here, then images might be deleted and database data be false + private fun updateDownloadStatus(downloaded: Boolean) { transaction { ChapterTable.update({ (ChapterTable.sourceOrder eq chapterIndex) and (ChapterTable.manga eq mangaId) }) { - it[isDownloaded] = false + it[isDownloaded] = downloaded } } }