Remove uses of File#listFiles()
parent
df05211561
commit
9f7ec888a6
@ -0,0 +1,18 @@
|
|||||||
|
package org.koitharu.kotatsu.core.util.ext
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.runInterruptible
|
||||||
|
import java.nio.file.Path
|
||||||
|
import kotlin.io.path.ExperimentalPathApi
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
import kotlin.io.path.fileSize
|
||||||
|
import kotlin.io.path.walk
|
||||||
|
|
||||||
|
@OptIn(ExperimentalPathApi::class)
|
||||||
|
suspend fun Path.computeSize(): Long = runInterruptible(Dispatchers.IO) {
|
||||||
|
// Directories are not included by default
|
||||||
|
walk().sumOf { it.fileSize() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Uri.toPathOrNull() = if (scheme == "file") path?.let { Path(it) } else null
|
||||||
@ -1,31 +1,13 @@
|
|||||||
package org.koitharu.kotatsu.local.data
|
package org.koitharu.kotatsu.local.data
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import java.io.File
|
|
||||||
import java.io.FileFilter
|
|
||||||
import java.io.FilenameFilter
|
|
||||||
import java.util.Locale
|
|
||||||
|
|
||||||
class CbzFilter : FileFilter, FilenameFilter {
|
fun isCbzExtension(string: String): Boolean {
|
||||||
|
return string.equals("cbz", ignoreCase = true) || string.equals("zip", ignoreCase = true)
|
||||||
override fun accept(dir: File, name: String): Boolean {
|
}
|
||||||
return isFileSupported(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun accept(pathname: File?): Boolean {
|
|
||||||
return isFileSupported(pathname?.name ?: return false)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
fun isFileSupported(name: String): Boolean {
|
|
||||||
val ext = name.substringAfterLast('.', "").lowercase(Locale.ROOT)
|
|
||||||
return ext == "cbz" || ext == "zip"
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isUriSupported(uri: Uri): Boolean {
|
fun hasCbzExtension(name: String): Boolean {
|
||||||
val scheme = uri.scheme?.lowercase(Locale.ROOT)
|
return isCbzExtension(name.substringAfterLast('.', ""))
|
||||||
return scheme != null && scheme == "cbz" || scheme == "zip"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun hasCbzExtension(uri: Uri) = uri.scheme?.let { isCbzExtension(it) } ?: false
|
||||||
|
|||||||
@ -1,29 +1,16 @@
|
|||||||
package org.koitharu.kotatsu.local.data
|
package org.koitharu.kotatsu.local.data
|
||||||
|
|
||||||
import java.io.File
|
|
||||||
import java.io.FileFilter
|
|
||||||
import java.io.FilenameFilter
|
|
||||||
import java.util.Locale
|
|
||||||
import java.util.zip.ZipEntry
|
import java.util.zip.ZipEntry
|
||||||
|
|
||||||
class ImageFileFilter : FilenameFilter, FileFilter {
|
fun isImageExtension(string: String): Boolean {
|
||||||
|
return string.equals("png", ignoreCase = true) || string.equals("jpg", ignoreCase = true)
|
||||||
override fun accept(dir: File, name: String): Boolean {
|
|| string.equals("jpeg", ignoreCase = true) || string.equals("webp", ignoreCase = true)
|
||||||
val ext = name.substringAfterLast('.', "").lowercase(Locale.ROOT)
|
}
|
||||||
return isExtensionValid(ext)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun accept(pathname: File?): Boolean {
|
|
||||||
val ext = pathname?.extension?.lowercase(Locale.ROOT) ?: return false
|
|
||||||
return isExtensionValid(ext)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun accept(entry: ZipEntry): Boolean {
|
fun hasImageExtension(name: String): Boolean {
|
||||||
val ext = entry.name.substringAfterLast('.', "").lowercase(Locale.ROOT)
|
return isImageExtension(name.substringAfterLast('.', ""))
|
||||||
return isExtensionValid(ext)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun isExtensionValid(ext: String): Boolean {
|
fun hasImageExtension(entry: ZipEntry): Boolean {
|
||||||
return ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "webp"
|
return hasImageExtension(entry.name)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.local.data
|
|
||||||
|
|
||||||
import java.io.File
|
|
||||||
import java.io.FilenameFilter
|
|
||||||
|
|
||||||
class TempFileFilter : FilenameFilter {
|
|
||||||
|
|
||||||
override fun accept(dir: File, name: String): Boolean {
|
|
||||||
return name.endsWith(".tmp", ignoreCase = true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue