Skip to content

Commit

Permalink
Fix folder verification so empty folders can't be selected
Browse files Browse the repository at this point in the history
- Also made error messages slightly more descriptive
  • Loading branch information
Iridium-IO committed Feb 5, 2024
1 parent 868320a commit 14de2d5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CompactGUI.Core/Compactor.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions CompactGUI.Core/SharedMethods.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions CompactGUI/ViewModels/MainViewModel.vb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 14de2d5

Please sign in to comment.