From 2484b5f14b0ca14e36f89a798810302825aa9bfa Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 28 Jul 2024 21:58:01 +0200 Subject: [PATCH] Fix/downloaded chapters with lost page count (#994) * Persist page count during chapter list update In case a downloaded chapter gets deleted during a chapter list update, the download status was tried to be preserved. However, in case the status could be preserved, the page count was lost and thus, the chapter now was marked as downloaded with a page count of -1. * Mark downloaded chapters without page count as not downloaded --- .../suwayomi/tachidesk/manga/impl/Chapter.kt | 20 +++++++++++++--- ...1_FixDownloadedChaptersWithoutPageCount.kt | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0041_FixDownloadedChaptersWithoutPageCount.kt 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 bb03f1c8..43d8d19b 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt @@ -223,7 +223,7 @@ object Chapter { val deletedChapterNumbers = TreeSet() val deletedReadChapterNumbers = TreeSet() val deletedBookmarkedChapterNumbers = TreeSet() - val deletedDownloadedChapterNumbers = TreeSet() + val deletedDownloadedChapterNumberInfoMap = mutableMapOf>() val deletedChapterNumberDateFetchMap = mutableMapOf() // clear any orphaned/duplicate chapters that are in the db but not in `chapterList` @@ -234,7 +234,13 @@ object Chapter { if (!chapterUrls.contains(dbChapter.url)) { if (dbChapter.read) deletedReadChapterNumbers.add(dbChapter.chapterNumber) if (dbChapter.bookmarked) deletedBookmarkedChapterNumbers.add(dbChapter.chapterNumber) - if (dbChapter.downloaded) deletedDownloadedChapterNumbers.add(dbChapter.chapterNumber) + if (dbChapter.downloaded) { + val pageCountByScanlator = + deletedDownloadedChapterNumberInfoMap.getOrPut( + dbChapter.chapterNumber, + ) { mutableMapOf() } + pageCountByScanlator[dbChapter.scanlator] = dbChapter.pageCount + } deletedChapterNumbers.add(dbChapter.chapterNumber) deletedChapterNumberDateFetchMap[dbChapter.chapterNumber] = dbChapter.fetchedAt dbChapter.id @@ -271,7 +277,15 @@ object Chapter { if (chapter.chapterNumber >= 0f && chapter.chapterNumber in deletedChapterNumbers) { this[ChapterTable.isRead] = chapter.chapterNumber in deletedReadChapterNumbers this[ChapterTable.isBookmarked] = chapter.chapterNumber in deletedBookmarkedChapterNumbers - this[ChapterTable.isDownloaded] = chapter.chapterNumber in deletedDownloadedChapterNumbers + + // 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 diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0041_FixDownloadedChaptersWithoutPageCount.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0041_FixDownloadedChaptersWithoutPageCount.kt new file mode 100644 index 00000000..19690f61 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0041_FixDownloadedChaptersWithoutPageCount.kt @@ -0,0 +1,23 @@ +package suwayomi.tachidesk.server.database.migration + +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +import de.neonew.exposed.migrations.helpers.SQLMigration + +@Suppress("ClassName", "unused") +class M0041_FixDownloadedChaptersWithoutPageCount : SQLMigration() { + override val sql: String = + """ + UPDATE CHAPTER + SET IS_DOWNLOADED = FALSE + WHERE ID IN ( + SELECT ID FROM CHAPTER + WHERE IS_DOWNLOADED = TRUE AND PAGE_COUNT <= 0 + ); + """.trimIndent() +}