From bf1a9b9effecf8fc18528da672990bc1ed85d6eb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 21 Jun 2023 10:57:02 -0500 Subject: return files + dirs together --- src/slskd/Files/FileService.cs | 22 +++++++++++++++++++--- src/slskd/Files/IFileService.cs | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c7c0105c..dfb5ad7b 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -42,7 +42,7 @@ namespace slskd.Files private IOptionsSnapshot OptionsSnapshot { get; } /// - /// Lists all of the contents of the specified , optionally applying the + /// Lists the contents in the specified , optionally applying the /// specified . /// /// The directory from which to start the listing. @@ -52,7 +52,7 @@ namespace slskd.Files /// Thrown if the specified directory is not rooted in an allowed directory. /// /// Thrown if the specified directory does not exist. - public async Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) + public async Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) { if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) { @@ -64,7 +64,23 @@ namespace slskd.Files throw new NotFoundException($"The directory '{parentDirectory}' does not exist"); } - return await Task.Run(() => new DirectoryInfo(parentDirectory).GetFileSystemInfos("*", enumerationOptions)); + return await Task.Run(() => + { + var dir = new DirectoryInfo(parentDirectory); + + var contents = dir.GetFileSystemInfos("*", enumerationOptions); + + var files = contents + .OfType() + .Select(f => FilesystemFile.FromFileInfo(f)); + + var dirs = contents + .OfType() + .Prepend(dir) + .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with { Files = files.Where(f => f.Parent == d.FullName) }); + + return dirs; + }); } private bool IsPermissibleDirectory(string dir, Options.DirectoriesOptions options) diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index 0b393e64..0a750b59 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -27,7 +27,7 @@ namespace slskd.Files public interface IFileService { /// - /// Lists all of the contents of the specified , optionally applying the + /// Lists the contents in the specified , optionally applying the /// specified . /// /// The directory from which to start the listing. @@ -37,6 +37,6 @@ namespace slskd.Files /// Thrown if the specified directory is not rooted in an allowed directory. /// /// Thrown if the specified directory does not exist. - Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); + Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file -- cgit v1.2.3