Change to J2k style downloads while keeping support for the old ones (#3513)

* Change to J2k style downloads while keeping support for the old ones

* Tweaks based on comments in the PR

* Add scanlator to download pending deleter chapter data class

* Inline the foreach functions

* Make the rename function do less lookups

* More rename downloaded chapter tweaks

* Downloader conflict fixing
This commit is contained in:
jobobby04 2020-07-25 10:59:33 -04:00 committed by GitHub
parent 9a55cf880e
commit ee01686ae4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 18 deletions

View File

@ -81,7 +81,7 @@ class DownloadCache(
if (sourceDir != null) { if (sourceDir != null) {
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] val mangaDir = sourceDir.files[provider.getMangaDirName(manga)]
if (mangaDir != null) { if (mangaDir != null) {
return provider.getChapterDirName(chapter) in mangaDir.files return provider.getValidChapterDirNames(chapter).any { it in mangaDir.files }
} }
} }
return false return false
@ -191,9 +191,10 @@ class DownloadCache(
fun removeChapter(chapter: Chapter, manga: Manga) { fun removeChapter(chapter: Chapter, manga: Manga) {
val sourceDir = rootDir.files[manga.source] ?: return val sourceDir = rootDir.files[manga.source] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return
val chapterDirName = provider.getChapterDirName(chapter) provider.getValidChapterDirNames(chapter).forEach {
if (chapterDirName in mangaDir.files) { if (it in mangaDir.files) {
mangaDir.files -= chapterDirName mangaDir.files -= it
}
} }
} }
@ -208,9 +209,10 @@ class DownloadCache(
val sourceDir = rootDir.files[manga.source] ?: return val sourceDir = rootDir.files[manga.source] ?: return
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return
chapters.forEach { chapter -> chapters.forEach { chapter ->
val chapterDirName = provider.getChapterDirName(chapter) provider.getValidChapterDirNames(chapter).forEach {
if (chapterDirName in mangaDir.files) { if (it in mangaDir.files) {
mangaDir.files -= chapterDirName mangaDir.files -= it
}
} }
} }
} }

View File

@ -251,16 +251,17 @@ class DownloadManager(private val context: Context) {
* @param newChapter the target chapter with the new name. * @param newChapter the target chapter with the new name.
*/ */
fun renameChapter(source: Source, manga: Manga, oldChapter: Chapter, newChapter: Chapter) { 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 newName = provider.getChapterDirName(newChapter)
val mangaDir = provider.getMangaDir(manga, source) 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) { if (oldFolder?.renameTo(newName) == true) {
cache.removeChapter(oldChapter, manga) cache.removeChapter(oldChapter, manga)
cache.addChapter(newName, mangaDir, manga) cache.addChapter(newName, mangaDir, manga)
} else { } else {
Timber.e("Could not rename downloaded chapter: %s.", oldName) Timber.e("Could not rename downloaded chapter: %s.", oldNames.joinToString())
} }
} }
} }

View File

@ -130,7 +130,8 @@ class DownloadPendingDeleter(context: Context) {
private data class ChapterEntry( private data class ChapterEntry(
val id: Long, val id: Long,
val url: String, 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. * Returns a chapter entry from a chapter model.
*/ */
private fun Chapter.toEntry(): ChapterEntry { 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.id = id
it.url = url it.url = url
it.name = name it.name = name
it.scanlator = scanlator
} }
} }
} }

View File

@ -88,7 +88,7 @@ class DownloadProvider(private val context: Context) {
*/ */
fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? { fun findChapterDir(chapter: Chapter, manga: Manga, source: Source): UniFile? {
val mangaDir = findMangaDir(manga, source) 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<Chapter>, manga: Manga, source: Source): List<UniFile> { fun findChapterDirs(chapters: List<Chapter>, manga: Manga, source: Source): List<UniFile> {
val mangaDir = findMangaDir(manga, source) ?: return emptyList() 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. * @param chapter the chapter to query.
*/ */
fun getChapterDirName(chapter: Chapter): String { 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<String> {
return listOf(
// Legacy chapter directory name used in v0.9.2 and before
DiskUtil.buildValidFilename(chapter.name),
// New chapter chapter directory name
getChapterDirName(chapter)
)
} }
} }

View File

@ -234,10 +234,8 @@ class Downloader(
val mangaDir = provider.findMangaDir(manga, source) val mangaDir = provider.findMangaDir(manga, source)
chapters chapters
// Avoid downloading chapters with the same name.
.distinctBy { it.name }
// Filter out those already downloaded. // 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. // Add chapters to queue from the start.
.sortedByDescending { it.source_order } .sortedByDescending { it.source_order }
} }