Fix Path -> Uri conversion for paths contain '%'

This reverts commit ab2e4a8e2d.
This commit is contained in:
FooIbar committed 2024-07-10 19:44:13 +08:00
1 parent a28d3ba840
commit 2d2ce3281e
2 files changed
+25 -21

No files matched your search

@@ -103,14 +103,14 @@ class AndroidFileSystem(context: Context) : FileSystem() {
}
}
val uri = dir.toUri()
var documentId = DocumentsContract.getDocumentId(uri)
if (uri.isCifsDocument()) {
documentId += '/'
}
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, documentId)
return runCatching {
val uri = dir.toUri()
var documentId = DocumentsContract.getDocumentId(uri)
if (uri.isCifsDocument()) {
documentId += '/'
}
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, documentId)
contentResolver.query(childrenUri, arrayOf(Document.COLUMN_DISPLAY_NAME), null, null, null)?.use { c ->
List(c.count) {
c.moveToNext()
@@ -126,15 +126,15 @@ class AndroidFileSystem(context: Context) : FileSystem() {
return physicalFileSystem.metadataOrNull(path)
}
val uri = path.toUri()
val isMediaUri = uri.authority == MediaStore.AUTHORITY
val projection = if (isMediaUri) {
arrayOf(MediaStore.MediaColumns.MIME_TYPE, MediaStore.MediaColumns.DATE_MODIFIED)
} else {
arrayOf(Document.COLUMN_MIME_TYPE, Document.COLUMN_LAST_MODIFIED)
}
return runCatching {
val uri = path.toUri()
val isMediaUri = uri.authority == MediaStore.AUTHORITY
val projection = if (isMediaUri) {
arrayOf(MediaStore.MediaColumns.MIME_TYPE, MediaStore.MediaColumns.DATE_MODIFIED)
} else {
arrayOf(Document.COLUMN_MIME_TYPE, Document.COLUMN_LAST_MODIFIED)
}
contentResolver.query(uri, projection, null, null, null)?.use { c ->
if (!c.moveToNext()) return null
+10 -6
View File
@@ -37,15 +37,19 @@ fun Path.toUri(): Uri {
return toFile().toUri()
}
val uri = str.replaceFirst("content:/", "content://").replace("#", "%23").toUri()
val paths = uri.pathSegments
return if (paths.size > 3 && paths[0] == "tree") {
val uri = str.replaceFirst("content:/", "content://").toUri()
val path = requireNotNull(uri.encodedPath) { "Invalid path: $str" }
val paths = path.split('/').dropWhile { it.isEmpty() }
return if (paths.size > 4 && paths[0] == "tree") {
uri.buildUpon().apply {
path(null)
repeat(3) { i ->
appendPath(paths[i])
appendEncodedPath(paths[i])
}
appendPath(paths.subList(3, paths.size).joinToString("/").replaceFirst(":/", ":"))
val root = Uri.decode(paths[3])
val prefix = if (root.endsWith(':')) root else "$root/"
val suffix = uri.encodedFragment?.let { "#$it" }.orEmpty()
appendPath(paths.subList(4, paths.size).joinToString("/", prefix, suffix))
}.build()
} else {
uri
@@ -53,7 +57,7 @@ fun Path.toUri(): Uri {
}
fun Uri.toOkioPath() = if (scheme == ContentResolver.SCHEME_FILE) {
path!!
requireNotNull(path) { "Invalid URI: $this" }
} else {
toString()
}.toPath()