diff --git a/CHANGELOG.md b/CHANGELOG.md index 04a2c41f0..af1f545aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - (**WebUI**) Handle serving non-default webui with "bundled" - (**WebUI**) Wait until WebUI is ready to open in browser - (**Downloads**) Truncate filenames by byte length to prevent "File name too long" IO errors +- (**Downloads**) Fix preserving chapter download states during an update - (**Extension**) Do not indicate an update is available when the extension is not installed - (**Chapter**) Fix losing chapter data on failed chapter list update - (**Chapter**) Fix database error when fetching chapter updates diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt index b6990861d..d8b3011bc 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt @@ -236,7 +236,7 @@ object Chapter { val deletedChapterNumbers = TreeSet() val deletedReadChapterNumbers = TreeSet() val deletedBookmarkedChapterNumbers = TreeSet() - val deletedDownloadedChapterNumberInfoMap = mutableMapOf>() + val deletedDownloadedChapterNumberToChapter = mutableMapOf() val deletedChapterNumberDateFetchMap = mutableMapOf() // clear any orphaned/duplicate chapters that are in the db but not in `chapterList` @@ -247,13 +247,7 @@ object Chapter { if (!chapterUrls.contains(dbChapter.url)) { if (dbChapter.read) deletedReadChapterNumbers.add(dbChapter.chapterNumber) if (dbChapter.bookmarked) deletedBookmarkedChapterNumbers.add(dbChapter.chapterNumber) - if (dbChapter.downloaded) { - val pageCountByScanlator = - deletedDownloadedChapterNumberInfoMap.getOrPut( - dbChapter.chapterNumber, - ) { mutableMapOf() } - pageCountByScanlator[dbChapter.scanlator] = dbChapter.pageCount - } + if (dbChapter.downloaded) deletedDownloadedChapterNumberToChapter[dbChapter.chapterNumber] = dbChapter deletedChapterNumbers.add(dbChapter.chapterNumber) deletedChapterNumberDateFetchMap[dbChapter.chapterNumber] = dbChapter.fetchedAt dbChapter.id @@ -292,18 +286,24 @@ object Chapter { this[ChapterTable.isRead] = chapter.chapterNumber in deletedReadChapterNumbers this[ChapterTable.isBookmarked] = chapter.chapterNumber in deletedBookmarkedChapterNumbers - // only preserve download status for chapters of the same scanlator, otherwise, - // the downloaded files won't be found anyway - val downloadedChapterInfo = deletedDownloadedChapterNumberInfoMap[chapter.chapterNumber] - val pageCount = downloadedChapterInfo?.get(chapter.scanlator) - if (pageCount != null) { - this[ChapterTable.isDownloaded] = true - this[ChapterTable.pageCount] = pageCount - } // Try to use the fetch date of the original entry to not pollute 'Updates' tab deletedChapterNumberDateFetchMap[chapter.chapterNumber]?.let { this[ChapterTable.fetchedAt] = it } + + val deletedChapter = deletedDownloadedChapterNumberToChapter[chapter.chapterNumber]!! + + val hasDownloadedPages = deletedChapter.pageCount > 0 + val isSameName = deletedChapter.name == chapter.name + val isSameScanlator = deletedChapter.scanlator == chapter.scanlator + + // Only preserve download status for chapters with the same name and of the same scanlator; otherwise, + // the downloaded files won't be found anyway + val isDownloadPreservable = hasDownloadedPages && isSameName && isSameScanlator + if (isDownloadPreservable) { + this[ChapterTable.isDownloaded] = true + this[ChapterTable.pageCount] = deletedChapter.pageCount + } } }.forEach { insertedChapters.add(ChapterTable.toDataClass(it)) } } @@ -313,12 +313,30 @@ object Chapter { .apply { chaptersToUpdate.forEach { addBatch(EntityID(it.id, ChapterTable)) + + val currentChapter = chaptersInDb.find { dbChapter -> dbChapter.id == it.id }!! + this[ChapterTable.name] = it.name this[ChapterTable.date_upload] = it.uploadDate this[ChapterTable.chapter_number] = it.chapterNumber this[ChapterTable.scanlator] = it.scanlator this[ChapterTable.sourceOrder] = it.index this[ChapterTable.realUrl] = it.realUrl + this[ChapterTable.isDownloaded] = currentChapter.downloaded + this[ChapterTable.pageCount] = currentChapter.pageCount + + if (!currentChapter.downloaded) { + return@forEach + } + + val isSameScanlator = currentChapter.scanlator == it.scanlator + val isSameName = currentChapter.name == it.name + + val isDownloadPreservable = isSameName && isSameScanlator + if (!isDownloadPreservable) { + this[ChapterTable.isDownloaded] = false + this[ChapterTable.pageCount] = -1 + } } }.toExecutable() .execute(this@transaction)