Repository navigation
🐛 Fix web serializer message, JVM open fallback, drop unused exception #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+259
−12
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...logs/src/jvmMain/kotlin/io/github/vinceglb/filekit/dialogs/JvmDefaultApplicationOpener.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package io.github.vinceglb.filekit.dialogs | ||
|
|
||
| import io.github.vinceglb.filekit.utils.Platform | ||
| import io.github.vinceglb.filekit.utils.PlatformUtil | ||
| import java.awt.Desktop | ||
| import java.io.File | ||
| import java.io.IOException | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| // Long enough to catch xdg-open failing fast (missing file or tool), short enough for a click handler. | ||
| internal const val XDG_OPEN_EXIT_TIMEOUT_MILLIS: Long = 500 | ||
|
|
||
| /** | ||
| * Opens files with the default application on the JVM. | ||
| * | ||
| * Uses `java.awt.Desktop` when it can open files. Otherwise, on Linux, starts `xdg-open` and waits | ||
| * at most [XDG_OPEN_EXIT_TIMEOUT_MILLIS] for it to report an error. Throws [FileKitDialogException] when neither works. | ||
| */ | ||
| internal class JvmDefaultApplicationOpener( | ||
| private val isDesktopOpenSupported: () -> Boolean, | ||
| private val desktopOpen: (File) -> Unit, | ||
| private val isLinux: Boolean, | ||
| private val startProcess: (List<String>) -> Process, | ||
| private val exitTimeoutMillis: Long = XDG_OPEN_EXIT_TIMEOUT_MILLIS, | ||
| ) { | ||
| fun open(file: File) { | ||
| when { | ||
| isDesktopOpenSupported() -> desktopOpen(file) | ||
|
|
||
| isLinux -> openWithXdgOpen(file) | ||
|
|
||
| else -> throw FileKitDialogException( | ||
| "Could not open the file with the default application: " + | ||
| "java.awt.Desktop cannot open files in this environment.", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun openWithXdgOpen(file: File) { | ||
| val process = try { | ||
| // An absolute path keeps xdg-open from reading the argument as an option or a URI scheme. | ||
| startProcess(listOf("xdg-open", file.absolutePath)) | ||
| } catch (e: IOException) { | ||
| throw FileKitDialogException( | ||
| "Could not open the file with the default application: " + | ||
| "java.awt.Desktop cannot open files and xdg-open could not be started.", | ||
| e, | ||
| ) | ||
| } | ||
|
|
||
| val exited = try { | ||
| process.waitFor(exitTimeoutMillis, TimeUnit.MILLISECONDS) | ||
| } catch (_: InterruptedException) { | ||
| Thread.currentThread().interrupt() | ||
| return | ||
| } | ||
|
|
||
| // Some xdg-open fallbacks keep running until the viewer closes: treat that as success. | ||
| if (exited && process.exitValue() != 0) { | ||
| throw FileKitDialogException( | ||
| "Could not open the file with the default application: " + | ||
| "xdg-open exited with code ${process.exitValue()}.", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| val Default: JvmDefaultApplicationOpener = JvmDefaultApplicationOpener( | ||
| isDesktopOpenSupported = { | ||
| Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN) | ||
| }, | ||
| desktopOpen = { Desktop.getDesktop().open(it) }, | ||
| isLinux = PlatformUtil.current == Platform.Linux, | ||
| startProcess = { command -> | ||
| ProcessBuilder(command) | ||
| .redirectInput(ProcessBuilder.Redirect.from(File("/dev/null"))) | ||
| .redirectOutput(ProcessBuilder.Redirect.DISCARD) | ||
| .redirectError(ProcessBuilder.Redirect.DISCARD) | ||
| .start() | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
148 changes: 148 additions & 0 deletions
148
.../src/jvmTest/kotlin/io/github/vinceglb/filekit/dialogs/JvmDefaultApplicationOpenerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| @file:Suppress("ktlint:standard:function-naming", "FunctionName") | ||
|
|
||
| package io.github.vinceglb.filekit.dialogs | ||
|
|
||
| import java.io.File | ||
| import java.io.IOException | ||
| import java.io.InputStream | ||
| import java.io.OutputStream | ||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFailsWith | ||
| import kotlin.test.assertIs | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class JvmDefaultApplicationOpenerTest { | ||
| private val file = File("report.pdf") | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_desktopSupported_usesDesktopOnly() { | ||
| val opened = mutableListOf<File>() | ||
| val commands = mutableListOf<List<String>>() | ||
| val opener = opener( | ||
| desktopSupported = true, | ||
| isLinux = true, | ||
| opened = opened, | ||
| commands = commands, | ||
| ) | ||
|
|
||
| opener.open(file) | ||
|
|
||
| assertEquals(listOf(file), opened) | ||
| assertTrue(commands.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_desktopError_isNotWrapped() { | ||
| val opener = JvmDefaultApplicationOpener( | ||
| isDesktopOpenSupported = { true }, | ||
| desktopOpen = { throw IOException("No application") }, | ||
| isLinux = true, | ||
| startProcess = { error("xdg-open must not run") }, | ||
| ) | ||
|
|
||
| assertFailsWith<IOException> { opener.open(file) } | ||
| } | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_desktopUnsupportedOnLinux_runsXdgOpenWithAbsolutePath() { | ||
| val commands = mutableListOf<List<String>>() | ||
| val opener = opener( | ||
| isLinux = true, | ||
| commands = commands, | ||
| process = FakeProcess(exitCode = 0), | ||
| ) | ||
|
|
||
| opener.open(file) | ||
|
|
||
| assertEquals(listOf(listOf("xdg-open", file.absolutePath)), commands) | ||
| } | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_xdgOpenStillRunning_returnsWithoutWaitingForever() { | ||
| val process = FakeProcess(exitCode = null) | ||
| val opener = opener(isLinux = true, process = process) | ||
|
|
||
| opener.open(file) | ||
|
|
||
| assertEquals(TIMEOUT_MILLIS, process.waitedMillis) | ||
| } | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_xdgOpenNonZeroExit_throwsDialogException() { | ||
| val opener = opener(isLinux = true, process = FakeProcess(exitCode = 3)) | ||
|
|
||
| val exception = assertFailsWith<FileKitDialogException> { opener.open(file) } | ||
|
|
||
| assertTrue(exception.message!!.contains("xdg-open exited with code 3")) | ||
| } | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_xdgOpenMissing_throwsDialogExceptionWithCause() { | ||
| val opener = JvmDefaultApplicationOpener( | ||
| isDesktopOpenSupported = { false }, | ||
| desktopOpen = { error("Desktop must not be used") }, | ||
| isLinux = true, | ||
| startProcess = { throw IOException("Cannot run program \"xdg-open\"") }, | ||
| ) | ||
|
|
||
| val exception = assertFailsWith<FileKitDialogException> { opener.open(file) } | ||
|
|
||
| assertIs<IOException>(exception.cause) | ||
| } | ||
|
|
||
| @Test | ||
| fun JvmDefaultApplicationOpener_desktopUnsupportedOutsideLinux_throwsDialogException() { | ||
| val commands = mutableListOf<List<String>>() | ||
| val opener = opener(isLinux = false, commands = commands) | ||
|
|
||
| assertFailsWith<FileKitDialogException> { opener.open(file) } | ||
| assertTrue(commands.isEmpty()) | ||
| } | ||
|
|
||
| private fun opener( | ||
| desktopSupported: Boolean = false, | ||
| isLinux: Boolean, | ||
| opened: MutableList<File> = mutableListOf(), | ||
| commands: MutableList<List<String>> = mutableListOf(), | ||
| process: Process = FakeProcess(exitCode = 0), | ||
| ) = JvmDefaultApplicationOpener( | ||
| isDesktopOpenSupported = { desktopSupported }, | ||
| desktopOpen = { opened += it }, | ||
| isLinux = isLinux, | ||
| startProcess = { command -> | ||
| commands += command | ||
| process | ||
| }, | ||
| exitTimeoutMillis = TIMEOUT_MILLIS, | ||
| ) | ||
|
|
||
| /** A process that has exited with [exitCode], or is still running when [exitCode] is null. */ | ||
| private class FakeProcess( | ||
| private val exitCode: Int?, | ||
| ) : Process() { | ||
| var waitedMillis: Long? = null | ||
|
|
||
| override fun waitFor(timeout: Long, unit: TimeUnit): Boolean { | ||
| waitedMillis = unit.toMillis(timeout) | ||
| return exitCode != null | ||
| } | ||
|
|
||
| override fun waitFor(): Int = exitCode ?: error("Unbounded wait") | ||
|
|
||
| override fun exitValue(): Int = exitCode ?: throw IllegalThreadStateException() | ||
|
|
||
| override fun getOutputStream(): OutputStream = OutputStream.nullOutputStream() | ||
|
|
||
| override fun getInputStream(): InputStream = InputStream.nullInputStream() | ||
|
|
||
| override fun getErrorStream(): InputStream = InputStream.nullInputStream() | ||
|
|
||
| override fun destroy() = Unit | ||
| } | ||
|
|
||
| private companion object { | ||
| const val TIMEOUT_MILLIS = 50L | ||
| } | ||
| } |
7 changes: 0 additions & 7 deletions
7
...ebMain/kotlin/io/github/vinceglb/filekit/dialogs/FileKitFileSaverWithoutBytesException.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
Desktopis unsupported on Linux andxdg-openstays alive while the viewer is open, this blocks the caller for the full three-second timeout even when opening succeeds. Because this API is synchronous and documented for direct click-handler use, that can freeze the UI; avoid waiting on the caller thread for this fallback.Useful? React with 👍 / 👎.