Merge pull request #4004 from dcog989/404-local-file-crash

Crash when opening non-existent local file
This commit is contained in:
Jack Ye 2025-09-22 21:09:29 +08:00 committed by GitHub
commit 1e8819af0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 12 deletions

View file

@ -150,6 +150,16 @@ namespace Flow.Launcher.Plugin.SharedCommands
return File.Exists(filePath);
}
/// <summary>
/// Checks if a file or directory exists
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool FileOrLocationExists(this string path)
{
return LocationExists(path) || FileExists(path);
}
/// <summary>
/// Open a directory window (using the OS's default handler, usually explorer)
/// </summary>

View file

@ -590,6 +590,7 @@
<system:String x:Key="errorTitle">Error</system:String>
<system:String x:Key="folderOpenError">An error occurred while opening the folder. {0}</system:String>
<system:String x:Key="browserOpenError">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</system:String>
<system:String x:Key="fileNotFoundError">File or directory not found: {0}</system:String>
<!-- General Notice -->
<system:String x:Key="pleaseWait">Please wait...</system:String>

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
@ -74,7 +74,6 @@ namespace Flow.Launcher
_mainVM.ChangeQueryText(query, requery);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
public void RestartApp()
{
_mainVM.Hide();
@ -179,7 +178,7 @@ namespace Flow.Launcher
Clipboard.SetFileDropList(paths);
});
if (exception == null)
{
if (showDefaultNotification)
@ -218,7 +217,7 @@ namespace Flow.Launcher
{
LogException(nameof(PublicAPIInstance), "Failed to copy text to clipboard", exception);
ShowMsgError(GetTranslation("failedToCopy"));
}
}
}
}
@ -327,7 +326,7 @@ namespace Flow.Launcher
((PluginJsonStorage<T>)_pluginJsonStorages[type]).Save();
}
public void OpenDirectory(string directoryPath, string fileNameOrFilePath = null)
{
try
@ -412,6 +411,12 @@ namespace Flow.Launcher
private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false)
{
if (uri.IsFile && !FilesFolders.FileOrLocationExists(uri.LocalPath))
{
ShowMsgError(GetTranslation("errorTitle"), string.Format(GetTranslation("fileNotFoundError"), uri.LocalPath));
return;
}
if (forceBrowser || uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
{
var browserInfo = _settings.CustomBrowser;
@ -441,13 +446,19 @@ namespace Flow.Launcher
}
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);
}
}
}
@ -481,7 +492,7 @@ namespace Flow.Launcher
OpenUri(appUri);
}
public void ToggleGameMode()
public void ToggleGameMode()
{
_mainVM.ToggleGameMode();
}