diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-06-26 07:51:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-26 07:51:53 -0500 |
| commit | 4a494dfa5783d250c41ca3662898594c8dd8f013 (patch) | |
| tree | cbfb520591b8e9fce535d93088c9f0990ab6ad84 | |
| parent | 674003a0c9cf48a20ef47de429b78b44098747ea (diff) | |
| parent | 197865c54b2eb50c43d71b89f4587955969a0924 (diff) | |
| download | slskd-4a494dfa5783d250c41ca3662898594c8dd8f013.tar.gz | |
Merge pull request #924 from slskd/downloaded-files
Add API endpoints for listing and deleting downloaded and incomplete files
| -rw-r--r-- | config/slskd.example.yml | 1 | ||||
| -rw-r--r-- | src/slskd/Common/Exceptions/ForbiddenException.cs | 72 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Files/API/FilesController.cs | 285 | ||||
| -rw-r--r-- | src/slskd/Files/FileService.cs | 188 | ||||
| -rw-r--r-- | src/slskd/Files/IFileService.cs | 70 | ||||
| -rw-r--r-- | src/slskd/Files/Types/FilesystemDirectory.cs | 45 | ||||
| -rw-r--r-- | src/slskd/Files/Types/FilesystemFile.cs | 45 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 2 | ||||
| -rw-r--r-- | src/slskd/slskd.csproj | 1 |
10 files changed, 717 insertions, 1 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index cbf59dab..6a36161e 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -1,5 +1,6 @@ # debug: false # remote_configuration: false +# remote_file_management: false # instance_name: default # flags: # no_logo: false diff --git a/src/slskd/Common/Exceptions/ForbiddenException.cs b/src/slskd/Common/Exceptions/ForbiddenException.cs new file mode 100644 index 00000000..814b8cfc --- /dev/null +++ b/src/slskd/Common/Exceptions/ForbiddenException.cs @@ -0,0 +1,72 @@ +// <copyright file="ForbiddenException.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd +{ + using System; + using System.Diagnostics.CodeAnalysis; + using System.Runtime.Serialization; + + /// <summary> + /// Represents errors that originate when an attempt is made to access a restricted resource. + /// </summary> + [ExcludeFromCodeCoverage] + [Serializable] + public class ForbiddenException : SlskdException + { + /// <summary> + /// Initializes a new instance of the <see cref="ForbiddenException"/> class. + /// </summary> + public ForbiddenException() + : base() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ForbiddenException"/> class with a specified error message. + /// </summary> + /// <param name="message">The message that describes the error.</param> + public ForbiddenException(string message) + : base(message) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ForbiddenException"/> class with a specified error message and a + /// reference to the inner exception that is the cause of this exception. + /// </summary> + /// <param name="message">The message that describes the error.</param> + /// <param name="innerException"> + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no + /// inner exception is specified. + /// </param> + public ForbiddenException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="ForbiddenException"/> class with serialized data. + /// </summary> + /// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param> + /// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param> + protected ForbiddenException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +}
\ No newline at end of file diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 1d9f814c..c438c03c 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -31,7 +31,6 @@ namespace slskd using NetTools; using slskd.Authentication; using slskd.Configuration; - using slskd.Cryptography; using slskd.Relay; using slskd.Shares; using slskd.Validation; @@ -159,6 +158,14 @@ namespace slskd public bool RemoteConfiguration { get; init; } = false; /// <summary> + /// Gets a value indicating whether remote file management is allowed. + /// </summary> + [Argument(default, "remote-file-management")] + [EnvironmentVariable("REMOTE_FILE_MANAGEMENT")] + [Description("allow remote file management")] + public bool RemoteFileManagement { get; init; } = false; + + /// <summary> /// Gets the unique name for this instance. /// </summary> [Argument('i', "instance-name")] diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs new file mode 100644 index 00000000..d39cffbd --- /dev/null +++ b/src/slskd/Files/API/FilesController.cs @@ -0,0 +1,285 @@ +// <copyright file="FilesController.cs" company="slskd Team"> +// 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/. +// </copyright> + +using Microsoft.Extensions.Options; + +namespace slskd.Files.API +{ + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using System.IO; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using OneOf; + + /// <summary> + /// Files. + /// </summary> + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class FilesController : ControllerBase + { + public FilesController( + IFileService fileService, + IOptionsSnapshot<Options> optionsSnapshot) + { + Files = fileService; + OptionsSnapshot = optionsSnapshot; + } + + private IFileService Files { get; } + private IOptionsSnapshot<Options> OptionsSnapshot { get; } + + [HttpGet("downloads/directories")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable<FilesystemDirectory>), 200)] + public async Task<IActionResult> GetDownloadContentsAsync([FromQuery] bool recursive = false) + { + try + { + var response = await Files.ListContentsAsync( + rootDirectory: OptionsSnapshot.Value.Directories.Downloads, + enumerationOptions: new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(response); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpGet("downloads/directories/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable<FilesystemDirectory>), 200)] + public async Task<IActionResult> GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery]bool recursive = false) + { + var decodedDir = base64SubdirectoryName.FromBase64(); + + try + { + var response = await Files.ListContentsAsync( + rootDirectory: OptionsSnapshot.Value.Directories.Downloads, + parentDirectory: decodedDir, + enumerationOptions: new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(response); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpDelete("downloads/directories/{base64SubdirectoryName")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary<string, OneOf<bool, string>>), 204)] + public async Task<IActionResult> DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + + var decodedDir = base64SubdirectoryName.FromBase64(); + + try + { + var results = await Files.DeleteDirectoriesAsync(OptionsSnapshot.Value.Directories.Downloads, decodedDir); + + return results[decodedDir].Match( + success => NoContent(), + failure => throw failure); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpDelete("downloads/files/{base64Filename}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary<string, OneOf<bool, string>>), 204)] + public async Task<IActionResult> DeleteDownloadFileAsync([FromRoute] string base64filename) + { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + + var decodedFilename = base64filename.FromBase64(); + + try + { + var results = await Files.DeleteFilesAsync(OptionsSnapshot.Value.Directories.Downloads, base64filename); + + return results[decodedFilename].Match( + success => NoContent(), + failure => throw failure); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpGet("incomplete/directories")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable<FilesystemDirectory>), 200)] + public async Task<IActionResult> GetIncompleteContentsAsync([FromQuery] bool recursive = false) + { + try + { + var response = await Files.ListContentsAsync( + rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, + enumerationOptions: new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(response); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpGet("incomplete/directories/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable<FilesystemDirectory>), 200)] + public async Task<IActionResult> GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false) + { + var decodedDir = base64SubdirectoryName.FromBase64(); + + try + { + var response = await Files.ListContentsAsync( + rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, + parentDirectory: decodedDir, + enumerationOptions: new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(response); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpDelete("incomplete/directories/{base64SubdirectoryName")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary<string, OneOf<bool, string>>), 204)] + public async Task<IActionResult> DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + + var decodedDir = base64SubdirectoryName.FromBase64(); + + try + { + var results = await Files.DeleteDirectoriesAsync(OptionsSnapshot.Value.Directories.Incomplete, decodedDir); + + return results[decodedDir].Match( + success => NoContent(), + failure => throw failure); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpDelete("incomplete/files/{base64Filename}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary<string, OneOf<bool, string>>), 204)] + public async Task<IActionResult> DeleteIncompleteFileAsync([FromRoute] string base64filename) + { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + + var decodedFilename = base64filename.FromBase64(); + + try + { + var results = await Files.DeleteFilesAsync(OptionsSnapshot.Value.Directories.Incomplete, base64filename); + + return results[decodedFilename].Match( + success => NoContent(), + failure => throw failure); + } + catch (ForbiddenException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + } +} diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs new file mode 100644 index 00000000..f37c861b --- /dev/null +++ b/src/slskd/Files/FileService.cs @@ -0,0 +1,188 @@ +// <copyright file="FileService.cs" company="slskd Team"> +// 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/. +// </copyright> + +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; + + /// <summary> + /// Manages files on disk. + /// </summary> + public class FileService : IFileService + { + /// <summary> + /// Initializes a new instance of the <see cref="FileService"/> class. + /// </summary> + public FileService( + IOptionsSnapshot<Options> optionsSnapshot) + { + OptionsSnapshot = optionsSnapshot; + } + + private IEnumerable<string> AllowedDirectories => new[] { OptionsSnapshot.Value.Directories.Downloads, OptionsSnapshot.Value.Directories.Incomplete }; + private IOptionsSnapshot<Options> OptionsSnapshot { get; } + + /// <summary> + /// Deletes the specified <paramref name="directories"/>. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <param name="rootDirectory">The root directory.</param> + /// <param name="directories">The directories to delete.</param> + /// <returns>The operation context.</returns> + /// <exception cref="NotFoundException">Thrown if a specified directory does not exist.</exception> + /// <exception cref="ForbiddenException">Thrown if a specified directory is restricted.</exception> + public async Task<Dictionary<string, OneOf<bool, Exception>>> 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<string, OneOf<bool, Exception>> 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; + } + + /// <summary> + /// Deletes the specified <paramref name="files"/>. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <param name="rootDirectory">The root directory.</param> + /// <param name="files">The list of files to delete.</param> + /// <returns>The operation context.</returns> + /// <exception cref="NotFoundException">Thrown if a specified file does not exist.</exception> + /// <exception cref="ForbiddenException">Thrown if a specified file is restricted.</exception> + public async Task<Dictionary<string, OneOf<bool, Exception>>> 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<string, OneOf<bool, Exception>> 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; + } + + /// <summary> + /// Lists the contents in the specified <paramref name="parentDirectory"/>, optionally applying the specified <paramref name="enumerationOptions"/>. + /// </summary> + /// <param name="rootDirectory">The root directory.</param> + /// <param name="parentDirectory">An optional subdirectory from which to start the listing.</param> + /// <param name="enumerationOptions">Optional enumeration options to apply.</param> + /// <returns>The list of found contents.</returns> + /// <exception cref="NotFoundException">Thrown if the specified directory does not exist.</exception> + /// <exception cref="ForbiddenException">Thrown if the specified directory is restricted.</exception> + public async Task<IEnumerable<FilesystemDirectory>> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null) + { + if (!AllowedDirectories.Any(d => rootDirectory == d)) + { + throw new ForbiddenException($"For security reasons, only application-controlled directories can be deleted"); + } + + var fullDirectory = parentDirectory is not null ? Path.Combine(rootDirectory, parentDirectory) : rootDirectory; + + if (!Directory.Exists(fullDirectory)) + { + throw new NotFoundException($"The directory '{fullDirectory}' does not exist"); + } + + return await Task.Run(() => + { + var dir = new DirectoryInfo(fullDirectory); + + var contents = dir.GetFileSystemInfos("*", enumerationOptions); + + var files = contents + .OfType<FileInfo>() + .Select(f => FilesystemFile.FromFileInfo(f)); + + var dirs = contents + .OfType<DirectoryInfo>() + .Prepend(dir) + .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; + }); + } + } +}
\ No newline at end of file diff --git a/src/slskd/Files/IFileService.cs b/src/slskd/Files/IFileService.cs new file mode 100644 index 00000000..8e3ddcb7 --- /dev/null +++ b/src/slskd/Files/IFileService.cs @@ -0,0 +1,70 @@ +// <copyright file="IFileService.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Files +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Threading.Tasks; + using OneOf; + + /// <summary> + /// Manages files on disk. + /// </summary> + public interface IFileService + { + /// <summary> + /// Deletes the specified <paramref name="directories"/>. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <param name="rootDirectory">The root directory.</param> + /// <param name="directories">The directories to delete.</param> + /// <returns>The operation context.</returns> + /// <exception cref="NotFoundException">Thrown if a specified directory does not exist.</exception> + /// <exception cref="ForbiddenException">Thrown if a specified directory is restricted.</exception> + Task<Dictionary<string, OneOf<bool, Exception>>> DeleteDirectoriesAsync(string rootDirectory, params string[] directories); + + /// <summary> + /// Deletes the specified <paramref name="files"/>. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <param name="rootDirectory">The root directory.</param> + /// <param name="files">The list of files to delete.</param> + /// <returns>The operation context.</returns> + /// <exception cref="NotFoundException">Thrown if a specified file does not exist.</exception> + /// <exception cref="ForbiddenException">Thrown if a specified file is restricted.</exception> + Task<Dictionary<string, OneOf<bool, Exception>>> DeleteFilesAsync(string rootDirectory, params string[] files); + + /// <summary> + /// Lists the contents in the specified <paramref name="parentDirectory"/>, optionally applying the specified <paramref name="enumerationOptions"/>. + /// </summary> + /// <param name="rootDirectory">The root directory.</param> + /// <param name="parentDirectory">An optional subdirectory from which to start the listing.</param> + /// <param name="enumerationOptions">Optional enumeration options to apply.</param> + /// <returns>The list of found contents.</returns> + /// <exception cref="NotFoundException">Thrown if the specified directory does not exist.</exception> + /// <exception cref="ForbiddenException">Thrown if the specified root directory is restricted.</exception> + Task<IEnumerable<FilesystemDirectory>> ListContentsAsync(string rootDirectory, string parentDirectory = null, EnumerationOptions enumerationOptions = null); + } +}
\ No newline at end of file diff --git a/src/slskd/Files/Types/FilesystemDirectory.cs b/src/slskd/Files/Types/FilesystemDirectory.cs new file mode 100644 index 00000000..4183c724 --- /dev/null +++ b/src/slskd/Files/Types/FilesystemDirectory.cs @@ -0,0 +1,45 @@ +// <copyright file="FilesystemDirectory.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Files +{ + using System; + using System.Collections.Generic; + using System.IO; + + public record FilesystemDirectory + { + public string Name { get; init; } + public string FullName { get; init; } + public FileAttributes Attributes { get; init; } + public DateTime CreatedAt { get; init; } + public DateTime ModifiedAt { get; init; } + public IEnumerable<FilesystemFile> Files { get; init; } + + public static FilesystemDirectory FromDirectoryInfo(DirectoryInfo i) + { + return new FilesystemDirectory + { + Name = i.Name, + FullName = i.FullName, + Attributes = i.Attributes, + CreatedAt = i.CreationTimeUtc, + ModifiedAt = i.LastWriteTimeUtc, + }; + } + } +} diff --git a/src/slskd/Files/Types/FilesystemFile.cs b/src/slskd/Files/Types/FilesystemFile.cs new file mode 100644 index 00000000..fc36fff0 --- /dev/null +++ b/src/slskd/Files/Types/FilesystemFile.cs @@ -0,0 +1,45 @@ +// <copyright file="FilesystemFile.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Files +{ + using System; + using System.IO; + + public record FilesystemFile + { + public string Name { get; init; } + public string FullName { get; init; } + public long Length { get; init; } + public FileAttributes Attributes { get; init; } + public DateTime CreatedAt { get; init; } + public DateTime ModifiedAt { get; init; } + + public static FilesystemFile FromFileInfo(FileInfo i) + { + return new FilesystemFile + { + Name = i.Name, + FullName = i.FullName, + Length = i.Length, + Attributes = i.Attributes, + CreatedAt = i.CreationTimeUtc, + ModifiedAt = i.LastWriteTimeUtc, + }; + } + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 1236865f..060284d8 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -55,6 +55,7 @@ namespace slskd using slskd.Configuration; using slskd.Core.API; using slskd.Cryptography; + using slskd.Files; using slskd.Integrations.FTP; using slskd.Integrations.Pushbullet; using slskd.Messaging; @@ -556,6 +557,7 @@ namespace slskd services.AddSingleton<ITransferService, TransferService>(); services.AddSingleton<IDownloadService, DownloadService>(); services.AddSingleton<IUploadService, UploadService>(); + services.AddTransient<IFileService, FileService>(); services.AddSingleton<IRelayService, RelayService>(); diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 74fad760..0bc82fda 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -54,6 +54,7 @@ <PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="7.0.0" /> + <PackageReference Include="OneOf" Version="3.0.255" /> <PackageReference Include="prometheus-net" Version="7.0.0" /> <PackageReference Include="prometheus-net.AspNetCore" Version="7.0.0" /> <PackageReference Include="prometheus-net.AspNetCore.HealthChecks" Version="7.0.0" /> |
