Skip to content

Commit e1d45cd

Browse files
author
tanpengcheng
committed
[feat] Modify file view request.
1 parent 7b0f13a commit e1d45cd

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

app/src/main/java/com/tans/tfiletransporter/file/FileUtils.kt

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.tans.tfiletransporter.file
22

3+
import android.content.Context
34
import android.os.Build
5+
import android.os.Environment
46
import com.tans.tfiletransporter.transferproto.fileexplore.model.FileExploreDir
57
import com.tans.tfiletransporter.transferproto.fileexplore.model.FileExploreFile
68
import com.tans.tfiletransporter.transferproto.fileexplore.model.ScanDirReq
@@ -9,39 +11,55 @@ import java.io.File
911

1012
val LOCAL_DEVICE = "${Build.BRAND} ${Build.MODEL}"
1113

12-
fun ScanDirReq.scanChildren(rootDir: File): ScanDirResp {
13-
val rootDirString = rootDir.canonicalPath
14-
val currentFile = File(rootDir, requestPath)
15-
return if (currentFile.isDirectory && currentFile.canRead()) {
16-
try {
17-
val children = currentFile.listFiles() ?: emptyArray<File>()
18-
val childrenDirs = mutableListOf<FileExploreDir>()
19-
val childrenFiles = mutableListOf<FileExploreFile>()
20-
for (c in children) {
21-
if (c.canRead()) {
22-
if (c.isDirectory) {
23-
childrenDirs.add(c.toFileExploreDir(rootDirString))
24-
} else {
25-
if (c.length() > 0) {
26-
childrenFiles.add(c.toFileExploreFile(rootDirString))
14+
fun ScanDirReq.scanChildren(context: Context): ScanDirResp {
15+
return try {
16+
val fileSeparator = File.separator
17+
if (requestPath.isBlank() || requestPath == fileSeparator) {
18+
val defaultStorageFile = Environment.getExternalStorageDirectory()
19+
val othersSdCardFiles =
20+
getSdCardPaths(context, true).map { File(it) }.filter { !defaultStorageFile.hasTargetParent(it) }
21+
ScanDirResp(
22+
path = fileSeparator,
23+
childrenFiles = emptyList(),
24+
childrenDirs = listOf(FileExploreDir(
25+
name = "Default Storage",
26+
path = defaultStorageFile.canonicalPath,
27+
childrenCount = defaultStorageFile.listFiles()?.size ?: 0,
28+
lastModify = defaultStorageFile.lastModified()
29+
)) + othersSdCardFiles.map { it.toFileExploreDir() }
30+
)
31+
} else {
32+
val currentFile = File(requestPath)
33+
if (currentFile.isDirectory && currentFile.canRead()) {
34+
val children = currentFile.listFiles() ?: emptyArray<File>()
35+
val childrenDirs = mutableListOf<FileExploreDir>()
36+
val childrenFiles = mutableListOf<FileExploreFile>()
37+
for (c in children) {
38+
if (c.canRead()) {
39+
if (c.isDirectory) {
40+
childrenDirs.add(c.toFileExploreDir())
41+
} else {
42+
if (c.length() > 0) {
43+
childrenFiles.add(c.toFileExploreFile())
44+
}
2745
}
2846
}
2947
}
48+
ScanDirResp(
49+
path = requestPath,
50+
childrenDirs = childrenDirs,
51+
childrenFiles = childrenFiles
52+
)
53+
} else {
54+
ScanDirResp(
55+
path = requestPath,
56+
childrenDirs = emptyList(),
57+
childrenFiles = emptyList()
58+
)
3059
}
31-
ScanDirResp(
32-
path = requestPath,
33-
childrenDirs = childrenDirs,
34-
childrenFiles = childrenFiles
35-
)
36-
} catch (e: Throwable) {
37-
e.printStackTrace()
38-
ScanDirResp(
39-
path = requestPath,
40-
childrenDirs = emptyList(),
41-
childrenFiles = emptyList()
42-
)
4360
}
44-
} else {
61+
} catch (e: Throwable) {
62+
e.printStackTrace()
4563
ScanDirResp(
4664
path = requestPath,
4765
childrenDirs = emptyList(),
@@ -50,11 +68,11 @@ fun ScanDirReq.scanChildren(rootDir: File): ScanDirResp {
5068
}
5169
}
5270

53-
fun File.toFileExploreDir(rootDirString: String): FileExploreDir {
71+
fun File.toFileExploreDir(): FileExploreDir {
5472
return if (isDirectory) {
5573
FileExploreDir(
5674
name = name,
57-
path = this.canonicalPath.removePrefix(rootDirString),
75+
path = this.canonicalPath,
5876
childrenCount = listFiles()?.size ?: 0,
5977
lastModify = lastModified()
6078
)
@@ -63,11 +81,11 @@ fun File.toFileExploreDir(rootDirString: String): FileExploreDir {
6381
}
6482
}
6583

66-
fun File.toFileExploreFile(rootDirString: String): FileExploreFile {
84+
fun File.toFileExploreFile(): FileExploreFile {
6785
return if (isFile) {
6886
FileExploreFile(
6987
name = name,
70-
path = this.canonicalPath.removePrefix(rootDirString),
88+
path = this.canonicalPath,
7189
size = length(),
7290
lastModify = lastModified()
7391
)

app/src/main/java/com/tans/tfiletransporter/ui/activity/filetransport/FileTransportActivity.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
6767
PublishSubject.create<Unit?>().toSerialized()
6868
}
6969

70-
val rootDirFile: File by lazy {
71-
Environment.getExternalStorageDirectory()
72-
}
73-
7470
private val scanDirRequest: FileExploreRequestHandler<ScanDirReq, ScanDirResp> by lazy {
7571
object : FileExploreRequestHandler<ScanDirReq, ScanDirResp> {
7672
override fun onRequest(isNew: Boolean, request: ScanDirReq): ScanDirResp {
7773
return if (Settings.isShareMyDir().blockingGet()) {
78-
request.scanChildren(rootDirFile)
74+
request.scanChildren(this@FileTransportActivity)
7975
} else {
8076
ScanDirResp(
8177
path = request.requestPath,
@@ -338,7 +334,7 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
338334

339335
suspend fun sendFiles(files: List<FileExploreFile>, bufferSize: Long) {
340336
val fixedFiles = files.filter { it.size > 0 }
341-
val senderFiles = fixedFiles.map { SenderFile( File(rootDirFile, it.path), it) }
337+
val senderFiles = fixedFiles.map { SenderFile( File(it.path), it) }
342338
if (senderFiles.isEmpty()) return
343339
sendSenderFiles(senderFiles, bufferSize)
344340
}

app/src/main/java/com/tans/tfiletransporter/ui/activity/filetransport/MyImagesFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class MyImagesFragment : BaseFragment<MyImagesFragmentLayoutBinding, MyImagesFra
120120
if (selectImages.isEmpty()) return@rxSingle
121121
clearImageCaches()
122122
val files = selectImages.createCatches()
123-
val senderFiles = files.map { SenderFile(it, it.toFileExploreFile("")) }
123+
val senderFiles = files.map { SenderFile(it, it.toFileExploreFile()) }
124124
if (senderFiles.isNotEmpty()) {
125125
runCatching {
126126
fileExplore.requestSendFilesSuspend(

0 commit comments

Comments
 (0)