diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-06-22 10:00:17 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-06-22 10:00:17 -0500 |
| commit | e2767eb91ed0e87a4e7d0b60b00226707cfefdfb (patch) | |
| tree | 0b4bb6536645464da3175799eefb57561e1c19c8 /src/slskd/Files | |
| parent | c76f272f380833ad01e98f7946a7e114a1635a6c (diff) | |
| download | slskd-e2767eb91ed0e87a4e7d0b60b00226707cfefdfb.tar.gz | |
tweak endpoints
Diffstat (limited to 'src/slskd/Files')
| -rw-r--r-- | src/slskd/Files/API/FilesController.cs | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 6ee2e7ac..f2f7284d 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -20,6 +20,7 @@ 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; @@ -46,22 +47,103 @@ namespace slskd.Files.API private IFileService Files { get; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } + [HttpGet("downloads")] + [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/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable<FilesystemDirectory>), 200)] - public async Task<IActionResult> GetDownloadContentsAsync([FromRoute]string base64SubdirectoryName = null, [FromQuery]bool recursive = false) + public async Task<IActionResult> GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery]bool recursive = false) { - var dir = OptionsSnapshot.Value.Directories.Downloads; + var decodedDir = base64SubdirectoryName.FromBase64(); - if (!string.IsNullOrEmpty(base64SubdirectoryName)) + try { - var decodedDir = base64SubdirectoryName.FromBase64(); - dir = Path.Combine(dir, decodedDir); + 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(); + } + } + + [HttpGet("incomplete")] + [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/{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(dir, new EnumerationOptions + var response = await Files.ListContentsAsync( + rootDirectory: OptionsSnapshot.Value.Directories.Incomplete, + parentDirectory: decodedDir, + enumerationOptions: new EnumerationOptions { AttributesToSkip = FileAttributes.System, RecurseSubdirectories = recursive, @@ -69,7 +151,7 @@ namespace slskd.Files.API return Ok(response); } - catch (InvalidDirectoryException) + catch (ForbiddenException) { return Forbid(); } |
