From a2ed01bf2610a8f615ef725322496cf910c39fe9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 21 Jun 2023 08:54:12 -0500 Subject: add initial endpoints --- src/slskd/Files/API/FilesController.cs | 154 +++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/slskd/Files/API/FilesController.cs (limited to 'src/slskd/Files/API') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs new file mode 100644 index 00000000..4a15aa49 --- /dev/null +++ b/src/slskd/Files/API/FilesController.cs @@ -0,0 +1,154 @@ +// +// 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/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Files.API +{ + using System.Collections.Generic; + using System.IO; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + + /// + /// Files. + /// + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class FilesController : ControllerBase + { + public FilesController( + IFileService fileService, + IOptionsSnapshot optionsSnapshot) + { + Files = fileService; + OptionsSnapshot = optionsSnapshot; + } + + private IFileService Files { get; } + private IOptionsSnapshot OptionsSnapshot { get; } + + [HttpGet("downloads")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable), 200)] + public IActionResult GetAllDownloadContentsAsync([FromQuery]bool recursive = false) + { + try + { + var dirs = Files.ListContentsAsync(OptionsSnapshot.Value.Directories.Downloads, new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(dirs); + } + catch (InvalidDirectoryException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpGet("downloads/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable), 200)] + public IActionResult GetSpecificDownloadContentsAsync([FromRoute]string base64SubdirectoryName, [FromQuery]bool recursive = false) + { + var decodedDir = base64SubdirectoryName.FromBase64(); + var fullDir = Path.Combine(OptionsSnapshot.Value.Directories.Downloads, decodedDir); + + try + { + var dirs = Files.ListContentsAsync(fullDir, new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(dirs); + } + catch (InvalidDirectoryException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpGet("incomplete")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable), 200)] + public IActionResult GetAllIncompleteDirectoriesAsync([FromQuery] bool recursive = false) + { + try + { + var dirs = Files.ListContentsAsync(OptionsSnapshot.Value.Directories.Incomplete, new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(dirs); + } + catch (InvalidDirectoryException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + + [HttpGet("incomplete/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable), 200)] + public IActionResult GetSpecificIncompleteDirectoriesAsync([FromRoute] string base64SubdirectoryName, [FromQuery] bool recursive = false) + { + var decodedDir = base64SubdirectoryName.FromBase64(); + var fullDir = Path.Combine(OptionsSnapshot.Value.Directories.Incomplete, decodedDir); + + try + { + var dirs = Files.ListContentsAsync(fullDir, new EnumerationOptions + { + AttributesToSkip = FileAttributes.System, + RecurseSubdirectories = recursive, + }); + + return Ok(dirs); + } + catch (InvalidDirectoryException) + { + return Forbid(); + } + catch (NotFoundException) + { + return NotFound(); + } + } + } +} -- cgit v1.2.3 From efccdff7b30d55fc63e195a8f6d865f19fd1a919 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 21 Jun 2023 10:57:29 -0500 Subject: implement download fetch --- src/slskd/Files/API/FilesController.cs | 90 ++++------------------------------ 1 file changed, 9 insertions(+), 81 deletions(-) (limited to 'src/slskd/Files/API') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 4a15aa49..6ee2e7ac 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -21,6 +21,7 @@ namespace slskd.Files.API { using System.Collections.Generic; using System.IO; + using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -45,101 +46,28 @@ namespace slskd.Files.API private IFileService Files { get; } private IOptionsSnapshot OptionsSnapshot { get; } - [HttpGet("downloads")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] - public IActionResult GetAllDownloadContentsAsync([FromQuery]bool recursive = false) - { - try - { - var dirs = Files.ListContentsAsync(OptionsSnapshot.Value.Directories.Downloads, new EnumerationOptions - { - AttributesToSkip = FileAttributes.System, - RecurseSubdirectories = recursive, - }); - - return Ok(dirs); - } - catch (InvalidDirectoryException) - { - return Forbid(); - } - catch (NotFoundException) - { - return NotFound(); - } - } - [HttpGet("downloads/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] - public IActionResult GetSpecificDownloadContentsAsync([FromRoute]string base64SubdirectoryName, [FromQuery]bool recursive = false) - { - var decodedDir = base64SubdirectoryName.FromBase64(); - var fullDir = Path.Combine(OptionsSnapshot.Value.Directories.Downloads, decodedDir); - - try - { - var dirs = Files.ListContentsAsync(fullDir, new EnumerationOptions - { - AttributesToSkip = FileAttributes.System, - RecurseSubdirectories = recursive, - }); - - return Ok(dirs); - } - catch (InvalidDirectoryException) - { - return Forbid(); - } - catch (NotFoundException) - { - return NotFound(); - } - } - - [HttpGet("incomplete")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] - public IActionResult GetAllIncompleteDirectoriesAsync([FromQuery] bool recursive = false) + [ProducesResponseType(typeof(IEnumerable), 200)] + public async Task GetDownloadContentsAsync([FromRoute]string base64SubdirectoryName = null, [FromQuery]bool recursive = false) { - try - { - var dirs = Files.ListContentsAsync(OptionsSnapshot.Value.Directories.Incomplete, new EnumerationOptions - { - AttributesToSkip = FileAttributes.System, - RecurseSubdirectories = recursive, - }); + var dir = OptionsSnapshot.Value.Directories.Downloads; - return Ok(dirs); - } - catch (InvalidDirectoryException) - { - return Forbid(); - } - catch (NotFoundException) + if (!string.IsNullOrEmpty(base64SubdirectoryName)) { - return NotFound(); + var decodedDir = base64SubdirectoryName.FromBase64(); + dir = Path.Combine(dir, decodedDir); } - } - - [HttpGet("incomplete/{base64SubdirectoryName}")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] - public IActionResult GetSpecificIncompleteDirectoriesAsync([FromRoute] string base64SubdirectoryName, [FromQuery] bool recursive = false) - { - var decodedDir = base64SubdirectoryName.FromBase64(); - var fullDir = Path.Combine(OptionsSnapshot.Value.Directories.Incomplete, decodedDir); try { - var dirs = Files.ListContentsAsync(fullDir, new EnumerationOptions + var response = await Files.ListContentsAsync(dir, new EnumerationOptions { AttributesToSkip = FileAttributes.System, RecurseSubdirectories = recursive, }); - return Ok(dirs); + return Ok(response); } catch (InvalidDirectoryException) { -- cgit v1.2.3 From e2767eb91ed0e87a4e7d0b60b00226707cfefdfb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 22 Jun 2023 10:00:17 -0500 Subject: tweak endpoints --- src/slskd/Files/API/FilesController.cs | 96 +++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) (limited to 'src/slskd/Files/API') 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 OptionsSnapshot { get; } + [HttpGet("downloads")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable), 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(); + } + } + [HttpGet("downloads/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] - public async Task GetDownloadContentsAsync([FromRoute]string base64SubdirectoryName = null, [FromQuery]bool recursive = false) + public async Task 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), 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(); + } + } + + [HttpGet("incomplete/{base64SubdirectoryName}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable), 200)] + public async Task 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(); } -- cgit v1.2.3 From 1017eeee702535bb2c6dd3ea09e5574627a9a7d3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Jun 2023 12:55:43 -0500 Subject: implement controller methods --- src/slskd/Files/API/FilesController.cs | 109 +++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) (limited to 'src/slskd/Files/API') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index f2f7284d..ff8a1707 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -25,6 +25,7 @@ namespace slskd.Files.API using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using OneOf; /// /// Files. @@ -47,7 +48,7 @@ namespace slskd.Files.API private IFileService Files { get; } private IOptionsSnapshot OptionsSnapshot { get; } - [HttpGet("downloads")] + [HttpGet("downloads/directories")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] public async Task GetDownloadContentsAsync([FromQuery] bool recursive = false) @@ -74,7 +75,7 @@ namespace slskd.Files.API } } - [HttpGet("downloads/{base64SubdirectoryName}")] + [HttpGet("downloads/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] public async Task GetDownloadSubdirectoryContentsAsync([FromRoute] string base64SubdirectoryName, [FromQuery]bool recursive = false) @@ -104,7 +105,57 @@ namespace slskd.Files.API } } - [HttpGet("incomplete")] + [HttpDelete("downloads/directories/{base64SubdirectoryName")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>), 204)] + public async Task DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + { + 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>), 204)] + public async Task DeleteDownloadFileAsync([FromRoute] string base64filename) + { + 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), 200)] public async Task GetIncompleteContentsAsync([FromQuery] bool recursive = false) @@ -131,7 +182,7 @@ namespace slskd.Files.API } } - [HttpGet("incomplete/{base64SubdirectoryName}")] + [HttpGet("incomplete/directories/{base64SubdirectoryName}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] public async Task GetIncompleteSubdirectoryContentsAsync([FromRoute, Required] string base64SubdirectoryName, [FromQuery] bool recursive = false) @@ -160,5 +211,55 @@ namespace slskd.Files.API return NotFound(); } } + + [HttpDelete("incomplete/directories/{base64SubdirectoryName")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>), 204)] + public async Task DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) + { + 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>), 204)] + public async Task DeleteIncompleteFileAsync([FromRoute] string base64filename) + { + 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(); + } + } } } -- cgit v1.2.3 From 197865c54b2eb50c43d71b89f4587955969a0924 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jun 2023 09:21:01 -0500 Subject: disallow deletes if option isn't enabled --- src/slskd/Files/API/FilesController.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/slskd/Files/API') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index ff8a1707..d39cffbd 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -110,6 +110,11 @@ namespace slskd.Files.API [ProducesResponseType(typeof(Dictionary>), 204)] public async Task DeleteDownloadSubdirectoryAsync([FromRoute] string base64SubdirectoryName) { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + var decodedDir = base64SubdirectoryName.FromBase64(); try @@ -135,6 +140,11 @@ namespace slskd.Files.API [ProducesResponseType(typeof(Dictionary>), 204)] public async Task DeleteDownloadFileAsync([FromRoute] string base64filename) { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + var decodedFilename = base64filename.FromBase64(); try @@ -217,6 +227,11 @@ namespace slskd.Files.API [ProducesResponseType(typeof(Dictionary>), 204)] public async Task DeleteIncompleteSubdirectoryAsync([FromRoute] string base64SubdirectoryName) { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + var decodedDir = base64SubdirectoryName.FromBase64(); try @@ -242,6 +257,11 @@ namespace slskd.Files.API [ProducesResponseType(typeof(Dictionary>), 204)] public async Task DeleteIncompleteFileAsync([FromRoute] string base64filename) { + if (!OptionsSnapshot.Value.RemoteFileManagement) + { + return Forbid(); + } + var decodedFilename = base64filename.FromBase64(); try -- cgit v1.2.3