update path checks to allow denial or warning

This commit is contained in:
IsWaffle 2024-04-05 12:29:47 -04:00
parent 68b895786b
commit 8cceee6668
3 changed files with 82 additions and 38 deletions

View File

@ -162,54 +162,47 @@ public static class FileHelper
}
}
private enum PathCheckType
public static bool CheckPathForProblemLocations(string path, out PathCheck failedCheck)
{
EndsWith = 0,
Contains = 1,
DriveRoot = 2
}
failedCheck = new();
public static bool CheckPathForProblemLocations(string path, out string detectedName)
{
detectedName = "";
var problemNames = new Dictionary<string, PathCheckType>()
var problemPaths = new List<PathCheck>()
{
{ "Documents", PathCheckType.EndsWith },
{ "Desktop", PathCheckType.Contains },
{ "scoped_dir", PathCheckType.Contains },
{ "Downloads", PathCheckType.Contains },
{ "OneDrive", PathCheckType.Contains },
{ "NextCloud", PathCheckType.Contains },
{ "DropBox", PathCheckType.Contains },
{ "Google", PathCheckType.Contains },
{ "Program Files", PathCheckType.Contains },
{ "Program Files (x86)", PathCheckType.Contains },
{ "Drive Root", PathCheckType.DriveRoot }
new("Documents", PathCheckType.EndsWith, PathCheckAction.Warn),
new("Desktop", PathCheckType.Contains, PathCheckAction.Warn),
new("scoped_dir", PathCheckType.Contains, PathCheckAction.Deny),
new("Downloads", PathCheckType.Contains, PathCheckAction.Deny),
new("OneDrive", PathCheckType.Contains, PathCheckAction.Deny),
new("NextCloud", PathCheckType.Contains, PathCheckAction.Warn),
new("DropBox", PathCheckType.Contains, PathCheckAction.Warn),
new("Google", PathCheckType.Contains, PathCheckAction.Warn),
new("Program Files", PathCheckType.Contains, PathCheckAction.Deny),
new("Program Files (x86", PathCheckType.Contains, PathCheckAction.Deny),
new("Drive Root", PathCheckType.DriveRoot, PathCheckAction.Deny)
};
foreach (var name in problemNames)
foreach (var check in problemPaths)
{
switch (name.Value)
switch (check.CheckType)
{
case PathCheckType.EndsWith:
if (path.ToLower().EndsWith(name.Key.ToLower()))
if (path.ToLower().EndsWith(check.Target.ToLower()))
{
detectedName = name.Key;
failedCheck = check;
return true;
}
break;
case PathCheckType.Contains:
if (path.ToLower().Contains(name.Key.ToLower()))
if (path.ToLower().Contains(check.Target.ToLower()))
{
detectedName = name.Key;
failedCheck = check;
return true;
}
break;
case PathCheckType.DriveRoot:
if (Regex.Match(path.ToLower(), @"^\w:(\\|\/)$").Success)
{
detectedName = name.Key;
failedCheck = check;
return true;
}
break;

View File

@ -0,0 +1,32 @@
namespace SPTInstaller.Models;
public enum PathCheckType
{
EndsWith = 0,
Contains = 1,
DriveRoot = 2
}
public enum PathCheckAction
{
Warn = 0,
Deny = 1,
}
public class PathCheck
{
public string Target { get; private set; }
public PathCheckType CheckType { get; private set; }
public PathCheckAction CheckAction { get; private set; }
public PathCheck()
{
}
public PathCheck(string target, PathCheckType checkType, PathCheckAction checkAction)
{
Target = target;
CheckType = checkType;
CheckAction = checkAction;
}
}

View File

@ -164,19 +164,38 @@ public class PreChecksViewModel : ViewModelBase
Task.Run(async () =>
{
if (FileHelper.CheckPathForProblemLocations(InstallPath, out var detectedName))
if (FileHelper.CheckPathForProblemLocations(InstallPath, out var failedCheck ))
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
Log.Warning("Problem folder detected, confirming install path ...");
var confirmation = await DialogHost.Show(new ConfirmationDialog($"We suspect you may be installing into a problematic folder: {detectedName}.\nYou might want to consider installing somewhere else to avoid issues.\n\nAre you sure you want to install to this path?\n{InstallPath}"));
if (confirmation == null || !bool.TryParse(confirmation.ToString(), out var confirm) || !confirm)
switch (failedCheck.CheckAction)
{
Log.Information("User declined install path, exiting");
Environment.Exit(0);
case PathCheckAction.Warn:
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
Log.Warning("Problem path detected, confirming install path ...");
var confirmation = await DialogHost.Show(new ConfirmationDialog(
$"We suspect you may be installing into a problematic folder: {failedCheck.Target}.\nYou might want to consider installing somewhere else to avoid issues.\n\nAre you sure you want to install to this path?\n{InstallPath}"));
if (confirmation == null || !bool.TryParse(confirmation.ToString(), out var confirm) ||
!confirm)
{
Log.Information("User declined install path, exiting");
Environment.Exit(0);
}
});
break;
}
case PathCheckAction.Deny:
{
Log.Error("Problem path detected, install denied");
NavigateTo(new MessageViewModel(HostScreen, Result.FromError($"We suspect you may be installing into a problematic folder: {failedCheck.Target}.\nWe won't be letting you install here. Please move the installer to another folder.\nSuggestion: a folder under your drive root, such as 'C:\\spt\\'\nDenied Path: {InstallPath}")));
break;
}
default:
throw new ArgumentOutOfRangeException();
}
});
Log.Information("User accepted install path");
}