11package com.tans.tfiletransporter.file
22
3+ import android.content.Context
4+ import android.os.Environment
35import java.io.File
46
57
6- fun File.toFileLeaf (rootDirString : String ): FileLeaf .CommonFileLeaf {
8+ fun File.toFileLeaf (): FileLeaf .CommonFileLeaf {
79 return FileLeaf .CommonFileLeaf (
810 name = name,
9- path = canonicalPath.removePrefix(rootDirString) ,
11+ path = canonicalPath,
1012 size = length(),
1113 lastModified = lastModified()
1214 )
1315}
1416
15- fun File.toDirLeaf (rootDirString : String ): FileLeaf .DirectoryFileLeaf {
17+ fun File.toDirLeaf (): FileLeaf .DirectoryFileLeaf {
1618 return FileLeaf .DirectoryFileLeaf (
1719 name = name,
18- path = canonicalPath.removePrefix(rootDirString) ,
20+ path = canonicalPath,
1921 childrenCount = listFiles()?.size?.toLong() ? : 0L ,
2022 lastModified = lastModified()
2123 )
2224}
2325
24- fun File.childrenLeafs (rootDirString : String ): Pair <List <FileLeaf .DirectoryFileLeaf >, List<FileLeaf.CommonFileLeaf>> {
26+ fun File.childrenLeafs (): Pair <List <FileLeaf .DirectoryFileLeaf >, List<FileLeaf.CommonFileLeaf>> {
2527 val children = listFiles() ? : emptyArray<File >()
2628 val resultFiles = mutableListOf<FileLeaf .CommonFileLeaf >()
2729 val resultDirs = mutableListOf<FileLeaf .DirectoryFileLeaf >()
@@ -30,10 +32,10 @@ fun File.childrenLeafs(rootDirString: String): Pair<List<FileLeaf.DirectoryFileL
3032 if (c.canRead()) {
3133 if (c.isFile) {
3234 if (c.length() > 0 ) {
33- resultFiles.add(c.toFileLeaf(rootDirString ))
35+ resultFiles.add(c.toFileLeaf())
3436 }
3537 } else {
36- resultDirs.add(c.toDirLeaf(rootDirString ))
38+ resultDirs.add(c.toDirLeaf())
3739 }
3840 }
3941 } catch (e: Throwable ) {
@@ -43,35 +45,42 @@ fun File.childrenLeafs(rootDirString: String): Pair<List<FileLeaf.DirectoryFileL
4345 return resultDirs to resultFiles
4446}
4547
46- fun createLocalRootTree (rootFile : File ): FileTree {
48+ fun createLocalRootTree (context : Context ): FileTree {
4749 val fileSeparator = File .separator
48- return if (rootFile.canRead()) {
49- val (dirLeafs, fileLeafs) = rootFile.childrenLeafs(rootFile.canonicalPath)
50- FileTree (
51- dirLeafs = dirLeafs,
52- fileLeafs = fileLeafs,
53- path = fileSeparator,
54- parentTree = null
50+ val defaultStorageFile = Environment .getExternalStorageDirectory()
51+ val othersSdCardFiles =
52+ getSdCardPaths(context, true ).map { File (it) }.filter { ! defaultStorageFile.hasTargetParent(it) }
53+ val defaultStorageLeaf = if (defaultStorageFile.canRead()) {
54+ listOf (
55+ FileLeaf .DirectoryFileLeaf (
56+ name = " Default Storage" ,
57+ path = defaultStorageFile.canonicalPath,
58+ childrenCount = defaultStorageFile.listFiles()?.size?.toLong() ? : 0L ,
59+ lastModified = defaultStorageFile.lastModified()
60+ )
5561 )
5662 } else {
57- FileTree (
58- dirLeafs = emptyList(),
59- fileLeafs = emptyList(),
60- path = fileSeparator,
61- parentTree = null
62- )
63+ emptyList()
64+ }
65+ val sdCardLeafs = othersSdCardFiles.map {
66+ it.toDirLeaf()
6367 }
68+ return FileTree (
69+ dirLeafs = defaultStorageLeaf + sdCardLeafs,
70+ fileLeafs = emptyList(),
71+ path = fileSeparator,
72+ parentTree = null
73+ )
6474}
6575
6676fun FileTree.newLocalSubTree (
67- dirLeaf : FileLeaf .DirectoryFileLeaf ,
68- rootFile : File ): FileTree {
69- val file = File (rootFile, dirLeaf.path)
70- val (dirLeafs, fileLeafs) = file.childrenLeafs(rootFile.canonicalPath)
77+ dirLeaf : FileLeaf .DirectoryFileLeaf ): FileTree {
78+ val file = File (dirLeaf.path)
79+ val (dirLeafs, fileLeafs) = file.childrenLeafs()
7180 return FileTree (
7281 dirLeafs = dirLeafs,
7382 fileLeafs = fileLeafs,
74- path = file.canonicalPath.removePrefix(rootFile.canonicalPath) ,
83+ path = file.canonicalPath,
7584 parentTree = this
7685 )
7786}
0 commit comments