Skip to content

Commit

Permalink
🔀 Merge pull request #134 from vinceglb/fix-path-linux-3
Browse files Browse the repository at this point in the history
🐛 Fix URISyntaxException: Illegal character in path 3
  • Loading branch information
vinceglb authored Oct 6, 2024
2 parents 83cf199 + b1fa4de commit fdca1bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.awt.Window
import java.io.File
import java.net.URI
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.util.UUID

//https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.FileChooser.html
Expand Down Expand Up @@ -179,7 +180,7 @@ internal class XdgFilePickerPortal : PlatformFilePicker {

if (response.toInt() == 0) {
val uris = (results["uris"]!!.value as List<String>).map { path ->
path.URI()
path.toURI()
}
onComplete(uris, this)
} else {
Expand Down Expand Up @@ -227,7 +228,29 @@ internal class Pair<A, B>(
@field:Position(1) val b: B
) : Tuple()

private fun String.URI(): URI = URLEncoder
.encode(this, "UTF-8")
.replace("+", "%20")
.let { URI(it) }
private fun splitUrl(url: String): kotlin.Pair<String?, String> {
val schemeEndIndex = url.indexOf("://")

return if (schemeEndIndex != -1) {
// If "://" is found, split the string into scheme and rest
val scheme = url.substring(0, schemeEndIndex + 3)
val rest = url.substring(schemeEndIndex + 3)
kotlin.Pair(scheme, rest)
} else {
// If no scheme is found, return null for scheme and the entire string as the rest
kotlin.Pair(null, url)
}
}

internal fun String.toURI(): URI {
// Split the URL into scheme and the rest of the path
val (_, rest) = splitUrl(this)

// Encode the rest of the path
val encodedRest = URLEncoder
.encode(rest, StandardCharsets.UTF_8.toString())
.replace("+", "%20") // URLEncoder encodes spaces as '+', so replace with %20

// Reconstruct the toURI with the encoded path
return URI(encodedRest)
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package io.github.vinceglb.filekit.core.platform.xdg

import java.net.URI
import java.net.URLEncoder
import kotlin.test.Test
import kotlin.test.assertEquals

class URITest {
private fun String.URI(): URI = URLEncoder
.encode(this, "UTF-8")
.replace("+", "%20")
.let { URI(it) }

@Test
fun testSimpleURI() {
val path = "/home/user/file.txt"
val uri = path.URI()
assertEquals(path, uri.path)
val path = "image:///home/user/file.txt"
val uri = path.toURI()
assertEquals("/home/user/file.txt", uri.path)
}

@Test
fun testURIWithSpaces() {
val path = "/home/user/file with spaces.txt"
val uri = path.URI()
assertEquals(path, uri.path)
val path = "file:///home/user/file with spaces.txt"
val uri = path.toURI()
assertEquals("/home/user/file with spaces.txt", uri.path)
}

@Test
fun testURIWithSpecialCharacters() {
val path = "/home/user/Ubuntu [24.04].file"
val uri = path.URI()
assertEquals(path, uri.path)
val path = "file:///home/user/Ubuntu [24.04].file"
val uri = path.toURI()
assertEquals("/home/user/Ubuntu [24.04].file", uri.path)
}

@Test
fun testURIWithoutScheme() {
val path = "/home/user/file.txt"
val uri = path.toURI()
assertEquals("/home/user/file.txt", uri.path)
}
}

0 comments on commit fdca1bf

Please sign in to comment.