diff --git a/Plugin.cs b/Plugin.cs index 0d842cf..0392396 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -16,7 +16,9 @@ // along with this program. If not, see . using System; +using System.Collections.Generic; using System.IO; +using System.Linq; using System.Windows; using QuickLook.Common.Plugin; @@ -28,14 +30,59 @@ public class Plugin : IViewer private bool _isDirectory; public int Priority => -5; - + private static readonly int FILES_MAX = 300; public void Init() { } + /// + /// A safe way to get all the files in a directory and sub directory without crashing on UnauthorizedException or PathTooLongException + /// + /// Starting directory + /// Filename pattern match + /// Search subdirectories or only top level directory for files + /// List of files + public static IEnumerable GetDirectoryFiles(string rootPath, string patternMatch, SearchOption searchOption) + { + var foundFiles = Enumerable.Empty(); + + if (searchOption == SearchOption.AllDirectories) + { + try + { + IEnumerable subDirs = Directory.EnumerateDirectories(rootPath); + foreach (string dir in subDirs) + { + if (foundFiles.Count() < FILES_MAX) + foundFiles = foundFiles.Concat(GetDirectoryFiles(dir, patternMatch, searchOption)); // Add files in subdirectories recursively to the list + } + } + catch (UnauthorizedAccessException) { } + catch (PathTooLongException) { } + } + + try + { + if (foundFiles.Count() < FILES_MAX) + foundFiles = foundFiles.Concat(Directory.EnumerateFiles(rootPath, patternMatch)); // Add files from the current directory + } + catch (UnauthorizedAccessException) { } + + return foundFiles; + } + + private int FileCount(string path) + { + /*DirectoryInfo dirInfo = new DirectoryInfo(path); + return dirInfo.EnumerateDirectories() + .AsParallel() + .SelectMany(di => di.EnumerateFiles("*.*", SearchOption.AllDirectories)) + .Count();*/ + return GetDirectoryFiles(path, "*", SearchOption.AllDirectories).Count(); + } public bool CanHandle(string path) { - if (Path.GetPathRoot(path) == path) + if (Path.GetPathRoot(path) == path || FileCount(path) > FILES_MAX - 1) return false; _isDirectory = Directory.Exists(path); diff --git a/QuickLook.Plugin.FolderViewer.csproj b/QuickLook.Plugin.FolderViewer.csproj index 2e26277..9605545 100644 --- a/QuickLook.Plugin.FolderViewer.csproj +++ b/QuickLook.Plugin.FolderViewer.csproj @@ -103,4 +103,8 @@ + + cd "$(SolutionDir)Scripts" +powershell -file "$(SolutionDir)Scripts\pack-zip.ps1" + \ No newline at end of file