Skip to content

Commit

Permalink
Fix crash when reading files from unautorized folder
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Feb 1, 2025
1 parent 6bc80b9 commit d98a667
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
22 changes: 22 additions & 0 deletions src/Services/IOService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,5 +448,27 @@ public static (string, string) GetDifferingRoot(in string path1, in string path2

return (remainingPath1, remainingPath2);
}

/// <summary>
/// Safe alternative of <see cref="Directory.GetFiles(string)"/> that catches all exceptions
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static IEnumerable<string> GetFilesInFolder(string path)
{
IEnumerable<string> files = Enumerable.Empty<string>();
if (Directory.Exists(path))
{
try
{
files = Directory.GetFiles(path);
}
catch (Exception ex)
{
Logger.Error($"Failed to get files of folder {path}", ex);
}
}
return files;
}
}
}
17 changes: 2 additions & 15 deletions src/ViewModels/Import/ImportFolderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,7 @@ private List<string> GetPathsToImport()
List<string> toImport = new(Files);
foreach (var folder in NonRecursiveDirectories)
{
if (Directory.Exists(folder))
{
IEnumerable<string> files;
try
{
files = Directory.GetFiles(folder);
}
catch (Exception ex)
{
Logger.Error($"Failed to get files of folder {folder}", ex);
files = Enumerable.Empty<string>();
}
toImport.AddRange(files);
}
toImport.AddRange(IOService.TryGetFilesInFolder(folder));

Check failure on line 119 in src/ViewModels/Import/ImportFolderViewModel.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'IOService' does not contain a definition for 'TryGetFilesInFolder'

Check failure on line 119 in src/ViewModels/Import/ImportFolderViewModel.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'IOService' does not contain a definition for 'TryGetFilesInFolder'
}

//3. Filter out doubles and banished paths
Expand Down Expand Up @@ -205,7 +192,7 @@ private List<string> LetUserFilterToImport(IList<string> toImport)
var checkedFolders = CheckableTreeNode<Folder>.GetCheckedItems(CheckableFolders).Flatten();
foreach (var folder in checkedFolders)
{
toImportBySubFolders.AddRange(Directory.GetFiles(folder.FullPath));
toImportBySubFolders.AddRange(IOService.TryGetFilesInFolder(folder.FullPath));

Check failure on line 195 in src/ViewModels/Import/ImportFolderViewModel.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'IOService' does not contain a definition for 'TryGetFilesInFolder'

Check failure on line 195 in src/ViewModels/Import/ImportFolderViewModel.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'IOService' does not contain a definition for 'TryGetFilesInFolder'
}
toImport = toImport.Intersect(toImportBySubFolders).ToList();
}
Expand Down

0 comments on commit d98a667

Please sign in to comment.