Use unified storage location for local source

This commit is contained in:
arkon 2023-11-25 17:06:15 -05:00
parent 21ae04d25d
commit cf9e60fd92
6 changed files with 19 additions and 49 deletions

View file

@ -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<AndroidStorageFolderProvider>()) }
addSingletonFactory { LocalCoverManager(app, get()) }
// Asynchronously init expensive components for a faster cold start

View file

@ -54,9 +54,6 @@ class PreferenceModule(val app: Application) : InjektModule {
addSingletonFactory {
BackupPreferences(get())
}
addSingletonFactory {
AndroidStorageFolderProvider(app)
}
addSingletonFactory {
StoragePreferences(
folderProvider = get<AndroidStorageFolderProvider>(),

View file

@ -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<File> {
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`.
*/

View file

@ -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) {

View file

@ -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<File> {
return DiskUtil.getExternalStorages(context)
.map { File(it.absolutePath, baseFolderLocation) }
.asSequence()
actual fun getBaseDirectory(): File {
return File(folderProvider.directory(), "local")
}
actual fun getFilesInBaseDirectories(): Sequence<File> {
return getBaseDirectories()
// Get all the files inside all baseDir
.flatMap { it.listFiles().orEmpty().toList() }
actual fun getFilesInBaseDirectory(): List<File> {
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<File> {
return getFilesInBaseDirectories()
actual fun getFilesInMangaDirectory(name: String): List<File> {
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

View file

@ -4,11 +4,11 @@ import java.io.File
expect class LocalSourceFileSystem {
fun getBaseDirectories(): Sequence<File>
fun getBaseDirectory(): File
fun getFilesInBaseDirectories(): Sequence<File>
fun getFilesInBaseDirectory(): List<File>
fun getMangaDirectory(name: String): File?
fun getFilesInMangaDirectory(name: String): Sequence<File>
fun getFilesInMangaDirectory(name: String): List<File>
}