From 0c88d91278562eb4e99fb9883714cdc5a8731cef Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Fri, 18 Jun 2021 09:49:30 -0500 Subject: [PATCH] Ignore .DS_Store and @eaDir folders when scanning directories. (#314) --- API/Services/DirectoryService.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs index ac5f5ec5e..f823d2066 100644 --- a/API/Services/DirectoryService.cs +++ b/API/Services/DirectoryService.cs @@ -13,6 +13,9 @@ namespace API.Services public class DirectoryService : IDirectoryService { private readonly ILogger _logger; + private static readonly Regex ExcludeDirectories = new Regex( + @"@eaDir|\.DS_Store", + RegexOptions.Compiled | RegexOptions.IgnoreCase); public DirectoryService(ILogger logger) { @@ -222,6 +225,7 @@ namespace API.Services /// Directory to scan /// Action to apply on file path /// Regex pattern to search against + /// /// public static int TraverseTreeParallelForEach(string root, Action action, string searchPattern, ILogger logger) { @@ -241,11 +245,11 @@ namespace API.Services while (dirs.Count > 0) { var currentDir = dirs.Pop(); - string[] subDirs; + IEnumerable subDirs; string[] files; try { - subDirs = Directory.GetDirectories(currentDir); + subDirs = Directory.GetDirectories(currentDir).Where(path => ExcludeDirectories.Matches(path).Count == 0); } // Thrown if we do not have discovery permission on the directory. catch (UnauthorizedAccessException e) { @@ -316,7 +320,7 @@ namespace API.Services // Push the subdirectories onto the stack for traversal. // This could also be done before handing the files. - foreach (string str in subDirs) + foreach (var str in subDirs) dirs.Push(str); }