From 1f9f9662bc5abe75f24dae30d48e843005d86394 Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Wed, 17 Aug 2022 05:26:08 +0800 Subject: [PATCH] Fix download chapter dir when scanlator is empty or changed (#7780) --- .../interactor/SyncChaptersWithSource.kt | 9 ++++++-- .../eu/kanade/domain/chapter/model/Chapter.kt | 2 +- .../data/download/DownloadProvider.kt | 21 ++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/chapter/interactor/SyncChaptersWithSource.kt b/app/src/main/java/eu/kanade/domain/chapter/interactor/SyncChaptersWithSource.kt index d77cc597d..9bd7ccdc1 100644 --- a/app/src/main/java/eu/kanade/domain/chapter/interactor/SyncChaptersWithSource.kt +++ b/app/src/main/java/eu/kanade/domain/chapter/interactor/SyncChaptersWithSource.kt @@ -103,8 +103,13 @@ class SyncChaptersWithSource( toAdd.add(toAddChapter) } else { if (shouldUpdateDbChapter.await(dbChapter, chapter)) { - if (dbChapter.name != chapter.name && downloadManager.isChapterDownloaded(dbChapter.name, dbChapter.scanlator, manga.title, manga.source)) { - downloadManager.renameChapter(source, manga, dbChapter.toDbChapter(), chapter.toDbChapter()) + downloadManager.run { + val shouldRenameChapter = provider.isChapterDirNameChanged(dbChapter, chapter) && + isChapterDownloaded(dbChapter.name, dbChapter.scanlator, manga.title, manga.source) + + if (shouldRenameChapter) { + renameChapter(source, manga, dbChapter.toDbChapter(), chapter.toDbChapter()) + } } var toChangeChapter = dbChapter.copy( name = chapter.name, diff --git a/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt b/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt index 24da27348..324f9fca1 100644 --- a/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt +++ b/app/src/main/java/eu/kanade/domain/chapter/model/Chapter.kt @@ -37,7 +37,7 @@ data class Chapter( url = sChapter.url, dateUpload = sChapter.date_upload, chapterNumber = sChapter.chapter_number, - scanlator = sChapter.scanlator, + scanlator = sChapter.scanlator?.ifBlank { null }, ) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt index 10ac1b914..4258c3e0b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadProvider.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import logcat.LogPriority import uy.kohesive.injekt.injectLazy +import eu.kanade.domain.chapter.model.Chapter as DomainChapter /** * This class is used to provide the directories where the downloads should be saved. @@ -144,6 +145,10 @@ class DownloadProvider(private val context: Context) { ) } + fun isChapterDirNameChanged(oldChapter: DomainChapter, newChapter: DomainChapter): Boolean { + return oldChapter.name != newChapter.name || oldChapter.scanlator != newChapter.scanlator + } + /** * Returns valid downloaded chapter directory names. * @@ -152,15 +157,21 @@ class DownloadProvider(private val context: Context) { */ fun getValidChapterDirNames(chapterName: String, chapterScanlator: String?): List { val chapterDirName = getChapterDirName(chapterName, chapterScanlator) - return listOf( + return buildList(5) { // Folder of images - chapterDirName, + add(chapterDirName) // Archived chapters - "$chapterDirName.cbz", + add("$chapterDirName.cbz") + + if (chapterScanlator == null) { + // Previously null scanlator fields were converted to "" due to a bug + add("_$chapterDirName") + add("_$chapterDirName.cbz") + } // Legacy chapter directory name used in v0.9.2 and before - DiskUtil.buildValidFilename(chapterName), - ) + add(DiskUtil.buildValidFilename(chapterName)) + } } }