Skip to content

Commit

Permalink
🔀 Merge pull request #136 from vinceglb/fix-path-linux-4
Browse files Browse the repository at this point in the history
🐛  Fix URISyntaxException: Illegal character in path 4
  • Loading branch information
vinceglb authored Oct 6, 2024
2 parents a1b8ee0 + 13d81c9 commit be343d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import org.freedesktop.dbus.types.Variant
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 @@ -191,7 +189,8 @@ internal class XdgFilePickerPortal : PlatformFilePicker {
}

// awt only supports X11
private fun getWindowIdentifier(parentWindow: Window?) = parentWindow?.let { "X11:${Native.getWindowID(it)}" }
private fun getWindowIdentifier(parentWindow: Window?) =
parentWindow?.let { "X11:${Native.getWindowID(it)}" }

private fun getFileChooserObject(connection: DBusConnection) = connection.getRemoteObject(
"org.freedesktop.portal.Desktop",
Expand All @@ -215,9 +214,23 @@ internal class XdgFilePickerPortal : PlatformFilePicker {
@DBusInterfaceName(value = "org.freedesktop.portal.FileChooser")
@Suppress("FunctionName")
internal interface FileChooserDbusInterface : DBusInterface {
fun OpenFile(parentWindow: String, title: String, options: MutableMap<String, Variant<*>>): DBusPath
fun SaveFile(parentWindow: String, title: String, options: MutableMap<String, Variant<*>>): DBusPath
fun SaveFiles(parentWindow: String, title: String, options: MutableMap<String, Variant<*>>): DBusPath
fun OpenFile(
parentWindow: String,
title: String,
options: MutableMap<String, Variant<*>>
): DBusPath

fun SaveFile(
parentWindow: String,
title: String,
options: MutableMap<String, Variant<*>>
): DBusPath

fun SaveFiles(
parentWindow: String,
title: String,
options: MutableMap<String, Variant<*>>
): DBusPath

@DBusBoundProperty(name = "version", access = Access.READ)
fun GetVersion(): UInt32
Expand All @@ -228,29 +241,9 @@ internal class Pair<A, B>(
@field:Position(1) val b: B
) : Tuple()

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)
}
internal fun String.toURI(): URI =
this
.replace(" ", "%20")
.replace("[", "%5B")
.replace("]", "%5D")
.let { URI(it) }
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package io.github.vinceglb.filekit.core.platform.xdg

import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals

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

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

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

@Test
fun testURIWithoutScheme() {
val path = "/home/user/file.txt"
fun testToFixIssue129() {
val path = "file:///home/dik/%D0%A0%D0%B0%D0%B1%D0%BE%D1%87%D0%B8%D0%B9%20%D1%81%D1%82%D0%BE%D0%BB/Ubuntu%20[24.04].torrent"
val uri = path.toURI()
assertEquals("/home/user/file.txt", uri.path)
val file = File(uri)
assertEquals("/home/dik/Рабочий стол/Ubuntu [24.04].torrent", file.path)
}
}

0 comments on commit be343d6

Please sign in to comment.