From 26786311ffc3a01bdd15158fe0c03bb58d8391ec Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jun 2023 09:43:11 -0500 Subject: include subdirectories in directory body, return only one --- src/slskd/Files/API/FilesController.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/slskd/Files/API/FilesController.cs') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 08895639..f4b3ce87 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -50,7 +50,7 @@ namespace slskd.Files.API [HttpGet("downloads/directories")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] + [ProducesResponseType(typeof(FilesystemDirectory), 200)] public async Task GetDownloadContentsAsync([FromQuery] bool recursive = false) { try @@ -77,7 +77,7 @@ namespace slskd.Files.API [HttpGet("downloads/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] + [ProducesResponseType(typeof(FilesystemDirectory), 200)] public async Task GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery] bool recursive = false) { var decodedDir = base64SubdirectoryName.FromBase64(); @@ -167,7 +167,7 @@ namespace slskd.Files.API [HttpGet("incomplete/directories")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] + [ProducesResponseType(typeof(FilesystemDirectory), 200)] public async Task GetIncompleteContentsAsync([FromQuery] bool recursive = false) { try @@ -194,7 +194,7 @@ namespace slskd.Files.API [HttpGet("incomplete/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] + [ProducesResponseType(typeof(FilesystemDirectory), 200)] public async Task GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false) { var decodedDir = base64SubdirectoryName.FromBase64(); -- cgit v1.2.3 From 7316f144c2fca00fbcfe54b65846a4102ea536ab Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 1 Jul 2023 15:12:12 -0500 Subject: refactor, simplify, document --- src/slskd/Files/API/FilesController.cs | 288 ++++++++++++++++----------------- 1 file changed, 143 insertions(+), 145 deletions(-) (limited to 'src/slskd/Files/API/FilesController.cs') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index f4b3ce87..12c642c0 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -22,10 +22,12 @@ namespace slskd.Files.API using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; + using System.Security; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using OneOf; + using Serilog; /// /// Files. @@ -47,163 +49,145 @@ namespace slskd.Files.API private IFileService Files { get; } private IOptionsSnapshot OptionsSnapshot { get; } + private ILogger Log { get; set; } = Serilog.Log.ForContext(); + /// + /// Lists the contents of the downloads directory. + /// + /// An optional value indicating whether to recursively list subdirectories and files. + /// + /// The request completed successfully. + /// Authentication failed. [HttpGet("downloads/directories")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(FilesystemDirectory), 200)] - public async Task 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(); - } - } + [ProducesResponseType(401)] + public Task GetDownloadContentsAsync([FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName: null, recursive); + /// + /// Lists the contents of the specified subdirectory within the downloads directory. + /// + /// The relative, base 64 encoded, name of the subdirectory to list. + /// An optional value indicating whether to recursively list subdirectories and files. + /// + /// The request completed successfully. + /// Authentication failed. + /// Access to the specified subdirectory was denied. + /// The specified subdirectory does not exist. [HttpGet("downloads/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(FilesystemDirectory), 200)] - public async Task 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(); - } - } + [ProducesResponseType(401)] + [ProducesResponseType(403)] + [ProducesResponseType(404)] + public Task GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName, recursive); + /// + /// Deletes the specified subdirectory within the downloads directory. + /// + /// The relative, base 64 encoded, name of the subdirectory to delete. + /// + /// The request completed successfully. + /// Authentication failed. + /// Access to the specified subdirectory was denied. + /// The specified subdirectory does not exist. [HttpDelete("downloads/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary>), 204)] - public async Task 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(); - } - } + public Task DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + => DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName); + /// + /// Deletes the specified file within the downloads directory. + /// + /// The relative, base 64 encoded, name of the file to delete. + /// + /// The request completed successfully. + /// Authentication failed. + /// Access to the specified subdirectory was denied. + /// The specified subdirectory does not exist. [HttpDelete("downloads/files/{base64Filename}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary>), 204)] - public async Task 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(); - } - } + public Task DeleteDownloadFileAsync([FromRoute] string base64filename) + => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64filename); + /// + /// Lists the contents of the downloads directory. + /// + /// An optional value indicating whether to recursively list subdirectories and files. + /// + /// The request completed successfully. + /// Authentication failed. [HttpGet("incomplete/directories")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(FilesystemDirectory), 200)] - public async Task 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(); - } - } + public Task GetIncompleteContentsAsync([FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName: null, recursive); + /// + /// Lists the contents of the specified subdirectory within the incomplete directory. + /// + /// The relative, base 64 encoded, name of the subdirectory to list. + /// An optional value indicating whether to recursively list subdirectories and files. + /// + /// The request completed successfully. + /// Authentication failed. + /// Access to the specified subdirectory was denied. + /// The specified subdirectory does not exist. [HttpGet("incomplete/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(FilesystemDirectory), 200)] - public async Task GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false) + public Task GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName, recursive); + + /// + /// Deletes the specified subdirectory within the downloads directory. + /// + /// The relative, base 64 encoded, name of the subdirectory to delete. + /// + /// The request completed successfully. + /// Authentication failed. + /// Access to the specified subdirectory was denied. + /// The specified subdirectory does not exist. + [HttpDelete("incomplete/directories/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>), 204)] + public Task DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + => DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName); + + /// + /// Deletes the specified file within the downloads directory. + /// + /// The relative, base 64 encoded, name of the file to delete. + /// + /// The request completed successfully. + /// Authentication failed. + /// Access to the specified subdirectory was denied. + /// The specified subdirectory does not exist. + [HttpDelete("incomplete/files/{base64Filename}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(204)] + public Task DeleteIncompleteFileAsync([FromRoute] string base64filename) + => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64filename); + + private async Task ListDirectoryAsync(string rootDirectory, string base64SubdirectoryName = null, bool recursive = false) { - var decodedDir = base64SubdirectoryName.FromBase64(); + var requestedDir = (base64SubdirectoryName ?? string.Empty) + .FromBase64() + .Replace('\\', Path.DirectorySeparatorChar) + .Replace('/', Path.DirectorySeparatorChar); + + requestedDir = Path.GetFullPath(Path.Combine(rootDirectory, requestedDir)); + + Log.Debug("Listing directory '{Directory}'", requestedDir); try { var response = await Files.ListContentsAsync( - rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, - parentDirectory: decodedDir, + directory: requestedDir, enumerationOptions: new EnumerationOptions { AttributesToSkip = FileAttributes.System, @@ -212,72 +196,86 @@ namespace slskd.Files.API return Ok(response); } - catch (ForbiddenException) + catch (SecurityException) { + Log.Warning("Directory listing of '{Directory}' forbidden", requestedDir); return Forbid(); } catch (NotFoundException) { + Log.Debug("Directory '{Directory}' not found", requestedDir); return NotFound(); } } - [HttpDelete("incomplete/directories/{base64SubdirectoryName}")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 204)] - public async Task DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + private async Task DeleteSubdirectoryAsync(string rootDirectory, string base64SubdirectoryName) { if (!OptionsSnapshot.Value.RemoteFileManagement) { return Forbid(); } - var decodedDir = base64SubdirectoryName.FromBase64(); + var requestedDir = base64SubdirectoryName + .FromBase64() + .Replace('\\', Path.DirectorySeparatorChar) + .Replace('/', Path.DirectorySeparatorChar); + + requestedDir = Path.GetFullPath(Path.Combine(rootDirectory, requestedDir)); + + Log.Information("Deleting directory '{Directory}'", requestedDir); try { - var results = await Files.DeleteDirectoriesAsync(OptionsSnapshot.Value.Directories.Incomplete, decodedDir); + var results = await Files.DeleteDirectoriesAsync(requestedDir); - return results[decodedDir].Match( + return results[requestedDir].Match( success => NoContent(), failure => throw failure); } - catch (ForbiddenException) + catch (SecurityException) { + Log.Warning("Directory deletion of '{Directory}' forbidden", requestedDir); return Forbid(); } catch (NotFoundException) { + Log.Information("Directory '{Directory}' not found", requestedDir); return NotFound(); } } - [HttpDelete("incomplete/files/{base64Filename}")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 204)] - public async Task DeleteIncompleteFileAsync([FromRoute] string base64filename) + private async Task DeleteFileAsync(string rootDirectory, string base64Filename) { if (!OptionsSnapshot.Value.RemoteFileManagement) { return Forbid(); } - var decodedFilename = base64filename.FromBase64(); + var requestedFilename = base64Filename + .FromBase64() + .Replace('\\', Path.DirectorySeparatorChar) + .Replace('/', Path.DirectorySeparatorChar); + + requestedFilename = Path.GetFullPath(Path.Combine(rootDirectory, requestedFilename)); + + Log.Information("Deleting file '{File}'", requestedFilename); try { - var results = await Files.DeleteFilesAsync(OptionsSnapshot.Value.Directories.Incomplete, base64filename); + var results = await Files.DeleteFilesAsync(requestedFilename); - return results[decodedFilename].Match( + return results[requestedFilename].Match( success => NoContent(), failure => throw failure); } - catch (ForbiddenException) + catch (SecurityException) { + Log.Warning("File deletion of '{File}' forbidden", requestedFilename); return Forbid(); } catch (NotFoundException) { + Log.Information("File '{File}' not found", requestedFilename); return NotFound(); } } -- cgit v1.2.3 From d3044304c1543b4df899a0eb1d4edf21b0ed3a61 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 2 Jul 2023 13:08:31 -0500 Subject: tweak --- src/slskd/Files/API/FilesController.cs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/slskd/Files/API/FilesController.cs') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 12c642c0..2bb2d051 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -95,24 +95,24 @@ namespace slskd.Files.API /// The specified subdirectory does not exist. [HttpDelete("downloads/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 204)] + [ProducesResponseType(204)] public Task DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName) => DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName); /// /// Deletes the specified file within the downloads directory. /// - /// The relative, base 64 encoded, name of the file to delete. + /// The relative, base 64 encoded, name of the file to delete. /// /// The request completed successfully. /// Authentication failed. /// Access to the specified subdirectory was denied. /// The specified subdirectory does not exist. - [HttpDelete("downloads/files/{base64Filename}")] + [HttpDelete("downloads/files/{base64FileName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 204)] - public Task DeleteDownloadFileAsync([FromRoute] string base64filename) - => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64filename); + [ProducesResponseType(204)] + public Task DeleteDownloadFileAsync([FromRoute] string base64FileName) + => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64FileName); /// /// Lists the contents of the downloads directory. @@ -154,24 +154,24 @@ namespace slskd.Files.API /// The specified subdirectory does not exist. [HttpDelete("incomplete/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 204)] + [ProducesResponseType(204)] public Task DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) => DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName); /// /// Deletes the specified file within the downloads directory. /// - /// The relative, base 64 encoded, name of the file to delete. + /// The relative, base 64 encoded, name of the file to delete. /// /// The request completed successfully. /// Authentication failed. /// Access to the specified subdirectory was denied. /// The specified subdirectory does not exist. - [HttpDelete("incomplete/files/{base64Filename}")] + [HttpDelete("incomplete/files/{base64FileName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(204)] - public Task DeleteIncompleteFileAsync([FromRoute] string base64filename) - => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64filename); + public Task DeleteIncompleteFileAsync([FromRoute] string base64FileName) + => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64FileName); private async Task ListDirectoryAsync(string rootDirectory, string base64SubdirectoryName = null, bool recursive = false) { @@ -244,14 +244,14 @@ namespace slskd.Files.API } } - private async Task DeleteFileAsync(string rootDirectory, string base64Filename) + private async Task DeleteFileAsync(string rootDirectory, string base64FileName) { if (!OptionsSnapshot.Value.RemoteFileManagement) { return Forbid(); } - var requestedFilename = base64Filename + var requestedFilename = base64FileName .FromBase64() .Replace('\\', Path.DirectorySeparatorChar) .Replace('/', Path.DirectorySeparatorChar); -- cgit v1.2.3 From 8ed1baaa3bccf32490924920b8ff38c5812aa8c3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Jul 2023 17:48:30 -0500 Subject: ensure relative paths are supplied --- src/slskd/Files/API/FilesController.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/slskd/Files/API/FilesController.cs') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 2bb2d051..372e5f5e 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -19,14 +19,12 @@ using Microsoft.Extensions.Options; namespace slskd.Files.API { - using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Security; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - using OneOf; using Serilog; /// @@ -218,7 +216,8 @@ namespace slskd.Files.API var requestedDir = base64SubdirectoryName .FromBase64() .Replace('\\', Path.DirectorySeparatorChar) - .Replace('/', Path.DirectorySeparatorChar); + .Replace('/', Path.DirectorySeparatorChar) + .TrimStart(Path.DirectorySeparatorChar); requestedDir = Path.GetFullPath(Path.Combine(rootDirectory, requestedDir)); @@ -254,7 +253,8 @@ namespace slskd.Files.API var requestedFilename = base64FileName .FromBase64() .Replace('\\', Path.DirectorySeparatorChar) - .Replace('/', Path.DirectorySeparatorChar); + .Replace('/', Path.DirectorySeparatorChar) + .TrimStart(Path.DirectorySeparatorChar); requestedFilename = Path.GetFullPath(Path.Combine(rootDirectory, requestedFilename)); -- cgit v1.2.3