Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using QuickLook.Common.Plugin;

Expand All @@ -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()
{
}

/// <summary>
/// A safe way to get all the files in a directory and sub directory without crashing on UnauthorizedException or PathTooLongException
/// </summary>
/// <param name="rootPath">Starting directory</param>
/// <param name="patternMatch">Filename pattern match</param>
/// <param name="searchOption">Search subdirectories or only top level directory for files</param>
/// <returns>List of files</returns>
public static IEnumerable<string> GetDirectoryFiles(string rootPath, string patternMatch, SearchOption searchOption)
{
var foundFiles = Enumerable.Empty<string>();

if (searchOption == SearchOption.AllDirectories)
{
try
{
IEnumerable<string> subDirs = Directory.EnumerateDirectories(rootPath);
foreach (string dir in subDirs)
{
if (foundFiles.Count() < FILES_MAX)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you break (or even return) if the condition fails, so that each recursive call can travel up as soon as it exceeds FILE_MAX?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less than FILES_MAX, just return the file count of folders. Use try, catch to catch UnauthorizedException and so on.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I did not get it.

What I am trying to say is, if the condition fails, and the count exceeds, add an else block to either break or return so that we stop additional looping.

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);
Expand Down
4 changes: 4 additions & 0 deletions QuickLook.Plugin.FolderViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>cd "$(SolutionDir)Scripts"
powershell -file "$(SolutionDir)Scripts\pack-zip.ps1"</PostBuildEvent>
</PropertyGroup>
</Project>