From f475a0882b183a9ed46b390a1f3a77b72b9fa893 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 11 Jan 2020 04:29:56 -0800 Subject: [PATCH] Changing the downloads folder naming AGAIN This time folders are named just the chapter, or scanlator_chapterName if there's a scanlator Migrations now needs a rename to set the chapters from the new to the new new format --- .../java/eu/kanade/tachiyomi/Migrations.kt | 3 ++ .../tachiyomi/data/download/DownloadCache.kt | 2 +- .../data/download/DownloadProvider.kt | 39 ++++++++++++++++++- .../kanade/tachiyomi/ui/main/MainActivity.kt | 2 - 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt index 3008a84d72..25b91118ca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/Migrations.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi +import eu.kanade.tachiyomi.data.download.DownloadProvider import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault @@ -60,6 +61,8 @@ object Migrations { } } } + if (oldVersion < 54) + DownloadProvider(context).renameChaapters() return true } return false 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 9600867369..9061a997ef 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 @@ -121,7 +121,7 @@ class DownloadCache( /** * Renews the downloads cache. */ - private fun renew() { + fun renew() { val onlineSources = sourceManager.getOnlineSources() val sourceDirs = rootDir.dir.listFiles() 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 43edd79714..2cce3e479d 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 @@ -4,11 +4,13 @@ import android.content.Context import android.net.Uri import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.Source +import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.util.DiskUtil import uy.kohesive.injekt.injectLazy @@ -97,7 +99,37 @@ class DownloadProvider(private val context: Context) { */ fun findChapterDirs(chapters: List, manga: Manga, source: Source): List { val mangaDir = findMangaDir(manga, source) ?: return emptyList() - return chapters.flatMap { getValidChapterDirNames(it) }.mapNotNull { mangaDir.findFile(it) } + return chapters.mapNotNull { chp -> + getValidChapterDirNames(chp).mapNotNull { mangaDir.findFile(it) }.firstOrNull() + } + } + + /** + * Renames the chapter folders with id's and removes it + null scanlators + * + * @param chapters the chapters to query. + * @param manga the manga of the chapter. + * @param source the source of the chapter. + */ + fun renameChaapters() { + val db by injectLazy() + val sourceManager by injectLazy() + val mangas = db.getLibraryMangas().executeAsBlocking() + mangas.forEach sfor@{ manga -> + val sourceId = manga.source + val source = sourceManager.get(sourceId) ?: return@sfor + val mangaDir = findMangaDir(manga, source) ?: return@sfor + mangaDir.listFiles()?.forEach { + val nameSplit = it.name?.split("_")?.toMutableList() ?: return@sfor + if (nameSplit.size > 2 && nameSplit.first().first().isDigit()) { + nameSplit.removeAt(0) + val newName = nameSplit.joinToString("_").removePrefix("null_") + it.renameTo(newName) + } + } + } + val cache = DownloadCache(context, this, sourceManager) + cache.renew() } /** @@ -157,7 +189,10 @@ class DownloadProvider(private val context: Context) { * @param chapter the chapter to query. */ fun getChapterDirName(chapter: Chapter): String { - return DiskUtil.buildValidFilename("${chapter.id}_${chapter.scanlator}_${chapter.name}") + return DiskUtil.buildValidFilename( + if (chapter.scanlator != null) "${chapter.scanlator}_${chapter.name}" + else chapter.name + ) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 46099cc878..56969fc2dd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -9,7 +9,6 @@ import android.graphics.Color import android.graphics.Rect import android.os.Build import android.os.Bundle -import android.util.Log import android.view.MotionEvent import android.view.View import android.view.ViewGroup @@ -63,7 +62,6 @@ import kotlinx.android.synthetic.main.main_activity.* import kotlinx.coroutines.delay import timber.log.Timber import uy.kohesive.injekt.injectLazy -import java.lang.Exception import java.util.Date class MainActivity : BaseActivity() {