From 992ce89bead3cb5b3802ad1d894c465d52978bec Mon Sep 17 00:00:00 2001 From: Artem Chepurnoy Date: Wed, 17 Jan 2024 11:14:35 +0200 Subject: [PATCH] fix: Unable to read text from a file on Desktop --- .../com/artemchep/keyguard/copy/TextServiceJvm.kt | 15 ++++++++------- .../com/artemchep/keyguard/platform/LeUri.kt | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/common/src/desktopMain/kotlin/com/artemchep/keyguard/copy/TextServiceJvm.kt b/common/src/desktopMain/kotlin/com/artemchep/keyguard/copy/TextServiceJvm.kt index 579890b..f20e4e6 100644 --- a/common/src/desktopMain/kotlin/com/artemchep/keyguard/copy/TextServiceJvm.kt +++ b/common/src/desktopMain/kotlin/com/artemchep/keyguard/copy/TextServiceJvm.kt @@ -3,8 +3,9 @@ package com.artemchep.keyguard.copy import com.artemchep.keyguard.common.service.text.TextService import dev.icerock.moko.resources.FileResource import org.kodein.di.DirectDI +import java.io.File import java.io.InputStream -import kotlin.io.path.Path +import java.net.URI class TextServiceJvm() : TextService { constructor( @@ -19,14 +20,14 @@ class TextServiceJvm() : TextService { .getResourceAsStream(filePath)!! override fun readFromFile(uri: String): InputStream { - when { - uri.startsWith("file://") -> { - return Path(uri) - .toFile() - .inputStream() + val parsedUri = URI.create(uri) + return when (parsedUri.scheme) { + "file" -> { + val file = parsedUri.path.let(::File) + file.inputStream() } else -> { - throw IllegalStateException("Unsupported URI protocol.") + throw IllegalStateException("Unsupported URI protocol, could not read '$uri'.") } } } diff --git a/common/src/desktopMain/kotlin/com/artemchep/keyguard/platform/LeUri.kt b/common/src/desktopMain/kotlin/com/artemchep/keyguard/platform/LeUri.kt index 5f444d7..244f523 100644 --- a/common/src/desktopMain/kotlin/com/artemchep/keyguard/platform/LeUri.kt +++ b/common/src/desktopMain/kotlin/com/artemchep/keyguard/platform/LeUri.kt @@ -12,4 +12,4 @@ data class LeUriImpl( actual fun leParseUri(uri: String): LeUri = LeUriImpl(uri) -actual fun leParseUri(file: File): LeUri = LeUriImpl(file.toPath().toString()) +actual fun leParseUri(file: File): LeUri = LeUriImpl(file.toURI().toString())