fix: Unable to read text from a file on Desktop

This commit is contained in:
Artem Chepurnoy 2024-01-17 11:14:35 +02:00
parent 1e6b20268f
commit 992ce89bea
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
2 changed files with 9 additions and 8 deletions

View File

@ -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'.")
}
}
}

View File

@ -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())