From 14de2d59fe54abfb74c7421da3a5c9bbf5761f97 Mon Sep 17 00:00:00 2001 From: IridiumIO Date: Mon, 5 Feb 2024 17:53:50 +1000 Subject: [PATCH] Fix folder verification so empty folders can't be selected - Also made error messages slightly more descriptive --- CompactGUI.Core/Compactor.vb | 2 +- CompactGUI.Core/SharedMethods.vb | 13 +++++++------ CompactGUI/ViewModels/MainViewModel.vb | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/CompactGUI.Core/Compactor.vb b/CompactGUI.Core/Compactor.vb index cc0ddbc..bcb41a7 100644 --- a/CompactGUI.Core/Compactor.vb +++ b/CompactGUI.Core/Compactor.vb @@ -6,7 +6,7 @@ Public Class Compactor Public Sub New(folder As String, cLevel As CompressionAlgorithm, excludedFilesTypes As String()) - If Not verifyFolder(folder) Then Return + If Not verifyFolder(folder).isValid Then Return _workingDir = folder _excludedFileTypes = excludedFilesTypes diff --git a/CompactGUI.Core/SharedMethods.vb b/CompactGUI.Core/SharedMethods.vb index 2959927..e308210 100644 --- a/CompactGUI.Core/SharedMethods.vb +++ b/CompactGUI.Core/SharedMethods.vb @@ -5,15 +5,16 @@ Imports System.Text Public Module SharedMethods - Function verifyFolder(folder As String) As Boolean + Function verifyFolder(folder As String) As (isValid As Boolean, msg As String) - If Not IO.Directory.Exists(folder) Then : Return False - ElseIf folder.Contains((Environment.GetFolderPath(Environment.SpecialFolder.Windows))) Then : Return False - ElseIf folder.EndsWith(":\") Then : Return False - ElseIf DriveInfo.GetDrives().First(Function(f) folder.startswith(f.Name)).DriveFormat <> "NTFS" Then : Return False + If Not IO.Directory.Exists(folder) Then : Return (False, "Directory does not exist") + ElseIf folder.Contains((Environment.GetFolderPath(Environment.SpecialFolder.Windows))) Then : Return (False, "Cannot compress system directory") + ElseIf folder.EndsWith(":\") Then : Return (False, "Cannot compress root directory") + ElseIf Not IO.Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories).Any() Then : Return (False, "Directory is empty") + ElseIf DriveInfo.GetDrives().First(Function(f) folder.StartsWith(f.Name)).DriveFormat <> "NTFS" Then : Return (False, "Cannot compress a directory on a non-NTFS drive") End If - Return True + Return (True, "") End Function diff --git a/CompactGUI/ViewModels/MainViewModel.vb b/CompactGUI/ViewModels/MainViewModel.vb index 905db31..7449d00 100644 --- a/CompactGUI/ViewModels/MainViewModel.vb +++ b/CompactGUI/ViewModels/MainViewModel.vb @@ -33,8 +33,8 @@ Public Class MainViewModel : Inherits ObservableObject path = folderSelector.SelectedPath End If Dim validFolder = Core.verifyFolder(path) - If Not validFolder Then - Dim msgError As New ContentDialog With {.Title = "Invalid Folder", .Content = "This is either a system folder, root directory or a non-NTFS drive and cannot be selected.", .CloseButtonText = "OK"} + If Not validFolder.isValid Then + Dim msgError As New ContentDialog With {.Title = "Invalid Folder", .Content = $"{validFolder.msg}", .CloseButtonText = "OK"} msgError.ShowAsync() Return End If