mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 04:33:54 -04:00
#1295 Make FileSearch recursive
This commit is contained in:
parent
6ed73447c1
commit
28b7a2d756
@ -75,13 +75,19 @@ class FileSearch @Inject constructor(private val context: Context) {
|
|||||||
*StorageDeviceUtils.getStorageDevices(context, false).map { it.name }.toTypedArray()
|
*StorageDeviceUtils.getStorageDevices(context, false).map { it.name }.toTypedArray()
|
||||||
)
|
)
|
||||||
|
|
||||||
private fun scanDirectory(directory: String) = filesMatchingExtensions(directory) ?: emptyList()
|
private fun scanDirectory(directory: String): List<File> = File(directory).listFiles()
|
||||||
|
?.fold(
|
||||||
private fun filesMatchingExtensions(directory: String) = File(directory)
|
mutableListOf(), { acc, file ->
|
||||||
.listFiles { _, name -> name.endsWithAny(*zimFileExtensions) }
|
acc.apply {
|
||||||
?.toList()
|
if (file.isDirectory) {
|
||||||
|
addAll(scanDirectory(file.path))
|
||||||
|
} else if (file.extension.isAny(*zimFileExtensions)) {
|
||||||
|
add(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}) ?: emptyList()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun String.endsWithAny(vararg suffixes: String) =
|
internal fun String.isAny(vararg suffixes: String) =
|
||||||
suffixes.fold(false, { acc, s -> acc or endsWith(s) })
|
suffixes.firstOrNull { endsWith(it) } != null
|
||||||
|
@ -48,7 +48,6 @@ class FileSearchTest {
|
|||||||
private val contentResolver: ContentResolver = mockk()
|
private val contentResolver: ContentResolver = mockk()
|
||||||
private val storageDevice: StorageDevice = mockk()
|
private val storageDevice: StorageDevice = mockk()
|
||||||
|
|
||||||
private val unitTestTempDirectoryPath = "unittest${File.separator}"
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
setScheduler(Schedulers.trampoline())
|
setScheduler(Schedulers.trampoline())
|
||||||
@ -89,15 +88,27 @@ class FileSearchTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `scan of directory that has files returns files`() {
|
fun `scan of directory that has files returns files`() {
|
||||||
val zimFile = File.createTempFile("${unitTestTempDirectoryPath}fileToFind", ".zim")
|
val zimFile = File.createTempFile("fileToFind", ".zim")
|
||||||
val zimaaFile = File.createTempFile("${unitTestTempDirectoryPath}fileToFind2", ".zimaa")
|
val zimaaFile = File.createTempFile("fileToFind2", ".zimaa")
|
||||||
File.createTempFile("${unitTestTempDirectoryPath}willNotFind", ".txt")
|
File.createTempFile("willNotFind", ".txt")
|
||||||
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
|
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
|
||||||
val fileList = fileSearch.scan(zimFile.parent)
|
val fileList = fileSearch.scan(zimFile.parent)
|
||||||
.test()
|
.test()
|
||||||
.values()[0]
|
.values()[0]
|
||||||
assertThat(fileList).containsExactlyInAnyOrder(zimFile, zimaaFile)
|
assertThat(fileList).containsExactlyInAnyOrder(zimFile, zimaaFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `scan of directory recursively traverses filesystem`() {
|
||||||
|
val tempRoot = File.createTempFile("tofindroot", "extension")
|
||||||
|
.parentFile.absolutePath
|
||||||
|
val zimFile = File.createTempFile("fileToFind", ".zim", File("${tempRoot}${File.separator}dir").apply { mkdirs() })
|
||||||
|
every { contentResolver.query(any(), any(), any(), any(), any()) } returns null
|
||||||
|
val fileList = fileSearch.scan(zimFile.parentFile.parent)
|
||||||
|
.test()
|
||||||
|
.values()[0]
|
||||||
|
assertThat(fileList).containsExactlyInAnyOrder(zimFile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@ -105,7 +116,7 @@ class FileSearchTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `scan media store, if files are readable they are returned`() {
|
fun `scan media store, if files are readable they are returned`() {
|
||||||
val fileToFind = File.createTempFile("${unitTestTempDirectoryPath}fileToFind", ".zim")
|
val fileToFind = File.createTempFile("fileToFind", ".zim")
|
||||||
expectFromMediaStore(fileToFind)
|
expectFromMediaStore(fileToFind)
|
||||||
fileSearch.scan("")
|
fileSearch.scan("")
|
||||||
.test()
|
.test()
|
||||||
@ -114,7 +125,7 @@ class FileSearchTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `scan media store, if files are not readable they are not returned`() {
|
fun `scan media store, if files are not readable they are not returned`() {
|
||||||
val unreadableFile = File.createTempFile("${unitTestTempDirectoryPath}fileToFind", ".zim")
|
val unreadableFile = File.createTempFile("fileToFind", ".zim")
|
||||||
expectFromMediaStore(unreadableFile)
|
expectFromMediaStore(unreadableFile)
|
||||||
unreadableFile.delete()
|
unreadableFile.delete()
|
||||||
fileSearch.scan("")
|
fileSearch.scan("")
|
||||||
@ -141,7 +152,7 @@ class FileSearchTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun deleteTempDirectory() {
|
private fun deleteTempDirectory() {
|
||||||
File.createTempFile("${unitTestTempDirectoryPath}temp", ".txt")
|
File.createTempFile("temp", ".txt")
|
||||||
.parentFile.deleteRecursively()
|
.parentFile.deleteRecursively()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user