From bfd10f690324af0a30b7767817e04da8c4db3b47 Mon Sep 17 00:00:00 2001 From: dcog989 Date: Mon, 22 Sep 2025 13:37:22 +0100 Subject: [PATCH 1/3] Crash when opening non-existent file --- Flow.Launcher/PublicAPIInstance.cs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 6a8ee40f98c..731c329e286 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -412,6 +412,14 @@ public void OpenDirectory(string directoryPath, string fileNameOrFilePath = null private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false) { + if (uri.IsFile) + { + if (!File.Exists(uri.LocalPath) && !Directory.Exists(uri.LocalPath)) + { + ShowMsgError(GetTranslation("errorTitle"), $"File or directory not found: {uri.LocalPath}"); + return; + } + } if (forceBrowser || uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) { var browserInfo = _settings.CustomBrowser; @@ -441,13 +449,19 @@ private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false) } else { - Process.Start(new ProcessStartInfo() + try { - FileName = uri.AbsoluteUri, - UseShellExecute = true - })?.Dispose(); - - return; + Process.Start(new ProcessStartInfo() + { + FileName = uri.AbsoluteUri, + UseShellExecute = true + })?.Dispose(); + } + catch (Exception e) + { + LogException(ClassName, $"Failed to open: {uri.AbsoluteUri}", e); + ShowMsgError(GetTranslation("errorTitle"), e.Message); + } } } From 71b8144c3c428f4ffe2ebbbfc3544a0bf5afbc69 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 22 Sep 2025 20:55:36 +0800 Subject: [PATCH 2/3] Add translations & Improve code quality --- Flow.Launcher/Languages/en.xaml | 1 + Flow.Launcher/PublicAPIInstance.cs | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 626fe1385a4..561bb277eff 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -590,6 +590,7 @@ Error An error occurred while opening the folder. {0} An error occurred while opening the URL in the browser. Please check your Default Web Browser configuration in the General section of the settings window + File or directory not found: {0} Please wait... diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 731c329e286..cbd793dc811 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Specialized; @@ -412,14 +412,12 @@ public void OpenDirectory(string directoryPath, string fileNameOrFilePath = null private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false) { - if (uri.IsFile) + if (uri.IsFile && !File.Exists(uri.LocalPath) && !Directory.Exists(uri.LocalPath)) { - if (!File.Exists(uri.LocalPath) && !Directory.Exists(uri.LocalPath)) - { - ShowMsgError(GetTranslation("errorTitle"), $"File or directory not found: {uri.LocalPath}"); - return; - } + ShowMsgError(GetTranslation("errorTitle"), string.Format(GetTranslation("fileNotFoundError"), uri.LocalPath)); + return; } + if (forceBrowser || uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) { var browserInfo = _settings.CustomBrowser; From 763fee0c1400ef3cf3b1cfb49cd0e54ec5c0ce7b Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 22 Sep 2025 20:58:22 +0800 Subject: [PATCH 3/3] Add new helper method & Improve code quality --- Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs | 10 ++++++++++ Flow.Launcher/PublicAPIInstance.cs | 11 +++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index 6c506cfc06c..3af57f00d53 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -150,6 +150,16 @@ public static bool FileExists(this string filePath) return File.Exists(filePath); } + /// + /// Checks if a file or directory exists + /// + /// + /// + public static bool FileOrLocationExists(this string path) + { + return LocationExists(path) || FileExists(path); + } + /// /// Open a directory window (using the OS's default handler, usually explorer) /// diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index cbd793dc811..b4c3aa92b82 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -74,7 +74,6 @@ public void ChangeQuery(string query, bool requery = false) _mainVM.ChangeQueryText(query, requery); } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "")] public void RestartApp() { _mainVM.Hide(); @@ -179,7 +178,7 @@ public async void CopyToClipboard(string stringToCopy, bool directCopy = false, Clipboard.SetFileDropList(paths); }); - + if (exception == null) { if (showDefaultNotification) @@ -218,7 +217,7 @@ public async void CopyToClipboard(string stringToCopy, bool directCopy = false, { LogException(nameof(PublicAPIInstance), "Failed to copy text to clipboard", exception); ShowMsgError(GetTranslation("failedToCopy")); - } + } } } @@ -327,7 +326,7 @@ public void SavePluginSettings() ((PluginJsonStorage)_pluginJsonStorages[type]).Save(); } - + public void OpenDirectory(string directoryPath, string fileNameOrFilePath = null) { try @@ -412,7 +411,7 @@ public void OpenDirectory(string directoryPath, string fileNameOrFilePath = null private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false) { - if (uri.IsFile && !File.Exists(uri.LocalPath) && !Directory.Exists(uri.LocalPath)) + if (uri.IsFile && !FilesFolders.FileOrLocationExists(uri.LocalPath)) { ShowMsgError(GetTranslation("errorTitle"), string.Format(GetTranslation("fileNotFoundError"), uri.LocalPath)); return; @@ -493,7 +492,7 @@ public void OpenAppUri(Uri appUri) OpenUri(appUri); } - public void ToggleGameMode() + public void ToggleGameMode() { _mainVM.ToggleGameMode(); }