aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Integrations
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-24 14:39:40 -0500
committerGitHub <noreply@github.com>2021-07-24 14:39:40 -0500
commit58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd (patch)
tree64072f5eaa896335cc04de8ffe84fa79a50a2be7 /src/slskd/Integrations
parentfc7800409dfbe0cf26a72dd1de17cbae23d0cb35 (diff)
parent78b3b953d9c36c2cc2939ad00881b0cbf388865a (diff)
downloadslskd-58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd.tar.gz
Merge pull request #125 from slskd/options
Refactor configuration components to allow for run-time updates
Diffstat (limited to 'src/slskd/Integrations')
-rw-r--r--src/slskd/Integrations/FTP/FTPClientFactory.cs38
-rw-r--r--src/slskd/Integrations/FTP/FTPService.cs10
-rw-r--r--src/slskd/Integrations/Pushbullet/IPushbulletService.cs3
-rw-r--r--src/slskd/Integrations/Pushbullet/PushbulletService.cs40
4 files changed, 64 insertions, 27 deletions
diff --git a/src/slskd/Integrations/FTP/FTPClientFactory.cs b/src/slskd/Integrations/FTP/FTPClientFactory.cs
index cc076ca1..e9ee0721 100644
--- a/src/slskd/Integrations/FTP/FTPClientFactory.cs
+++ b/src/slskd/Integrations/FTP/FTPClientFactory.cs
@@ -15,6 +15,8 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+using Microsoft.Extensions.Options;
+
namespace slskd.Integrations.FTP
{
using System;
@@ -30,25 +32,13 @@ namespace slskd.Integrations.FTP
/// Initializes a new instance of the <see cref="FTPClientFactory"/> class.
/// </summary>
/// <param name="optionsMonitor">The options monitor used to derive application options.</param>
- public FTPClientFactory(Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor)
+ public FTPClientFactory(IOptionsMonitor<Options> optionsMonitor)
{
- Options = optionsMonitor.CurrentValue;
-
- try
- {
- EncryptionMode = (FtpEncryptionMode)Enum.Parse(typeof(FtpEncryptionMode), FTPOptions.EncryptionMode, ignoreCase: true);
- }
- catch (Exception ex)
- {
- // Options should validate that the given string is parsable to FtpEncryptionMode through EnumAttribute; if this
- // throws there's a bug somewhere.
- throw new ArgumentException($"Failed to parse {typeof(FtpEncryptionMode).Name} from application Options. This is most likely a programming error; please file a GitHub issue and include your FTP configuration.", ex);
- }
+ OptionsMonitor = optionsMonitor;
}
- private FtpEncryptionMode EncryptionMode { get; set; }
- private FTPOptions FTPOptions => Options.Integration.FTP;
- private Options Options { get; set; }
+ private FTPOptions FTPOptions => OptionsMonitor.CurrentValue.Integration.FTP;
+ private IOptionsMonitor<Options> OptionsMonitor { get; set; }
/// <summary>
/// Creates an instance of <see cref="FtpClient"/>.
@@ -57,10 +47,24 @@ namespace slskd.Integrations.FTP
public FtpClient CreateFtpClient()
{
var client = new FtpClient(FTPOptions.Address, FTPOptions.Port, FTPOptions.Username, FTPOptions.Password);
- client.EncryptionMode = EncryptionMode;
+ client.EncryptionMode = ParseFtpEncryptionMode(FTPOptions.EncryptionMode);
client.ValidateAnyCertificate = FTPOptions.IgnoreCertificateErrors;
return client;
}
+
+ private FtpEncryptionMode ParseFtpEncryptionMode(string encryptionMode)
+ {
+ try
+ {
+ return (FtpEncryptionMode)Enum.Parse(typeof(FtpEncryptionMode), encryptionMode, ignoreCase: true);
+ }
+ catch (Exception ex)
+ {
+ // Options should validate that the given string is parsable to FtpEncryptionMode through EnumAttribute; if this
+ // throws there's a bug somewhere.
+ throw new ArgumentException($"Failed to parse {typeof(FtpEncryptionMode).Name} from application Options. This is most likely a programming error; please file a GitHub issue and include your FTP configuration.", ex);
+ }
+ }
}
} \ No newline at end of file
diff --git a/src/slskd/Integrations/FTP/FTPService.cs b/src/slskd/Integrations/FTP/FTPService.cs
index a5fe6a8f..becf1a12 100644
--- a/src/slskd/Integrations/FTP/FTPService.cs
+++ b/src/slskd/Integrations/FTP/FTPService.cs
@@ -15,6 +15,8 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+using Microsoft.Extensions.Options;
+
namespace slskd.Integrations.FTP
{
using System;
@@ -38,18 +40,18 @@ namespace slskd.Integrations.FTP
/// <param name="log">The logger.</param>
public FTPService(
IFTPClientFactory ftpClientFactory,
- Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor,
+ IOptionsMonitor<Options> optionsMonitor,
ILogger<FTPService> log)
{
Factory = ftpClientFactory;
- Options = optionsMonitor.CurrentValue;
+ OptionsMonitor = optionsMonitor;
Log = log;
}
private IFTPClientFactory Factory { get; set; }
- private FTPOptions FTPOptions => Options.Integration.FTP;
+ private FTPOptions FTPOptions => OptionsMonitor.CurrentValue.Integration.FTP;
private ILogger<FTPService> Log { get; set; }
- private Options Options { get; }
+ private IOptionsMonitor<Options> OptionsMonitor { get; }
/// <summary>
/// Uploads the specified <paramref name="filename"/> to the configured FTP server.
diff --git a/src/slskd/Integrations/Pushbullet/IPushbulletService.cs b/src/slskd/Integrations/Pushbullet/IPushbulletService.cs
index 6da42326..bad33903 100644
--- a/src/slskd/Integrations/Pushbullet/IPushbulletService.cs
+++ b/src/slskd/Integrations/Pushbullet/IPushbulletService.cs
@@ -17,12 +17,13 @@
namespace slskd.Integrations.Pushbullet
{
+ using System;
using System.Threading.Tasks;
/// <summary>
/// Pushbullet integration service.
/// </summary>
- public interface IPushbulletService
+ public interface IPushbulletService : IDisposable
{
/// <summary>
/// Sends a push notification to Pushbullet.
diff --git a/src/slskd/Integrations/Pushbullet/PushbulletService.cs b/src/slskd/Integrations/Pushbullet/PushbulletService.cs
index cb31e6dc..1a224321 100644
--- a/src/slskd/Integrations/Pushbullet/PushbulletService.cs
+++ b/src/slskd/Integrations/Pushbullet/PushbulletService.cs
@@ -15,6 +15,8 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+using Microsoft.Extensions.Options;
+
namespace slskd.Integrations.Pushbullet
{
using System;
@@ -41,23 +43,33 @@ namespace slskd.Integrations.Pushbullet
/// <param name="log">The logger.</param>
public PushbulletService(
IHttpClientFactory httpClientFactory,
- Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor,
+ IOptionsMonitor<Options> optionsMonitor,
ILogger<PushbulletService> log)
{
HttpClientFactory = httpClientFactory;
- Options = optionsMonitor.CurrentValue;
+ OptionsMonitor = optionsMonitor;
Log = log;
RecentlySent = new MemoryCache(new MemoryCacheOptions());
}
+ private bool Disposed { get; set; }
private IHttpClientFactory HttpClientFactory { get; }
- private Options Options { get; }
private ILogger<PushbulletService> Log { get; }
- private PushbulletOptions PushbulletOptions => Options.Integration.Pushbullet;
+ private IOptionsMonitor<Options> OptionsMonitor { get; }
+ private PushbulletOptions PushbulletOptions => OptionsMonitor.CurrentValue.Integration.Pushbullet;
private IMemoryCache RecentlySent { get; }
/// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(disposing: true);
+ GC.SuppressFinalize(this);
+ }
+
+ /// <summary>
/// Sends a push notification to Pushbullet.
/// </summary>
/// <param name="title">The notification title.</param>
@@ -66,6 +78,11 @@ namespace slskd.Integrations.Pushbullet
/// <returns>The operation context.</returns>
public Task PushAsync(string title, string cacheKey, string body)
{
+ if (!PushbulletOptions.Enabled)
+ {
+ return Task.CompletedTask;
+ }
+
if (string.IsNullOrWhiteSpace(title))
{
throw new ArgumentException("A notification title must be supplied", nameof(title));
@@ -91,6 +108,19 @@ namespace slskd.Integrations.Pushbullet
return PushInternalAsync(title, body);
}
+ protected virtual void Dispose(bool disposing)
+ {
+ if (!Disposed)
+ {
+ if (disposing)
+ {
+ RecentlySent.Dispose();
+ }
+
+ Disposed = true;
+ }
+ }
+
private async Task PushInternalAsync(string title, string body)
{
try
@@ -133,4 +163,4 @@ namespace slskd.Integrations.Pushbullet
}
}
}
-}
+} \ No newline at end of file