From 13d81c9f800bad1b200a9b1c1e85008cfefbe9ae Mon Sep 17 00:00:00 2001 From: Vincent Guillebaud Date: Sun, 6 Oct 2024 20:09:13 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20Fix=20URISyntaxException:=20I?= =?UTF-8?q?llegal=20character=20in=20path=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/platform/xdg/XdgFilePickerPortal.kt | 57 ++++++++----------- .../filekit/core/platform/xdg/URITest.kt | 19 ++++--- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/filekit-core/src/jvmMain/kotlin/io/github/vinceglb/filekit/core/platform/xdg/XdgFilePickerPortal.kt b/filekit-core/src/jvmMain/kotlin/io/github/vinceglb/filekit/core/platform/xdg/XdgFilePickerPortal.kt index e36e267..941f95c 100644 --- a/filekit-core/src/jvmMain/kotlin/io/github/vinceglb/filekit/core/platform/xdg/XdgFilePickerPortal.kt +++ b/filekit-core/src/jvmMain/kotlin/io/github/vinceglb/filekit/core/platform/xdg/XdgFilePickerPortal.kt @@ -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 @@ -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", @@ -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>): DBusPath - fun SaveFile(parentWindow: String, title: String, options: MutableMap>): DBusPath - fun SaveFiles(parentWindow: String, title: String, options: MutableMap>): DBusPath + fun OpenFile( + parentWindow: String, + title: String, + options: MutableMap> + ): DBusPath + + fun SaveFile( + parentWindow: String, + title: String, + options: MutableMap> + ): DBusPath + + fun SaveFiles( + parentWindow: String, + title: String, + options: MutableMap> + ): DBusPath @DBusBoundProperty(name = "version", access = Access.READ) fun GetVersion(): UInt32 @@ -228,29 +241,9 @@ internal class Pair( @field:Position(1) val b: B ) : Tuple() -private fun splitUrl(url: String): kotlin.Pair { - 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) } diff --git a/filekit-core/src/jvmTest/kotlin/io/github/vinceglb/filekit/core/platform/xdg/URITest.kt b/filekit-core/src/jvmTest/kotlin/io/github/vinceglb/filekit/core/platform/xdg/URITest.kt index 281314c..9bf4121 100644 --- a/filekit-core/src/jvmTest/kotlin/io/github/vinceglb/filekit/core/platform/xdg/URITest.kt +++ b/filekit-core/src/jvmTest/kotlin/io/github/vinceglb/filekit/core/platform/xdg/URITest.kt @@ -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) } }