Skip to content

Commit

Permalink
allExceptExtensions is added on get files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadyNagy committed Mar 5, 2024
1 parent 3952296 commit 6848687
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ActiveDirectoryFileManagement.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>ActiveDirectoryFileManagement</id>
<title>Active Directory File Management</title>
<version>1.0.2</version>
<version>1.0.3</version>
<authors>ShadyNagy</authors>
<description>
Active Directory File Management Package
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ public interface IDirectoryService
/// </summary>
/// <param name="path">The directory path to search.</param>
/// <param name="extensions">An array of file extensions to filter the files. If null or empty, all files are returned.</param>
/// <param name="allExceptExtensions">If true then get all files but not these extensions and if false then get only files with these extensions.</param>
/// <returns>An enumerable collection of file paths.</returns>
IEnumerable<string> GetFilesUnderUser(string path, string[] extensions);
IEnumerable<string> GetFilesUnderUser(string path, string[] extensions, bool allExceptExtensions = false);

/// <summary>
/// Retrieves the files from the specified path.
/// </summary>
/// <param name="path">The directory path to search.</param>
/// <param name="extensions">An array of file extensions to filter the files. If null or empty, all files are returned.</param>
/// <param name="allExceptExtensions">If true then get all files but not these extensions and if false then get only files with these extensions.</param>
/// <returns>An enumerable collection of file paths.</returns>
IEnumerable<string> GetFiles(string path, string[] extensions);
IEnumerable<string> GetFiles(string path, string[] extensions, bool allExceptExtensions = false);

/// <summary>
/// Retrieves the directories from the specified path under the context of an Active Directory user.
Expand Down
17 changes: 13 additions & 4 deletions src/ActiveDirectoryFileManagement/Services/DirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,23 @@ public IEnumerable<string> GetFiles(string path)
/// </summary>
/// <param name="path">The directory path to search.</param>
/// <param name="extensions">An array of file extensions to filter the files. If null or empty, all files are returned.</param>
/// <param name="allExceptExtensions">If true then get all files but not these extensions and if false then get only files with these extensions.</param>
/// <returns>An enumerable collection of file paths.</returns>
public IEnumerable<string> GetFilesUnderUser(string path, string[] extensions)
public IEnumerable<string> GetFilesUnderUser(string path, string[] extensions, bool allExceptExtensions = false)
{
return _activeDirectoryService.ImpersonateUserAndRunAction(() => GetFilesByExtensions(path, extensions));
return _activeDirectoryService.ImpersonateUserAndRunAction(() => allExceptExtensions? GetFilesByNotExtensions(path, extensions) : GetFilesByExtensions(path, extensions));
}

/// <summary>
/// Retrieves the files from the specified path.
/// </summary>
/// <param name="path">The directory path to search.</param>
/// <param name="extensions">An array of file extensions to filter the files. If null or empty, all files are returned.</param>
/// <param name="allExceptExtensions">If true then get all files but not these extensions and if false then get only files with these extensions.</param>
/// <returns>An enumerable collection of file paths.</returns>
public IEnumerable<string> GetFiles(string path, string[] extensions)
public IEnumerable<string> GetFiles(string path, string[] extensions, bool allExceptExtensions = false)
{
return GetFilesByExtensions(path, extensions);
return allExceptExtensions? GetFilesByNotExtensions(path, extensions) : GetFilesByExtensions(path, extensions);
}

/// <summary>
Expand Down Expand Up @@ -150,4 +152,11 @@ private IEnumerable<string> GetFilesByExtensions(string path, string[] extension
return Directory.EnumerateFiles(path)
.Where(file => extensionSet.Contains(Path.GetExtension(file)));
}

private IEnumerable<string> GetFilesByNotExtensions(string path, string[] extensions)
{
var extensionSet = new HashSet<string>(extensions.Select(ext => "." + ext), StringComparer.OrdinalIgnoreCase);
return Directory.EnumerateFiles(path)
.Where(file => !extensionSet.Contains(Path.GetExtension(file)));
}
}

0 comments on commit 6848687

Please sign in to comment.