From 37032356080e05b23e54db48d58c62fc0159754a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 19 Jun 2023 22:27:06 -0500 Subject: stub IFileService --- src/slskd/Files/FileService.cs | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/slskd/Files/FileService.cs (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs new file mode 100644 index 00000000..c38e6ff4 --- /dev/null +++ b/src/slskd/Files/FileService.cs @@ -0,0 +1,68 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Files +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Linq.Expressions; + using System.Threading.Tasks; + + /// + /// Manages files on disk. + /// + public class FileService : IFileService + { + /// + /// Initializes a new instance of the class. + /// + /// + public FileService( + IOptionsSnapshot optionsSnapshot) + { + OptionsSnapshot = optionsSnapshot; + } + + private IOptionsSnapshot OptionsSnapshot { get; } + + public async Task> ListDownloadedFiles() + { + var dir = new DirectoryInfo(OptionsSnapshot.Value.Directories.Downloads); + + var files = await Task.Run(() => + { + return dir.GetFiles("*", new EnumerationOptions + { + AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, + IgnoreInaccessible = false, + RecurseSubdirectories = true, + }); + }); + + return files.AsEnumerable(); + } + + public Task> ListIncompleteFiles(Expression> expression = null) + { + throw new NotImplementedException(); + } + } +} -- cgit v1.2.3 From e2451a8c1207566851c0baad99a78093441445ae Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 19 Jun 2023 22:32:52 -0500 Subject: rough in download listing --- src/slskd/Files/FileService.cs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c38e6ff4..c03183d5 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -43,18 +43,24 @@ namespace slskd.Files private IOptionsSnapshot OptionsSnapshot { get; } - public async Task> ListDownloadedFiles() + /// + /// Recursively lists all of the files in the downloads directory, starting from the optional , and + /// optionally applying the specified . + /// + /// An optional parent directory from which to begin searching. + /// Optional enumeration options to apply. + /// The list of found files. + public async Task> ListDownloadedFiles(string parentDirectory = null, EnumerationOptions enumerationOptions = null) { - var dir = new DirectoryInfo(OptionsSnapshot.Value.Directories.Downloads); + var root = OptionsSnapshot.Value.Directories.Downloads; + parentDirectory = Path.Combine(root, parentDirectory) ?? root; + var dir = new DirectoryInfo(parentDirectory); + + enumerationOptions ??= new EnumerationOptions(); var files = await Task.Run(() => { - return dir.GetFiles("*", new EnumerationOptions - { - AttributesToSkip = FileAttributes.Hidden | FileAttributes.System, - IgnoreInaccessible = false, - RecurseSubdirectories = true, - }); + return dir.GetFiles("*", enumerationOptions); }); return files.AsEnumerable(); -- cgit v1.2.3 From 5e9c7f4a4ba0f20ded6133b763cf0d0132fb7caa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 20 Jun 2023 09:12:42 -0500 Subject: implement file service --- .../Common/Exceptions/InvalidDirectoryException.cs | 72 ++++++++++++++++++++++ src/slskd/Files/FileService.cs | 51 +++++++++------ src/slskd/Files/IFileService.cs | 23 ++++--- 3 files changed, 119 insertions(+), 27 deletions(-) create mode 100644 src/slskd/Common/Exceptions/InvalidDirectoryException.cs (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Common/Exceptions/InvalidDirectoryException.cs b/src/slskd/Common/Exceptions/InvalidDirectoryException.cs new file mode 100644 index 00000000..1bfdfda8 --- /dev/null +++ b/src/slskd/Common/Exceptions/InvalidDirectoryException.cs @@ -0,0 +1,72 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using System.Diagnostics.CodeAnalysis; + using System.Runtime.Serialization; + + /// + /// Represents errors that originate when a searched resource can't be located. + /// + [ExcludeFromCodeCoverage] + [Serializable] + public class InvalidDirectoryException : SlskdException + { + /// + /// Initializes a new instance of the class. + /// + public InvalidDirectoryException() + : base() + { + } + + /// + /// Initializes a new instance of the class with a specified error message. + /// + /// The message that describes the error. + public InvalidDirectoryException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a + /// reference to the inner exception that is the cause of this exception. + /// + /// The message that describes the error. + /// + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no + /// inner exception is specified. + /// + public InvalidDirectoryException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class with serialized data. + /// + /// The SerializationInfo that holds the serialized object data about the exception being thrown. + /// The StreamingContext that contains contextual information about the source or destination. + protected InvalidDirectoryException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c03183d5..a579bc79 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -19,11 +19,9 @@ using Microsoft.Extensions.Options; namespace slskd.Files { - using System; using System.Collections.Generic; using System.IO; using System.Linq; - using System.Linq.Expressions; using System.Threading.Tasks; /// @@ -44,31 +42,48 @@ namespace slskd.Files private IOptionsSnapshot OptionsSnapshot { get; } /// - /// Recursively lists all of the files in the downloads directory, starting from the optional , and - /// optionally applying the specified . + /// Lists all of the directories in the specified , optionally applying the + /// specified . /// - /// An optional parent directory from which to begin searching. + /// The directory from which to start the listing. /// Optional enumeration options to apply. - /// The list of found files. - public async Task> ListDownloadedFiles(string parentDirectory = null, EnumerationOptions enumerationOptions = null) + /// The list of found directories. + /// + /// Thrown if the specified directory is not rooted in an allowed directory. + /// + public async Task> ListDirectoriesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) { - var root = OptionsSnapshot.Value.Directories.Downloads; - parentDirectory = Path.Combine(root, parentDirectory) ?? root; - var dir = new DirectoryInfo(parentDirectory); + if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) + { + throw new InvalidDirectoryException($"The directory '{parentDirectory}' is not rooted in any of the allowed directories"); + } - enumerationOptions ??= new EnumerationOptions(); + return await Task.Run(() => new DirectoryInfo(parentDirectory).GetDirectories("*", enumerationOptions)); + } - var files = await Task.Run(() => + /// + /// Lists all of the files in the specified , optionally applying the specified . + /// + /// An optional parent directory from which to begin searching. + /// Optional enumeration options to apply. + /// The list of found files. + /// + /// Thrown if the specified directory is not rooted in an allowed directory. + /// + public async Task> ListFilesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) + { + if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) { - return dir.GetFiles("*", enumerationOptions); - }); + throw new InvalidDirectoryException($"The directory '{parentDirectory}' is not rooted in any of the applowed directories"); + } - return files.AsEnumerable(); + return await Task.Run(() => new DirectoryInfo(parentDirectory).GetFiles("*", enumerationOptions)); } - public Task> ListIncompleteFiles(Expression> expression = null) + private bool IsPermissibleDirectory(string dir, Options.DirectoriesOptions options) { - throw new NotImplementedException(); + var allowedRoots = new[] { options.Downloads, options.Incomplete }; + return allowedRoots.Any(r => dir.StartsWith(r)); } } -} +} \ No newline at end of file diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index a02f7a79..d8174d88 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -27,21 +27,26 @@ namespace slskd.Files public interface IFileService { /// - /// Recursively lists all of the files in the downloads directory, starting from the optional , and - /// optionally applying the specified . + /// Lists all of the directories in the specified , optionally applying the + /// specified . /// - /// An optional parent directory from which to begin searching. + /// The directory from which to start the listing. /// Optional enumeration options to apply. - /// The list of found files. - Task> ListDownloadedFiles(string parentDirectory = null, EnumerationOptions enumerationOptions = null); + /// The list of found directories. + /// + /// Thrown if the specified directory is not rooted in an allowed directory. + /// + Task> ListDirectoriesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); /// - /// Recursively lists all of the files in the incomplete directory, starting from the optional , and - /// optionally applying the specified . + /// Lists all of the files in the specified , optionally applying the specified . /// /// An optional parent directory from which to begin searching. /// Optional enumeration options to apply. /// The list of found files. - Task> ListIncompleteFiles(string parentDirectory = null, EnumerationOptions enumerationOptions = null); + /// + /// Thrown if the specified directory is not rooted in an allowed directory. + /// + Task> ListFilesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); } -} +} \ No newline at end of file -- cgit v1.2.3 From 05bc0e09d54ff4a9e3924668c312610c221bd2b6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 21 Jun 2023 08:54:04 -0500 Subject: simplify contents listing --- src/slskd/Files/FileService.cs | 27 +++++++-------------------- src/slskd/Files/IFileService.cs | 18 ++++-------------- 2 files changed, 11 insertions(+), 34 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index a579bc79..c7c0105c 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -42,42 +42,29 @@ namespace slskd.Files private IOptionsSnapshot OptionsSnapshot { get; } /// - /// Lists all of the directories in the specified , optionally applying the + /// Lists all of the contents of the specified , optionally applying the /// specified . /// /// The directory from which to start the listing. /// Optional enumeration options to apply. - /// The list of found directories. + /// The list of found contents. /// /// Thrown if the specified directory is not rooted in an allowed directory. /// - public async Task> ListDirectoriesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) + /// Thrown if the specified directory does not exist. + public async Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) { if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) { throw new InvalidDirectoryException($"The directory '{parentDirectory}' is not rooted in any of the allowed directories"); } - return await Task.Run(() => new DirectoryInfo(parentDirectory).GetDirectories("*", enumerationOptions)); - } - - /// - /// Lists all of the files in the specified , optionally applying the specified . - /// - /// An optional parent directory from which to begin searching. - /// Optional enumeration options to apply. - /// The list of found files. - /// - /// Thrown if the specified directory is not rooted in an allowed directory. - /// - public async Task> ListFilesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null) - { - if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) + if (!Directory.Exists(parentDirectory)) { - throw new InvalidDirectoryException($"The directory '{parentDirectory}' is not rooted in any of the applowed directories"); + throw new NotFoundException($"The directory '{parentDirectory}' does not exist"); } - return await Task.Run(() => new DirectoryInfo(parentDirectory).GetFiles("*", enumerationOptions)); + return await Task.Run(() => new DirectoryInfo(parentDirectory).GetFileSystemInfos("*", enumerationOptions)); } private bool IsPermissibleDirectory(string dir, Options.DirectoriesOptions options) diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index d8174d88..0b393e64 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -27,26 +27,16 @@ namespace slskd.Files public interface IFileService { /// - /// Lists all of the directories in the specified , optionally applying the + /// Lists all of the contents of the specified , optionally applying the /// specified . /// /// The directory from which to start the listing. /// Optional enumeration options to apply. - /// The list of found directories. + /// The list of found contents. /// /// Thrown if the specified directory is not rooted in an allowed directory. /// - Task> ListDirectoriesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); - - /// - /// Lists all of the files in the specified , optionally applying the specified . - /// - /// An optional parent directory from which to begin searching. - /// Optional enumeration options to apply. - /// The list of found files. - /// - /// Thrown if the specified directory is not rooted in an allowed directory. - /// - Task> ListFilesAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); + /// Thrown if the specified directory does not exist. + Task> ListContentsAsync(string parentDirectory, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file -- cgit v1.2.3 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(-) (limited to 'src/slskd/Files/FileService.cs') 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 From c76f272f380833ad01e98f7946a7e114a1635a6c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 22 Jun 2023 09:57:28 -0500 Subject: obscure full path of files, throw ForbiddenException if root is not whitelisted --- src/slskd/Files/FileService.cs | 39 +++++++++++++++++++++------------------ src/slskd/Files/IFileService.cs | 9 ++++----- 2 files changed, 25 insertions(+), 23 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index dfb5ad7b..7194cc04 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -32,7 +32,6 @@ namespace slskd.Files /// /// Initializes a new instance of the class. /// - /// public FileService( IOptionsSnapshot optionsSnapshot) { @@ -45,28 +44,32 @@ namespace slskd.Files /// Lists the contents in the specified , optionally applying the /// specified . /// - /// The directory from which to start the listing. + /// The root directory. + /// An optional subdirectory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. - /// - /// 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) + /// Thrown if the specified directory is restricted. + public async Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) { - if (!IsPermissibleDirectory(parentDirectory, OptionsSnapshot.Value.Directories)) + var configuredDirectories = OptionsSnapshot.Value.Directories; + var allowedDirectories = new[] { configuredDirectories.Incomplete, configuredDirectories.Downloads }; + + if (!allowedDirectories.Any(d => rootDirectory == d)) { - throw new InvalidDirectoryException($"The directory '{parentDirectory}' is not rooted in any of the allowed directories"); + throw new ForbiddenException($"For security reasons, only the configured Downloads and Incomplete directories can be listed"); } - if (!Directory.Exists(parentDirectory)) + var fullDirectory = Path.Combine(rootDirectory, parentDirectory); + + if (!Directory.Exists(fullDirectory)) { - throw new NotFoundException($"The directory '{parentDirectory}' does not exist"); + throw new NotFoundException($"The directory '{fullDirectory}' does not exist"); } return await Task.Run(() => { - var dir = new DirectoryInfo(parentDirectory); + var dir = new DirectoryInfo(fullDirectory); var contents = dir.GetFileSystemInfos("*", enumerationOptions); @@ -77,16 +80,16 @@ namespace slskd.Files var dirs = contents .OfType() .Prepend(dir) - .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with { Files = files.Where(f => f.Parent == d.FullName) }); + .Select(d => FilesystemDirectory.FromDirectoryInfo(d) with + { + FullName = d.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/'), + Files = files + .Where(f => Path.GetDirectoryName(f.FullName) == d.FullName) + .Select(f => f with { FullName = f.FullName.ReplaceFirst(rootDirectory, string.Empty).TrimStart('\\', '/') }), + }); return dirs; }); } - - private bool IsPermissibleDirectory(string dir, Options.DirectoriesOptions options) - { - var allowedRoots = new[] { options.Downloads, options.Incomplete }; - return allowedRoots.Any(r => dir.StartsWith(r)); - } } } \ No newline at end of file diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index 0a750b59..8e7c780a 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -30,13 +30,12 @@ namespace slskd.Files /// Lists the contents in the specified , optionally applying the /// specified . /// - /// The directory from which to start the listing. + /// The root directory. + /// An optional subdirectory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. - /// - /// 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); + /// Thrown if the specified directory is restricted. + Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file -- cgit v1.2.3 From 1b0154aa8add61d0cfb072747e97b770e3c57603 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Jun 2023 12:51:06 -0500 Subject: make delete operations variadic, return OneOf --- src/slskd/Files/FileService.cs | 109 +++++++++++++++++++++++++++++++++++++--- src/slskd/Files/IFileService.cs | 35 +++++++++++-- 2 files changed, 133 insertions(+), 11 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 7194cc04..f37c861b 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -19,10 +19,12 @@ using Microsoft.Extensions.Options; namespace slskd.Files { + using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; + using OneOf; /// /// Manages files on disk. @@ -38,11 +40,105 @@ namespace slskd.Files OptionsSnapshot = optionsSnapshot; } + private IEnumerable AllowedDirectories => new[] { OptionsSnapshot.Value.Directories.Downloads, OptionsSnapshot.Value.Directories.Incomplete }; private IOptionsSnapshot OptionsSnapshot { get; } /// - /// Lists the contents in the specified , optionally applying the - /// specified . + /// Deletes the specified . + /// + /// + /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are + /// contained in the result, and are not thrown. + /// + /// The root directory. + /// The directories to delete. + /// The operation context. + /// Thrown if a specified directory does not exist. + /// Thrown if a specified directory is restricted. + public async Task>> DeleteDirectoriesAsync(string rootDirectory, params string[] directories) + { + if (!AllowedDirectories.Any(d => rootDirectory == d)) + { + throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); + } + + await Task.Yield(); + + Dictionary> results = new(); + + foreach (var directory in directories) + { + try + { + Directory.Delete(Path.Combine(rootDirectory, directory), recursive: true); + results.Add(directory, true); + } + catch (FileNotFoundException) + { + results.Add(directory, new NotFoundException($"The directory '{directory}' does not exist")); + } + catch (UnauthorizedAccessException) + { + results.Add(directory, new ForbiddenException()); + } + catch (Exception ex) + { + results.Add(directory, ex); + } + } + + return results; + } + + /// + /// Deletes the specified . + /// + /// + /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are + /// contained in the result, and are not thrown. + /// + /// The root directory. + /// The list of files to delete. + /// The operation context. + /// Thrown if a specified file does not exist. + /// Thrown if a specified file is restricted. + public async Task>> DeleteFilesAsync(string rootDirectory, params string[] files) + { + if (!AllowedDirectories.Any(d => rootDirectory == d)) + { + throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); + } + + await Task.Yield(); + + Dictionary> results = new(); + + foreach (var file in files) + { + try + { + File.Delete(Path.Combine(rootDirectory, file)); + results.Add(file, true); + } + catch (FileNotFoundException) + { + results.Add(file, new NotFoundException($"The file '{file}' does not exist")); + } + catch (UnauthorizedAccessException) + { + results.Add(file, new ForbiddenException()); + } + catch (Exception ex) + { + results.Add(file, ex); + } + } + + return results; + } + + /// + /// Lists the contents in the specified , optionally applying the specified . /// /// The root directory. /// An optional subdirectory from which to start the listing. @@ -52,15 +148,12 @@ namespace slskd.Files /// Thrown if the specified directory is restricted. public async Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) { - var configuredDirectories = OptionsSnapshot.Value.Directories; - var allowedDirectories = new[] { configuredDirectories.Incomplete, configuredDirectories.Downloads }; - - if (!allowedDirectories.Any(d => rootDirectory == d)) + if (!AllowedDirectories.Any(d => rootDirectory == d)) { - throw new ForbiddenException($"For security reasons, only the configured Downloads and Incomplete directories can be listed"); + throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); } - var fullDirectory = Path.Combine(rootDirectory, parentDirectory); + var fullDirectory = parentDirectory is not null ? Path.Combine(rootDirectory, parentDirectory) : rootDirectory; if (!Directory.Exists(fullDirectory)) { diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs index 8e7c780a..8e3ddcb7 100644 --- a/src/slskd/Files/IFileService.cs +++ b/src/slskd/Files/IFileService.cs @@ -17,9 +17,11 @@ namespace slskd.Files { + using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; + using OneOf; /// /// Manages files on disk. @@ -27,15 +29,42 @@ namespace slskd.Files public interface IFileService { /// - /// Lists the contents in the specified , optionally applying the - /// specified . + /// Deletes the specified . + /// + /// + /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are + /// contained in the result, and are not thrown. + /// + /// The root directory. + /// The directories to delete. + /// The operation context. + /// Thrown if a specified directory does not exist. + /// Thrown if a specified directory is restricted. + Task>> DeleteDirectoriesAsync(string rootDirectory, params string[] directories); + + /// + /// Deletes the specified . + /// + /// + /// Returns a dictionary keyed on directory name and containing a result for each specified directory. Exceptions are + /// contained in the result, and are not thrown. + /// + /// The root directory. + /// The list of files to delete. + /// The operation context. + /// Thrown if a specified file does not exist. + /// Thrown if a specified file is restricted. + Task>> DeleteFilesAsync(string rootDirectory, params string[] files); + + /// + /// Lists the contents in the specified , optionally applying the specified . /// /// The root directory. /// An optional subdirectory from which to start the listing. /// Optional enumeration options to apply. /// The list of found contents. /// Thrown if the specified directory does not exist. - /// Thrown if the specified directory is restricted. + /// Thrown if the specified root directory is restricted. Task> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null); } } \ No newline at end of file -- cgit v1.2.3