From 43f3bf2d128dc7e44043a8ef2199d75cdb0d3d4b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 3 Aug 2021 09:25:10 -0500 Subject: add plumbing to rescan shares via API --- .../API/Controllers/ApplicationController.cs | 18 +++-- .../API/Controllers/ConfigurationController.cs | 76 ++++++++++++++++++++++ .../Application/Management/API/DTO/ServerState.cs | 33 ++++++++++ .../Application/Management/Domain/ServerState.cs | 33 ---------- .../Application/Management/IManagementService.cs | 12 ++++ .../Application/Management/ManagementService.cs | 38 +++++++---- 6 files changed, 162 insertions(+), 48 deletions(-) create mode 100644 src/slskd/Application/Management/API/Controllers/ConfigurationController.cs create mode 100644 src/slskd/Application/Management/API/DTO/ServerState.cs delete mode 100644 src/slskd/Application/Management/Domain/ServerState.cs (limited to 'src/slskd/Application/Management') diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs index ddac832a..43029aa5 100644 --- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs @@ -22,7 +22,6 @@ namespace slskd.Management.API using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; - using Soulseek; /// /// Application. @@ -34,15 +33,26 @@ namespace slskd.Management.API [Consumes("application/json")] public class ApplicationController : ControllerBase { - public ApplicationController(ISoulseekClient client, IHostApplicationLifetime lifetime) + public ApplicationController(IManagementService managementService, IHostApplicationLifetime lifetime) { - Client = client; + Management = managementService; Lifetime = lifetime; } - private ISoulseekClient Client { get; } + private IManagementService Management { get; } private IHostApplicationLifetime Lifetime { get; } + /// + /// Gets the current state of the application. + /// + /// + [HttpGet] + [Authorize] + public IActionResult State() + { + return Ok(Management.GetServiceState()); + } + /// /// Stops the application. /// diff --git a/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs b/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs new file mode 100644 index 00000000..26ce6c52 --- /dev/null +++ b/src/slskd/Application/Management/API/Controllers/ConfigurationController.cs @@ -0,0 +1,76 @@ +// +// 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.Application.Management.API +{ + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using slskd.Management; + using slskd.Shares; + + /// + /// Configuration. + /// + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class ConfigurationController : ControllerBase + { + public ConfigurationController( + IOptionsSnapshot optionsShapshot, + IStateMonitor sharedFileCacheMonitor, + IManagementService managementService) + { + OptionsShapshot = optionsShapshot; + SharedFileCacheMonitor = sharedFileCacheMonitor; + ManagementService = managementService; + } + + private IManagementService ManagementService { get; } + private IOptionsSnapshot OptionsShapshot { get; } + private IStateMonitor SharedFileCacheMonitor { get; } + + [HttpGet] + [Authorize] + public IActionResult GetOptions() + { + // todo: sanitize this to remove passwords + return Ok(OptionsShapshot.Value); + } + + [HttpPut] + [Authorize] + public async Task RescanSharesAsync() + { + try + { + await ManagementService.RescanSharesAsync(); + } + catch (ShareScanInProgressException ex) + { + return Conflict(ex.Message); + } + + return Ok(); + } + } +} diff --git a/src/slskd/Application/Management/API/DTO/ServerState.cs b/src/slskd/Application/Management/API/DTO/ServerState.cs new file mode 100644 index 00000000..80c1e856 --- /dev/null +++ b/src/slskd/Application/Management/API/DTO/ServerState.cs @@ -0,0 +1,33 @@ +// +// 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/. +// + +namespace slskd.Management +{ + using System.Net; + using Soulseek; + + public class ServerState + { + public string Address { get; set; } + public IPEndPoint IPEndPoint { get; set; } + public SoulseekClientStates State { get; set; } + public string Username { get; set; } + public bool IsConnected => State.HasFlag(SoulseekClientStates.Connected); + public bool IsLoggedIn => State.HasFlag(SoulseekClientStates.LoggedIn); + public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); + } +} diff --git a/src/slskd/Application/Management/Domain/ServerState.cs b/src/slskd/Application/Management/Domain/ServerState.cs deleted file mode 100644 index 80c1e856..00000000 --- a/src/slskd/Application/Management/Domain/ServerState.cs +++ /dev/null @@ -1,33 +0,0 @@ -// -// 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/. -// - -namespace slskd.Management -{ - using System.Net; - using Soulseek; - - public class ServerState - { - public string Address { get; set; } - public IPEndPoint IPEndPoint { get; set; } - public SoulseekClientStates State { get; set; } - public string Username { get; set; } - public bool IsConnected => State.HasFlag(SoulseekClientStates.Connected); - public bool IsLoggedIn => State.HasFlag(SoulseekClientStates.LoggedIn); - public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); - } -} diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs index b3e13477..6c5f3784 100644 --- a/src/slskd/Application/Management/IManagementService.cs +++ b/src/slskd/Application/Management/IManagementService.cs @@ -43,5 +43,17 @@ namespace slskd.Management /// /// The current server state. ServerState GetServerState(); + + /// + /// Gets the current state of the slskd service. + /// + /// + ServiceState GetServiceState(); + + /// + /// Re-scans shared directories. + /// + /// The operation context. + Task RescanSharesAsync(); } } \ No newline at end of file diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs index 66ed2b97..2f79f496 100644 --- a/src/slskd/Application/Management/ManagementService.cs +++ b/src/slskd/Application/Management/ManagementService.cs @@ -1,18 +1,13 @@ // // 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 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. +// 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/. +// 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; @@ -21,6 +16,7 @@ namespace slskd.Management { using System; using System.Threading.Tasks; + using slskd.Shares; using Soulseek; /// @@ -32,18 +28,26 @@ namespace slskd.Management /// Initializes a new instance of the class. /// /// The options monitor used to derive application options. + /// The state monitor for application service state. /// The Soulseek client. + /// The shared file cache. public ManagementService( IOptionsMonitor optionsMonitor, - ISoulseekClient soulseekClient) + IStateMonitor serviceStateMonitor, + ISoulseekClient soulseekClient, + ISharedFileCache sharedFileCache) { OptionsMonitor = optionsMonitor; + ServiceStateMonitor = serviceStateMonitor; Client = soulseekClient; + SharedFileCache = sharedFileCache; } private ISoulseekClient Client { get; } private Options Options => OptionsMonitor.CurrentValue; private IOptionsMonitor OptionsMonitor { get; } + private IStateMonitor ServiceStateMonitor { get; } + private ISharedFileCache SharedFileCache { get; } /// /// Connects the Soulseek client to the server using the configured username and password. @@ -72,5 +76,17 @@ namespace slskd.Management State = Client.State, Username = Client.Username, }; + + /// + /// Gets the current state of the slskd service. + /// + /// + public ServiceState GetServiceState() => ServiceStateMonitor.CurrentValue; + + /// + /// Re-scans shared directories. + /// + /// The operation context. + public Task RescanSharesAsync() => SharedFileCache.FillAsync(); } } \ No newline at end of file -- cgit v1.2.3