From bb861962046055e264be8197a9fbc76e1be59141 Mon Sep 17 00:00:00 2001 From: Eugene Date: Wed, 9 Oct 2019 22:03:41 -0400 Subject: [PATCH] Unique chapter directory names to avoid download conflicts --- .../tachiyomi/data/download/DownloadCache.kt | 2 +- .../data/download/DownloadProvider.kt | 20 ++++++++++++++++--- .../tachiyomi/data/download/Downloader.kt | 6 +----- 3 files changed, 19 insertions(+), 9 deletions(-) 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 bc6d6b42ee..2912cdca57 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 @@ -82,7 +82,7 @@ class DownloadCache( if (sourceDir != null) { val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] if (mangaDir != null) { - return provider.getChapterDirName(chapter) in mangaDir.files + return mangaDir.files.any { it in provider.getValidChapterDirNames(chapter) } } } return false 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 bed8c5e369..087891444d 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 @@ -80,7 +80,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() } /** @@ -92,7 +92,7 @@ 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.flatMap { getValidChapterDirNames(it) }.mapNotNull { mangaDir.findFile(it) } } /** @@ -119,7 +119,21 @@ 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("${chapter.id}_${chapter.scanlator}_${chapter.name}") + } + + /** + * Returns valid downloaded chapter directory names. + * + * @param chapter the chapter to query. + */ + fun getValidChapterDirNames(chapter: Chapter): List { + return listOf( + getChapterDirName(chapter), + + // Legacy chapter directory name used in v0.8.4 and before + DiskUtil.buildValidFilename(chapter.name) + ) } } 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 2492891e69..dc7567898b 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 @@ -232,13 +232,9 @@ class Downloader( // Called in background thread, the operation can be slow with SAF. val chaptersWithoutDir = async { - 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 } }