diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index 900c9dbcf5..9337b1d3bb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -81,7 +81,7 @@ class DownloadCache( if (sourceDir != null) { val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] if (mangaDir != null) { - return provider.getChapterDirName(chapter) in mangaDir.files + return provider.getValidChapterDirNames(chapter).any { it in mangaDir.files } } } return false @@ -191,9 +191,10 @@ class DownloadCache( fun removeChapter(chapter: Chapter, manga: Manga) { val sourceDir = rootDir.files[manga.source] ?: return val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return - val chapterDirName = provider.getChapterDirName(chapter) - if (chapterDirName in mangaDir.files) { - mangaDir.files -= chapterDirName + provider.getValidChapterDirNames(chapter).forEach { + if (it in mangaDir.files) { + mangaDir.files -= it + } } } @@ -208,9 +209,10 @@ class DownloadCache( val sourceDir = rootDir.files[manga.source] ?: return val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return chapters.forEach { chapter -> - val chapterDirName = provider.getChapterDirName(chapter) - if (chapterDirName in mangaDir.files) { - mangaDir.files -= chapterDirName + provider.getValidChapterDirNames(chapter).forEach { + if (it in mangaDir.files) { + mangaDir.files -= it + } } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index c64c953e43..da277fae58 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -251,16 +251,17 @@ class DownloadManager(private val context: Context) { * @param newChapter the target chapter with the new name. */ fun renameChapter(source: Source, manga: Manga, oldChapter: Chapter, newChapter: Chapter) { - val oldName = provider.getChapterDirName(oldChapter) + val oldNames = provider.getValidChapterDirNames(oldChapter) val newName = provider.getChapterDirName(newChapter) val mangaDir = provider.getMangaDir(manga, source) - val oldFolder = mangaDir.findFile(oldName) + // There should only be one folder with the chapter + val oldFolder = oldNames.mapNotNull { mangaDir.findFile(it) }.firstOrNull() if (oldFolder?.renameTo(newName) == true) { cache.removeChapter(oldChapter, manga) cache.addChapter(newName, mangaDir, manga) } else { - Timber.e("Could not rename downloaded chapter: %s.", oldName) + Timber.e("Could not rename downloaded chapter: %s.", oldNames.joinToString()) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt index a4e9fd67a9..ff036006ca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadPendingDeleter.kt @@ -130,7 +130,8 @@ class DownloadPendingDeleter(context: Context) { private data class ChapterEntry( val id: Long, val url: String, - val name: String + val name: String, + val scanlator: String? ) /** @@ -154,7 +155,7 @@ class DownloadPendingDeleter(context: Context) { * Returns a chapter entry from a chapter model. */ private fun Chapter.toEntry(): ChapterEntry { - return ChapterEntry(id!!, url, name) + return ChapterEntry(id!!, url, name, scanlator) } /** @@ -174,6 +175,7 @@ class DownloadPendingDeleter(context: Context) { it.id = id it.url = url it.name = name + it.scanlator = scanlator } } } 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 3bf7a73008..7845dc2e97 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 @@ -88,7 +88,7 @@ class DownloadProvider(private val context: Context) { */ fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? { val mangaDir = findMangaDir(manga, source) - return mangaDir?.findFile(getChapterDirName(chapter)) + return getValidChapterDirNames(chapter).mapNotNull { mangaDir?.findFile(it) }.firstOrNull() } /** @@ -100,7 +100,9 @@ class DownloadProvider(private val context: Context) { */ fun findChapterDirs(chapters: List, manga: Manga, source: Source): List { val mangaDir = findMangaDir(manga, source) ?: return emptyList() - return chapters.mapNotNull { mangaDir.findFile(getChapterDirName(it)) } + return chapters.mapNotNull { chp -> + getValidChapterDirNames(chp).mapNotNull { mangaDir.findFile(it) }.firstOrNull() + } } /** @@ -127,6 +129,23 @@ class DownloadProvider(private val context: Context) { * @param chapter the chapter to query. */ fun getChapterDirName(chapter: Chapter): String { - return DiskUtil.buildValidFilename(chapter.name) + return DiskUtil.buildValidFilename( + if (chapter.scanlator != null) "${chapter.scanlator}_${chapter.name}" + else chapter.name + ) + } + + /** + * Returns valid downloaded chapter directory names. + * + * @param chapter the chapter to query. + */ + fun getValidChapterDirNames(chapter: Chapter): List { + return listOf( + // Legacy chapter directory name used in v0.9.2 and before + DiskUtil.buildValidFilename(chapter.name), + // New chapter chapter directory name + getChapterDirName(chapter) + ) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index bb8f3f1757..cd5f82653c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -234,10 +234,8 @@ class Downloader( val mangaDir = provider.findMangaDir(manga, source) chapters - // Avoid downloading chapters with the same name. - .distinctBy { it.name } // Filter out those already downloaded. - .filter { mangaDir?.findFile(provider.getChapterDirName(it)) == null } + .filter { provider.findChapterDir(it, manga, source) == null } // Add chapters to queue from the start. .sortedByDescending { it.source_order } }