Skip to content

Commit

Permalink
fromPublicFolder(DOWNLOADS) causes infinity recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
anggrayudi committed Jun 21, 2021
1 parent f88c09f commit 4eb6306
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
STORAGE_VERSION=0.8.1-SNAPSHOT
STORAGE_VERSION=0.8.1
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ object DocumentFileCompat {
} else {
val file = exploreFile(context, storageId, basePath, documentType, considerRawFile)
if (file == null && basePath.startsWith(Environment.DIRECTORY_DOWNLOADS) && storageId == PRIMARY) {
fromPublicFolder(context, PublicDirectory.DOWNLOADS, basePath.substringAfter('/', ""), considerRawFile = considerRawFile)
?.takeIf {
documentType == DocumentFileType.ANY
|| documentType == DocumentFileType.FILE && it.isFile
|| documentType == DocumentFileType.FOLDER && it.isDirectory
}
val downloads = context.fromTreeUri(Uri.parse(DOWNLOADS_TREE_URI))?.takeIf { it.canRead() } ?: return null
downloads.child(context, basePath.substringAfter('/', ""))?.takeIf {
documentType == DocumentFileType.ANY
|| documentType == DocumentFileType.FILE && it.isFile
|| documentType == DocumentFileType.FOLDER && it.isDirectory
}
} else {
file
}
Expand Down Expand Up @@ -210,7 +210,9 @@ object DocumentFileCompat {
if (subFile.isNotEmpty()) {
rawFile = File("$rawFile/$subFile".trimEnd('/'))
}
if (considerRawFile && rawFile.canRead() && (requiresWriteAccess && rawFile.isWritable(context) || !requiresWriteAccess)) {
if (rawFile.canRead() && (considerRawFile || rawFile.isExternalStorageManager(context))
&& (requiresWriteAccess && rawFile.isWritable(context) || !requiresWriteAccess)
) {
return DocumentFile.fromFile(rawFile)
}

Expand All @@ -227,17 +229,9 @@ object DocumentFileCompat {
but unfortunately cannot create file in the directory. So creating directory with this authority is useless.
Hence, convert it to writable URI with DocumentFile.toWritableDownloadsDocumentFile()
*/
var downloadFolder = context.fromTreeUri(Uri.parse(DOWNLOADS_TREE_URI))
val downloadFolder = context.fromTreeUri(Uri.parse(DOWNLOADS_TREE_URI))
if (downloadFolder?.canRead() == true) {
getDirectorySequence(subFile).forEach {
val directory = downloadFolder?.findFile(it) ?: return null
if (directory.canRead()) {
downloadFolder = directory
} else {
return null
}
}
downloadFolder
downloadFolder.child(context, subFile, requiresWriteAccess)
} else {
fromFullPath(context, rawFile.absolutePath, considerRawFile = false)
}
Expand Down Expand Up @@ -662,11 +656,7 @@ object DocumentFileCompat {
}
}
val file = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
var current = getRootDocumentFile(context, storageId, considerRawFile) ?: return null
getDirectorySequence(basePath).forEach {
current = current.findFile(it) ?: return null
}
current
getRootDocumentFile(context, storageId, considerRawFile)?.child(context, basePath) ?: return null
} else {
val directorySequence = getDirectorySequence(basePath).toMutableList()
val parentTree = ArrayList<String>(directorySequence.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,33 @@ fun DocumentFile.toTreeDocumentFile(context: Context): DocumentFile? {

fun DocumentFile.toMediaFile(context: Context) = if (isTreeDocumentFile) null else MediaFile(context, uri)

/**
* @param path single file name or file path. Empty string returns to itself.
*/
fun DocumentFile.child(context: Context, path: String, requiresWriteAccess: Boolean = false): DocumentFile? {
return when {
path.isEmpty() -> this
isDirectory -> {
val file = if (isRawFile) {
DocumentFile.fromFile(File(uri.path!!, path))
} else {
var currentDirectory = this
DocumentFileCompat.getDirectorySequence(path).forEach {
val directory = currentDirectory.findFile(it) ?: return null
if (directory.canRead()) {
currentDirectory = directory
} else {
return null
}
}
currentDirectory
}
file.takeIf { it.canRead() && (requiresWriteAccess && it.isWritable(context) || !requiresWriteAccess) }
}
else -> null
}
}

/**
* @return File path without storage ID. Returns empty `String` if:
* * It is the root path
Expand Down Expand Up @@ -633,18 +660,10 @@ fun DocumentFile.toWritableDownloadsDocumentFile(context: Context): DocumentFile
// content://com.android.providers.downloads.documents/tree/downloads/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FIKO5
// raw:/storage/emulated/0/Download/IKO5
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && (path.startsWith("/tree/downloads/document/raw:") || path.startsWith("/document/raw:")) -> {
var currentDirectory = DocumentFileCompat.fromPublicFolder(context, PublicDirectory.DOWNLOADS, considerRawFile = false)
?: return null
val downloads = DocumentFileCompat.fromPublicFolder(context, PublicDirectory.DOWNLOADS, considerRawFile = false) ?: return null
val fullPath = path.substringAfterLast("/document/raw:")
DocumentFileCompat.getDirectorySequence(fullPath.substringAfter("/${Environment.DIRECTORY_DOWNLOADS}")).forEach {
val directory = currentDirectory.findFile(it) ?: return null
if (directory.canRead()) {
currentDirectory = directory
} else {
return null
}
}
currentDirectory.takeIf { it.isWritable(context) }
val subFile = fullPath.substringAfter("/${Environment.DIRECTORY_DOWNLOADS}", "")
downloads.child(context, subFile, true)
}

// msd for directories and msf for files
Expand Down
5 changes: 4 additions & 1 deletion storage/src/main/java/com/anggrayudi/storage/file/FileExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ fun File.getStorageType(context: Context) = when {
else -> StorageType.UNKNOWN
}

fun File.child(name: String) = File(this, name)
/**
* @param path single file name or file path
*/
fun File.child(path: String) = File(this, path)

/**
* @see [Context.getDataDir]
Expand Down

0 comments on commit 4eb6306

Please sign in to comment.