// // ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ // ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ // █__ --█ █__ --█ ◄█ - █ // █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ // ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ // │ Copyright (c) JP Dillingham. // │ // │ 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, version 3. // │ // │ 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/. // │ // │ This program is distributed with Additional Terms pursuant to Section 7 // │ of the AGPLv3. See the LICENSE file in the root directory of this // │ project for the complete terms and conditions. // │ // │ https://slskd.org // │ // ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ // │ SPDX-FileCopyrightText: JP Dillingham // │ SPDX-License-Identifier: AGPL-3.0-only // ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ // namespace slskd.Shares.API { using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Soulseek; /// /// Shares. /// [ApiController] [ApiVersion("0")] [Produces("application/json")] [Consumes("application/json")] [Route("api/v{version:apiVersion}/[controller]")] public class SharesController : ControllerBase { /// /// Initializes a new instance of the class. /// /// public SharesController( IShareService shareService) { Shares = shareService; } private IShareService Shares { get; } /// /// Gets the current list of shares. /// /// The request completed successfully. /// [HttpGet("")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary>), 200)] public IActionResult List() { var response = Shares.Hosts.ToDictionary( keySelector: host => host.Name, elementSelector: host => host.Shares); return Ok(response); } /// /// Gets the share associated with the specified . /// /// The id of the share. /// The request completed successfully. /// The requested share could not be found. /// [HttpGet("{id}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Share), 200)] [ProducesResponseType(404)] public IActionResult Get(string id) { var share = Shares.Hosts.SelectMany(host => host.Shares).FirstOrDefault(share => share.Id == id); if (share == default) { return NotFound(); } return Ok(share); } /// /// Returns a list of all shared directories and files. /// /// /// The request completed successfully. [HttpGet("contents")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] public async Task BrowseAll() { return Ok(await Shares.BrowseAsync()); } /// /// Gets the contents of the share associated with the specified . /// /// The id of the share. /// /// The request completed successfully. /// The requested share could not be found. [HttpGet("{id}/contents")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(404)] public async Task BrowseShare(string id) { var share = Shares.Hosts.SelectMany(host => host.Shares).FirstOrDefault(share => share.Id == id); if (share == default) { return NotFound(); } var contents = await Shares.BrowseAsync(share); return Ok(contents); } /// /// Initiates a scan of the configured shares. /// /// /// The request completed successfully. /// A share scan is already in progress. [HttpPut] [Route("")] [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(409)] public IActionResult RescanSharesAsync() { if (Shares.StateMonitor.CurrentValue.Scanning) { return Conflict("A share scan is already in progress"); } _ = Shares.ScanAsync(); return Ok(); } /// /// Cancels a share scan, if one is running. /// /// /// The request completed successfully. /// A share scan was not in progress. [HttpDelete] [Route("")] [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult CancelShareScan() { if (Shares.TryCancelScan()) { return StatusCode(204); } return NotFound(); } } }