-
Notifications
You must be signed in to change notification settings - Fork 746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StorageFileHelper - Add GetFilesInDirectoryAsync #16341
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||
using System.Reflection; | ||||
using System.Collections.Generic; | ||||
using System.IO; | ||||
using System.Linq; | ||||
|
||||
namespace Uno.UI.Toolkit; | ||||
|
||||
|
@@ -18,9 +19,20 @@ public partial class StorageFileHelper | |||
/// <returns>A task that will complete with a result of true if file exists, otherwise with a result of false.</returns> | ||||
public static async Task<bool> ExistsInPackage(string fileName) => await FileExistsInPackage(fileName); | ||||
|
||||
/// <summary> | ||||
/// This asynchronous method retrieves the paths of files within a directory based on a specified filter for file extensions. | ||||
/// </summary> | ||||
/// <param name="extensionsFilter">An array of strings representing the file extensions to filter the files.If null, all files in the directory are considered.</param> | ||||
/// <returns>An array of strings containing the paths of the filtered files within the directory.</returns> | ||||
public static async Task<string[]> GetFilesInDirectoryAsync(string[] extensionsFilter) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rafael-rosa-knowcode By the look of the implementation, it means that you want all the files that |
||||
=> await GetFilesInDirectory(e => extensionsFilter == null || extensionsFilter.Any(filter => e.EndsWith(filter, StringComparison.OrdinalIgnoreCase))); | ||||
|
||||
#if IS_UNIT_TESTS || __NETSTD_REFERENCE__ | ||||
private static Task<bool> FileExistsInPackage(string fileName) | ||||
=> throw new NotImplementedException(); | ||||
|
||||
private static Task<string[]> GetFilesInDirectory(Func<string, bool> predicate) | ||||
=> throw new NotImplementedException(); | ||||
#endif | ||||
|
||||
#if __SKIA__ || WINDOWS || WINAPPSDK || WINDOWS_UWP || WINUI | ||||
|
@@ -39,5 +51,29 @@ private static Task<bool> FileExistsInPackage(string fileName) | |||
|
||||
return Task.FromResult(false); | ||||
} | ||||
|
||||
/// <summary> | ||||
/// Retrieves the paths of assets within the current application based on the specified filter predicate. | ||||
/// </summary> | ||||
/// <param name="predicate">A predicate function determining whether a file should be included in the result.</param> | ||||
/// <returns>Returns an array of strings containing the paths of the filtered assets.</returns> | ||||
private static Task<string[]> GetFilesInDirectory(Func<string, bool> predicate) | ||||
{ | ||||
List<string> assetsFiles = new(); | ||||
string path = string.Empty; | ||||
var executingPath = Assembly.GetExecutingAssembly().Location; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is likely going to cause trouble when we'll run as a single file. Let's use the same APIs that: uno/src/Uno.UWP/Storage/StorageFile.cs Line 28 in 23feb21
is using. |
||||
if (!string.IsNullOrEmpty(executingPath)) | ||||
{ | ||||
path = Path.GetDirectoryName(executingPath) + string.Empty; | ||||
if (!string.IsNullOrEmpty(path)) | ||||
{ | ||||
assetsFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories) | ||||
.Where(e => predicate(e)) | ||||
.Select(e => e.Replace(path, string.Empty).Replace('\\', '/').TrimStart('/')) | ||||
.ToList(); | ||||
} | ||||
} | ||||
return Task.FromResult(assetsFiles.ToArray()); | ||||
} | ||||
#endif | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,21 @@ private static Task<bool> FileExistsInPackage(string fileName) | |
|
||
return Task.FromResult(resourcePathname != null); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the paths of assets within the current application based on the specified filter predicate. | ||
/// </summary> | ||
/// <param name="predicate">A predicate function determining whether a file should be included in the result.</param> | ||
/// <returns>Returns an array of strings containing the paths of the filtered assets.</returns> | ||
private static Task<string[]> GetFilesInDirectory(Func<string, bool> predicate) | ||
{ | ||
string rootPath = AppDomain.CurrentDomain.BaseDirectory; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, let's use the same APIs that |
||
string[] files = Directory.GetFiles(rootPath, "*", SearchOption.AllDirectories); | ||
|
||
var results = files?.Where(e => predicate(e)) | ||
.Select(e => e.Replace(rootPath, string.Empty).Replace('\\', '/')) | ||
.ToArray() ?? Array.Empty<string>(); | ||
|
||
return Task.FromResult(results); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
Folder
instead ofDirectory
to preserve WinUI convention