diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-07-25 08:14:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-25 08:14:29 -0500 |
| commit | 76ea31e3556c25df85a9708e3cbbe6062b0abb44 (patch) | |
| tree | 627b720e05e26aab7b4a3ee86445f01ee6984528 /src/slskd/Files/API | |
| parent | 50bf39a16261ac4a0ba9f70501c183867d382712 (diff) | |
| parent | f3b4f3fc6328c5b56820b93a92d83680e0498833 (diff) | |
| download | slskd-76ea31e3556c25df85a9708e3cbbe6062b0abb44.tar.gz | |
Merge pull request #941 from slskd/view-files
Add the ability to view and delete downloaded and incomplete files to the UI
Diffstat (limited to 'src/slskd/Files/API')
| -rw-r--r-- | src/slskd/Files/API/FilesController.cs | 304 |
1 files changed, 151 insertions, 153 deletions
diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 08895639..372e5f5e 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -19,13 +19,13 @@ 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; /// <summary> /// Files. @@ -47,163 +47,145 @@ namespace slskd.Files.API private IFileService Files { get; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } + private ILogger Log { get; set; } = Serilog.Log.ForContext<FilesController>(); + /// <summary> + /// Lists the contents of the downloads directory. + /// </summary> + /// <param name="recursive">An optional value indicating whether to recursively list subdirectories and files.</param> + /// <returns></returns> + /// <response code="200">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> [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(); - } - } + [ProducesResponseType(typeof(FilesystemDirectory), 200)] + [ProducesResponseType(401)] + public Task<IActionResult> GetDownloadContentsAsync([FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName: null, recursive); + /// <summary> + /// Lists the contents of the specified subdirectory within the downloads directory. + /// </summary> + /// <param name="base64SubdirectoryName">The relative, base 64 encoded, name of the subdirectory to list.</param> + /// <param name="recursive">An optional value indicating whether to recursively list subdirectories and files.</param> + /// <returns></returns> + /// <response code="200">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + /// <response code="403">Access to the specified subdirectory was denied.</response> + /// <response code="404">The specified subdirectory does not exist.</response> [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(); - } - } + [ProducesResponseType(typeof(FilesystemDirectory), 200)] + [ProducesResponseType(401)] + [ProducesResponseType(403)] + [ProducesResponseType(404)] + public Task<IActionResult> GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName, recursive); + /// <summary> + /// Deletes the specified subdirectory within the downloads directory. + /// </summary> + /// <param name="base64SubdirectoryName">The relative, base 64 encoded, name of the subdirectory to delete.</param> + /// <returns></returns> + /// <response code="204">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + /// <response code="403">Access to the specified subdirectory was denied.</response> + /// <response code="404">The specified subdirectory does not exist.</response> [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(); - } - } + [ProducesResponseType(204)] + public Task<IActionResult> DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + => DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64SubdirectoryName); - [HttpDelete("downloads/files/{base64Filename}")] + /// <summary> + /// Deletes the specified file within the downloads directory. + /// </summary> + /// <param name="base64FileName">The relative, base 64 encoded, name of the file to delete.</param> + /// <returns></returns> + /// <response code="204">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + /// <response code="403">Access to the specified subdirectory was denied.</response> + /// <response code="404">The specified subdirectory does not exist.</response> + [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(); - } + [ProducesResponseType(204)] + public Task<IActionResult> DeleteDownloadFileAsync([FromRoute] string base64FileName) + => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Downloads, base64FileName); - var decodedFilename = base64filename.FromBase64(); + /// <summary> + /// Lists the contents of the downloads directory. + /// </summary> + /// <param name="recursive">An optional value indicating whether to recursively list subdirectories and files.</param> + /// <returns></returns> + /// <response code="200">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + [HttpGet("incomplete/directories")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(FilesystemDirectory), 200)] + public Task<IActionResult> GetIncompleteContentsAsync([FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName: null, recursive); - try - { - var results = await Files.DeleteFilesAsync(OptionsSnapshot.Value.Directories.Downloads, base64filename); + /// <summary> + /// Lists the contents of the specified subdirectory within the incomplete directory. + /// </summary> + /// <param name="base64SubdirectoryName">The relative, base 64 encoded, name of the subdirectory to list.</param> + /// <param name="recursive">An optional value indicating whether to recursively list subdirectories and files.</param> + /// <returns></returns> + /// <response code="200">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + /// <response code="403">Access to the specified subdirectory was denied.</response> + /// <response code="404">The specified subdirectory does not exist.</response> + [HttpGet("incomplete/directories/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(FilesystemDirectory), 200)] + public Task<IActionResult> GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false) + => ListDirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName, recursive); - return results[decodedFilename].Match( - success => NoContent(), - failure => throw failure); - } - catch (ForbiddenException) - { - return Forbid(); - } - catch (NotFoundException) - { - return NotFound(); - } - } + /// <summary> + /// Deletes the specified subdirectory within the downloads directory. + /// </summary> + /// <param name="base64SubdirectoryName">The relative, base 64 encoded, name of the subdirectory to delete.</param> + /// <returns></returns> + /// <response code="204">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + /// <response code="403">Access to the specified subdirectory was denied.</response> + /// <response code="404">The specified subdirectory does not exist.</response> + [HttpDelete("incomplete/directories/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(204)] + public Task<IActionResult> DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + => DeleteSubdirectoryAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64SubdirectoryName); - [HttpGet("incomplete/directories")] + /// <summary> + /// Deletes the specified file within the downloads directory. + /// </summary> + /// <param name="base64FileName">The relative, base 64 encoded, name of the file to delete.</param> + /// <returns></returns> + /// <response code="204">The request completed successfully.</response> + /// <response code="401">Authentication failed.</response> + /// <response code="403">Access to the specified subdirectory was denied.</response> + /// <response code="404">The specified subdirectory does not exist.</response> + [HttpDelete("incomplete/files/{base64FileName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable<FilesystemDirectory>), 200)] - public async Task<IActionResult> GetIncompleteContentsAsync([FromQuery] bool recursive = false) + [ProducesResponseType(204)] + public Task<IActionResult> DeleteIncompleteFileAsync([FromRoute] string base64FileName) + => DeleteFileAsync(rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, base64FileName); + + private async Task<IActionResult> ListDirectoryAsync(string rootDirectory, string base64SubdirectoryName = null, bool recursive = false) { - try - { - var response = await Files.ListContentsAsync( - rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, - enumerationOptions: new EnumerationOptions - { - AttributesToSkip = FileAttributes.System, - RecurseSubdirectories = recursive, - }); + var requestedDir = (base64SubdirectoryName ?? string.Empty) + .FromBase64() + .Replace('\\', Path.DirectorySeparatorChar) + .Replace('/', Path.DirectorySeparatorChar); - return Ok(response); - } - catch (ForbiddenException) - { - return Forbid(); - } - catch (NotFoundException) - { - return NotFound(); - } - } + requestedDir = Path.GetFullPath(Path.Combine(rootDirectory, requestedDir)); - [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(); + 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 +194,88 @@ 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<string, OneOf<bool, string>>), 204)] - public async Task<IActionResult> DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + private async Task<IActionResult> 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) + .TrimStart(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<string, OneOf<bool, string>>), 204)] - public async Task<IActionResult> DeleteIncompleteFileAsync([FromRoute] string base64filename) + private async Task<IActionResult> 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) + .TrimStart(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(); } } |
