Use nio for File.listFiles() #449

This commit is contained in:
Koitharu
2023-08-09 12:22:36 +03:00
parent 6cc13784e4
commit a5d99db105
8 changed files with 97 additions and 19 deletions

View File

@@ -15,7 +15,9 @@ import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.util.CompositeMutex
import org.koitharu.kotatsu.core.util.ext.children
import org.koitharu.kotatsu.core.util.ext.deleteAwait
import org.koitharu.kotatsu.core.util.ext.filterWith
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
import org.koitharu.kotatsu.local.data.input.LocalMangaInput
import org.koitharu.kotatsu.local.data.output.LocalMangaOutput
@@ -128,9 +130,6 @@ class LocalMangaRepository @Inject constructor(
suspend fun findSavedManga(remoteManga: Manga): LocalManga? {
val files = getAllFiles()
if (files.isEmpty()) {
return null
}
return channelFlow {
for (file in files) {
launch {
@@ -172,7 +171,7 @@ class LocalMangaRepository @Inject constructor(
val dirs = storageManager.getWriteableDirs()
runInterruptible(Dispatchers.IO) {
dirs.flatMap { dir ->
dir.listFiles(TempFileFilter())?.toList().orEmpty()
dir.children().filterWith(TempFileFilter())
}.forEach { file ->
file.deleteRecursively()
}
@@ -189,7 +188,7 @@ class LocalMangaRepository @Inject constructor(
}
private suspend fun getRawList(): ArrayList<LocalManga> {
val files = getAllFiles()
val files = getAllFiles().toList() // TODO remove toList()
return coroutineScope {
val dispatcher = Dispatchers.IO.limitedParallelism(MAX_PARALLELISM)
files.map { file ->
@@ -200,8 +199,8 @@ class LocalMangaRepository @Inject constructor(
}.filterNotNullTo(ArrayList(files.size))
}
private suspend fun getAllFiles() = storageManager.getReadableDirs().flatMap { dir ->
dir.listFiles()?.toList().orEmpty()
private suspend fun getAllFiles() = storageManager.getReadableDirs().asSequence().flatMap { dir ->
dir.children()
}
private fun Collection<LocalManga>.unwrap(): List<Manga> = map { it.manga }

View File

@@ -1,11 +1,16 @@
package org.koitharu.kotatsu.local.data
import java.io.File
import java.io.FileFilter
import java.io.FilenameFilter
class TempFileFilter : FilenameFilter {
class TempFileFilter : FilenameFilter, FileFilter {
override fun accept(dir: File, name: String): Boolean {
return name.endsWith(".tmp", ignoreCase = true)
}
override fun accept(file: File): Boolean {
return file.name.endsWith(".tmp", ignoreCase = true)
}
}

View File

@@ -4,6 +4,7 @@ import androidx.core.net.toFile
import androidx.core.net.toUri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible
import org.koitharu.kotatsu.core.util.AlphanumComparator
import org.koitharu.kotatsu.core.util.ext.listFilesRecursive
import org.koitharu.kotatsu.core.util.ext.longHashCode
import org.koitharu.kotatsu.core.util.ext.toListSorted
@@ -88,7 +89,7 @@ class LocalMangaDirInput(root: File) : LocalMangaInput(root) {
val file = chapter.url.toUri().toFile()
if (file.isDirectory) {
file.listFilesRecursive(ImageFileFilter())
.toListSorted(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.name })
.toListSorted(compareBy(AlphanumComparator()) { x -> x.name })
.map {
val pageUri = it.toUri().toString()
MangaPage(
@@ -104,7 +105,7 @@ class LocalMangaDirInput(root: File) : LocalMangaInput(root) {
.asSequence()
.filter { x -> !x.isDirectory }
.map { it.name }
.toListSorted(org.koitharu.kotatsu.core.util.AlphanumComparator())
.toListSorted(AlphanumComparator())
.map {
val pageUri = zipUri(file, it)
MangaPage(
@@ -121,7 +122,7 @@ class LocalMangaDirInput(root: File) : LocalMangaInput(root) {
private fun String.toHumanReadable() = replace("_", " ").toCamelCase()
private fun getChaptersFiles(): List<File> = root.listFilesRecursive(CbzFilter())
.toListSorted(compareBy(org.koitharu.kotatsu.core.util.AlphanumComparator()) { x -> x.name })
.toListSorted(compareBy(AlphanumComparator()) { x -> x.name })
private fun findFirstImageEntry(): String? {
val filter = ImageFileFilter()