From 2d2ce3281ebf80c29fe5192936dee1605716e52b Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Wed, 10 Jul 2024 19:30:21 +0800 Subject: [PATCH] Fix Path -> Uri conversion for paths contain '%' This reverts commit ab2e4a8e2d01cfc66c655ce7559b80e561d35465. --- .../com/hippo/files/AndroidFileSystem.kt | 30 +++++++++---------- app/src/main/kotlin/com/hippo/files/Paths.kt | 16 ++++++---- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt b/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt index 2c61516ab..a50ee78ce 100644 --- a/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt +++ b/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt @@ -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 diff --git a/app/src/main/kotlin/com/hippo/files/Paths.kt b/app/src/main/kotlin/com/hippo/files/Paths.kt index 49e3c1f76..78a28603f 100644 --- a/app/src/main/kotlin/com/hippo/files/Paths.kt +++ b/app/src/main/kotlin/com/hippo/files/Paths.kt @@ -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()