From 8191c984c52e886c3fba40c7fdcc48f9382abf46 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Apr 2024 18:11:03 -0500 Subject: add Exit() --- src/slskd/Program.cs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index d7f6caca..918ca828 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -241,6 +241,12 @@ namespace slskd [Argument('v', "version", "display version information")] private static bool ShowVersion { get; set; } + /// + /// Panic. + /// + /// An optional exit code. + public static void Exit(int code = 1) => Environment.Exit(code); + /// /// Entrypoint. /// @@ -576,7 +582,9 @@ namespace slskd services.AddTransient(); services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3 From 9a005447ef2f6726b2139b0ae4d99cf566ef365b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 11 May 2024 15:57:05 -0500 Subject: simplify file service --- src/slskd/Files/API/FilesController.cs | 4 +- src/slskd/Files/FileService.cs | 9 ++- src/slskd/Files/IFileService.cs | 74 ------------------------ src/slskd/Program.cs | 2 +- tests/slskd.Tests.Unit/Files/FileServiceTests.cs | 2 +- 5 files changed, 8 insertions(+), 83 deletions(-) delete mode 100644 src/slskd/Files/IFileService.cs (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index b99af658..1ced3e63 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -39,14 +39,14 @@ namespace slskd.Files.API public class FilesController : ControllerBase { public FilesController( - IFileService fileService, + FileService fileService, IOptionsSnapshot optionsSnapshot) { Files = fileService; OptionsSnapshot = optionsSnapshot; } - private IFileService Files { get; } + private FileService Files { get; } private IOptionsSnapshot OptionsSnapshot { get; } private ILogger Log { get; set; } = Serilog.Log.ForContext(); diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c820220f..ee8271b1 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -23,7 +23,6 @@ namespace slskd.Files using System.Collections.Generic; using System.IO; using System.Linq; - using System.Security; using System.Threading.Tasks; using OneOf; using Serilog; @@ -31,7 +30,7 @@ namespace slskd.Files /// /// Manages files on disk. /// - public class FileService : IFileService + public class FileService { /// /// Initializes a new instance of the class. @@ -66,7 +65,7 @@ namespace slskd.Files /// /// Thrown if a specified directory does not exist. /// Thrown if a specified directory is restricted. - public async Task>> DeleteDirectoriesAsync(params string[] directories) + public virtual async Task>> DeleteDirectoriesAsync(params string[] directories) { if (directories.Any(directory => Path.GetFullPath(directory) != directory)) { @@ -129,7 +128,7 @@ namespace slskd.Files /// Thrown if any of the specified files have a relative path. /// Thrown if a specified file does not exist. /// Thrown if a specified file is restricted. - public async Task>> DeleteFilesAsync(params string[] files) + public virtual async Task>> DeleteFilesAsync(params string[] files) { if (files.Any(file => Path.GetFullPath(file) != file)) { @@ -182,7 +181,7 @@ namespace slskd.Files /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. /// Thrown if the specified root directory is restricted. - public async Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null) + public virtual async Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null) { if (Path.GetFullPath(directory) != directory) { diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs deleted file mode 100644 index 89d4ce76..00000000 --- a/src/slskd/Files/IFileService.cs +++ /dev/null @@ -1,74 +0,0 @@ -// -// 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.Files -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Security; - using System.Threading.Tasks; - using OneOf; - - /// - /// Manages files on disk. - /// - public interface IFileService - { - /// - /// 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 directories to delete. - /// The operation context. - /// Thrown if any of the specified directories have a relative path. - /// - /// Thrown if any of the directories is an exact match for an application-controlled directory. - /// - /// Thrown if a specified directory does not exist. - /// Thrown if a specified directory is restricted. - Task>> DeleteDirectoriesAsync(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 list of files to delete. - /// The operation context. - /// Thrown if any of the specified files have a relative path. - /// Thrown if a specified file does not exist. - /// Thrown if a specified file is restricted. - Task>> DeleteFilesAsync(params string[] files); - - /// - /// Lists the contents in the specified , optionally applying the specified . - /// - /// The directory from which to start the listing. - /// Optional enumeration options to apply. - /// The list of found contents. - /// Thrown if the specified directory has a relative path. - /// Thrown if the specified directory does not exist. - /// Thrown if the specified root directory is restricted. - Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null); - } -} \ No newline at end of file diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 918ca828..545a80ab 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -590,7 +590,7 @@ namespace slskd services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddTransient(); + services.AddTransient(); services.AddSingleton(); diff --git a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs index 7c9931ab..917d1a43 100644 --- a/tests/slskd.Tests.Unit/Files/FileServiceTests.cs +++ b/tests/slskd.Tests.Unit/Files/FileServiceTests.cs @@ -29,7 +29,7 @@ namespace slskd.Tests.Unit.Files private Mock> OptionsSnapshotMock { get; init; } private string Temp { get; init; } - private IFileService FileService { get; init; } + private FileService FileService { get; init; } [Fact] public async Task ListContentsAsync_Throws_ArgumentException_Given_Relative_Path() -- cgit v1.2.3 From e4c20ae1a67dc6d03db6f3a6f6e8eac1c612d3f3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 11 May 2024 19:27:45 -0500 Subject: make FileService singleton --- src/slskd/Files/FileService.cs | 10 +++++----- src/slskd/Program.cs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c3da7621..18bf72d1 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -36,19 +36,19 @@ namespace slskd.Files /// Initializes a new instance of the class. /// public FileService( - IOptionsSnapshot optionsSnapshot) + IOptionsMonitor optionsMonitor) { - OptionsSnapshot = optionsSnapshot; + OptionsMonitor = optionsMonitor; } private IEnumerable AllowedDirectories => new[] { - Path.GetFullPath(OptionsSnapshot.Value.Directories.Downloads), - Path.GetFullPath(OptionsSnapshot.Value.Directories.Incomplete), + Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Downloads), + Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Incomplete), }; private ILogger Log { get; } = Serilog.Log.ForContext(); - private IOptionsSnapshot OptionsSnapshot { get; } + private IOptionsMonitor OptionsMonitor { get; } /// /// Deletes the specified . diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 545a80ab..27cbfc6f 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -590,7 +590,7 @@ namespace slskd services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddTransient(); + services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3