From cf9e60fd92b3cea9ab4a48a3b8f62329df388e8d Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Nov 2023 17:06:15 -0500 Subject: [PATCH] Use unified storage location for local source --- .../java/eu/kanade/tachiyomi/di/AppModule.kt | 4 ++- .../kanade/tachiyomi/di/PreferenceModule.kt | 3 --- .../kanade/tachiyomi/util/storage/DiskUtil.kt | 19 ------------- .../tachiyomi/source/local/LocalSource.kt | 9 +++---- .../source/local/io/LocalSourceFileSystem.kt | 27 +++++++------------ .../source/local/io/LocalSourceFileSystem.kt | 6 ++--- 6 files changed, 19 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt b/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt index 6aab7c7bc9..7a527329f6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/di/AppModule.kt @@ -25,6 +25,7 @@ import kotlinx.serialization.json.Json import nl.adaptivity.xmlutil.XmlDeclMode import nl.adaptivity.xmlutil.core.XmlVersion import nl.adaptivity.xmlutil.serialization.XML +import tachiyomi.core.provider.AndroidStorageFolderProvider import tachiyomi.data.AndroidDatabaseHandler import tachiyomi.data.Database import tachiyomi.data.DatabaseHandler @@ -123,7 +124,8 @@ class AppModule(val app: Application) : InjektModule { addSingletonFactory { ImageSaver(app) } - addSingletonFactory { LocalSourceFileSystem(app) } + addSingletonFactory { AndroidStorageFolderProvider(app) } + addSingletonFactory { LocalSourceFileSystem(get()) } addSingletonFactory { LocalCoverManager(app, get()) } // Asynchronously init expensive components for a faster cold start diff --git a/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt b/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt index 91c128ae80..257ff5dd43 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/di/PreferenceModule.kt @@ -54,9 +54,6 @@ class PreferenceModule(val app: Application) : InjektModule { addSingletonFactory { BackupPreferences(get()) } - addSingletonFactory { - AndroidStorageFolderProvider(app) - } addSingletonFactory { StoragePreferences( folderProvider = get(), diff --git a/core/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt b/core/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt index 5ea61b5a5b..fc0e81e654 100644 --- a/core/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt +++ b/core/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt @@ -3,9 +3,7 @@ package eu.kanade.tachiyomi.util.storage import android.content.Context import android.media.MediaScannerConnection import android.net.Uri -import android.os.Environment import android.os.StatFs -import androidx.core.content.ContextCompat import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.util.lang.Hash import java.io.File @@ -64,23 +62,6 @@ object DiskUtil { } } - /** - * Returns the root folders of all the available external storages. - */ - fun getExternalStorages(context: Context): List { - return ContextCompat.getExternalFilesDirs(context, null) - .filterNotNull() - .mapNotNull { - val file = File(it.absolutePath.substringBefore("/Android/")) - val state = Environment.getExternalStorageState(file) - if (state == Environment.MEDIA_MOUNTED || state == Environment.MEDIA_MOUNTED_READ_ONLY) { - file - } else { - null - } - } - } - /** * Don't display downloaded chapters in gallery apps creating `.nomedia`. */ diff --git a/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt b/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt index c3be92d2cb..8e2dc215c2 100644 --- a/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt +++ b/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt @@ -73,7 +73,7 @@ actual class LocalSource( override suspend fun getLatestUpdates(page: Int) = getSearchManga(page, "", LATEST_FILTERS) override suspend fun getSearchManga(page: Int, query: String, filters: FilterList): MangasPage { - val baseDirsFiles = fileSystem.getFilesInBaseDirectories() + val baseDirFiles = fileSystem.getFilesInBaseDirectory() val lastModifiedLimit by lazy { if (filters === LATEST_FILTERS) { System.currentTimeMillis() - LATEST_THRESHOLD @@ -81,7 +81,7 @@ actual class LocalSource( 0L } } - var mangaDirs = baseDirsFiles + var mangaDirs = baseDirFiles // Filter out files that are hidden and is not a folder .filter { it.isDirectory && !it.name.startsWith('.') } .distinctBy { it.name } @@ -308,9 +308,8 @@ actual class LocalSource( fun getFormat(chapter: SChapter): Format { try { - return fileSystem.getBaseDirectories() - .map { dir -> File(dir, chapter.url) } - .find { it.exists() } + return File(fileSystem.getBaseDirectory(), chapter.url) + .takeIf { it.exists() } ?.let(Format.Companion::valueOf) ?: throw Exception(context.stringResource(MR.strings.chapter_not_found)) } catch (e: Format.UnknownFormatException) { diff --git a/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt b/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt index f645f8b755..9e53bc5e28 100644 --- a/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt +++ b/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt @@ -1,37 +1,28 @@ package tachiyomi.source.local.io -import android.content.Context -import eu.kanade.tachiyomi.util.storage.DiskUtil -import tachiyomi.core.i18n.stringResource -import tachiyomi.i18n.MR +import tachiyomi.core.provider.FolderProvider import java.io.File actual class LocalSourceFileSystem( - private val context: Context, + private val folderProvider: FolderProvider, ) { - private val baseFolderLocation = "${context.stringResource(MR.strings.app_name)}${File.separator}local" - - actual fun getBaseDirectories(): Sequence { - return DiskUtil.getExternalStorages(context) - .map { File(it.absolutePath, baseFolderLocation) } - .asSequence() + actual fun getBaseDirectory(): File { + return File(folderProvider.directory(), "local") } - actual fun getFilesInBaseDirectories(): Sequence { - return getBaseDirectories() - // Get all the files inside all baseDir - .flatMap { it.listFiles().orEmpty().toList() } + actual fun getFilesInBaseDirectory(): List { + return getBaseDirectory().listFiles().orEmpty().toList() } actual fun getMangaDirectory(name: String): File? { - return getFilesInBaseDirectories() + return getFilesInBaseDirectory() // Get the first mangaDir or null .firstOrNull { it.isDirectory && it.name == name } } - actual fun getFilesInMangaDirectory(name: String): Sequence { - return getFilesInBaseDirectories() + actual fun getFilesInMangaDirectory(name: String): List { + return getFilesInBaseDirectory() // Filter out ones that are not related to the manga and is not a directory .filter { it.isDirectory && it.name == name } // Get all the files inside the filtered folders diff --git a/source-local/src/commonMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt b/source-local/src/commonMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt index 0440df26e8..f7e46c4159 100644 --- a/source-local/src/commonMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt +++ b/source-local/src/commonMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt @@ -4,11 +4,11 @@ import java.io.File expect class LocalSourceFileSystem { - fun getBaseDirectories(): Sequence + fun getBaseDirectory(): File - fun getFilesInBaseDirectories(): Sequence + fun getFilesInBaseDirectory(): List fun getMangaDirectory(name: String): File? - fun getFilesInMangaDirectory(name: String): Sequence + fun getFilesInMangaDirectory(name: String): List }