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 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 101 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 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)) { -- cgit v1.2.3