diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-06-21 08:54:12 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-06-21 08:54:12 -0500 |
| commit | a2ed01bf2610a8f615ef725322496cf910c39fe9 (patch) | |
| tree | 22af17856e1d3329e03e51d2ca13c5a1c9a9fb5e /src/slskd/Files/API | |
| parent | 05bc0e09d54ff4a9e3924668c312610c221bd2b6 (diff) | |
| download | slskd-a2ed01bf2610a8f615ef725322496cf910c39fe9.tar.gz | |
add initial endpoints
Diffstat (limited to 'src/slskd/Files/API')
| -rw-r--r-- | src/slskd/Files/API/FilesController.cs | 154 |
1 files changed, 154 insertions, 0 deletions
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 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.IO; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + + /// <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")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(IEnumerable<FileSystemInfo>), 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<FileSystemInfo>), 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<FileSystemInfo>), 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<FileSystemInfo>), 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(); + } + } + } +} |
