1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-18 21:35:27 -04:00

Cleanup settings controllers

This commit is contained in:
Mark McDowall
2026-02-16 13:37:59 -08:00
parent 33fb0a4e88
commit ac1c74105f
7 changed files with 55 additions and 71 deletions
@@ -137,6 +137,7 @@ namespace NzbDrone.Core.Configuration
_cache.Clear();
var allWithDefaults = GetConfigDictionary();
var hasUpdated = false;
foreach (var configValue in configValues)
{
@@ -155,11 +156,15 @@ namespace NzbDrone.Core.Configuration
if (!equal)
{
hasUpdated = true;
SetValue(configValue.Key.FirstCharToUpper(), configValue.Value.ToString());
}
}
_eventAggregator.PublishEvent(new ConfigFileSavedEvent());
if (hasUpdated)
{
_eventAggregator.PublishEvent(new ConfigFileSavedEvent());
}
}
public string BindAddress
@@ -55,6 +55,7 @@ namespace NzbDrone.Core.Configuration
public void SaveConfigDictionary(Dictionary<string, object> configValues)
{
var allWithDefaults = AllWithDefaults();
var hasUpdated = false;
foreach (var configValue in configValues)
{
@@ -68,11 +69,15 @@ namespace NzbDrone.Core.Configuration
if (!equal)
{
hasUpdated = true;
SetValue(configValue.Key, configValue.Value.ToString());
}
}
_eventAggregator.PublishEvent(new ConfigSavedEvent());
if (hasUpdated)
{
_eventAggregator.PublishEvent(new ConfigSavedEvent());
}
}
public bool IsDefined(string key)
@@ -11,16 +11,17 @@ namespace Sonarr.Api.V5.Settings;
[V5ApiController("settings/mediamanagement")]
public class MediaManagementSettingsController : SettingsController<MediaManagementSettingsResource>
{
public MediaManagementSettingsController(IConfigService configService,
PathExistsValidator pathExistsValidator,
FolderChmodValidator folderChmodValidator,
FolderWritableValidator folderWritableValidator,
SeriesPathValidator seriesPathValidator,
StartupFolderValidator startupFolderValidator,
SystemFolderValidator systemFolderValidator,
RootFolderAncestorValidator rootFolderAncestorValidator,
RootFolderValidator rootFolderValidator)
: base(configService)
public MediaManagementSettingsController(IConfigFileProvider configFileProvider,
IConfigService configService,
PathExistsValidator pathExistsValidator,
FolderChmodValidator folderChmodValidator,
FolderWritableValidator folderWritableValidator,
SeriesPathValidator seriesPathValidator,
StartupFolderValidator startupFolderValidator,
SystemFolderValidator systemFolderValidator,
RootFolderAncestorValidator rootFolderAncestorValidator,
RootFolderValidator rootFolderValidator)
: base(configFileProvider, configService)
{
SharedValidator.RuleFor(c => c.RecycleBinCleanupDays).GreaterThanOrEqualTo(0);
SharedValidator.RuleFor(c => c.ChmodFolder).SetValidator(folderChmodValidator).When(c => !string.IsNullOrEmpty(c.ChmodFolder) && (OsInfo.IsLinux || OsInfo.IsOsx));
@@ -62,7 +63,7 @@ public class MediaManagementSettingsController : SettingsController<MediaManagem
});
}
protected override MediaManagementSettingsResource ToResource(IConfigService model)
protected override MediaManagementSettingsResource ToResource(IConfigFileProvider configFile, IConfigService model)
{
return MediaManagementConfigResourceMapper.ToResource(model);
}
@@ -9,10 +9,12 @@ namespace Sonarr.Api.V5.Settings
public abstract class SettingsController<TResource> : RestController<TResource>
where TResource : RestResource, new()
{
protected readonly IConfigService _configService;
private readonly IConfigFileProvider _configFileProvider;
private readonly IConfigService _configService;
protected SettingsController(IConfigService configService)
protected SettingsController(IConfigFileProvider configFileProvider, IConfigService configService)
{
_configFileProvider = configFileProvider;
_configService = configService;
}
@@ -25,7 +27,7 @@ namespace Sonarr.Api.V5.Settings
[Produces("application/json")]
public TResource GetConfig()
{
var resource = ToResource(_configService);
var resource = ToResource(_configFileProvider, _configService);
resource.Id = 1;
return resource;
@@ -33,17 +35,18 @@ namespace Sonarr.Api.V5.Settings
[RestPutById]
[Consumes("application/json")]
public virtual ActionResult<TResource> SaveConfig([FromBody] TResource resource)
public virtual ActionResult<TResource> SaveSettings([FromBody] TResource resource)
{
var dictionary = resource.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
_configFileProvider.SaveConfigDictionary(dictionary);
_configService.SaveConfigDictionary(dictionary);
return Accepted(resource.Id);
}
protected abstract TResource ToResource(IConfigService model);
protected abstract TResource ToResource(IConfigFileProvider configFile, IConfigService model);
}
}
@@ -1,22 +1,16 @@
using System.Reflection;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Languages;
using Sonarr.Http;
using Sonarr.Http.REST.Attributes;
namespace Sonarr.Api.V5.Settings;
[V5ApiController("settings/ui")]
public class UiSettingsController : SettingsController<UiSettingsResource>
{
private readonly IConfigFileProvider _configFileProvider;
public UiSettingsController(IConfigFileProvider configFileProvider, IConfigService configService)
: base(configService)
: base(configFileProvider, configService)
{
_configFileProvider = configFileProvider;
SharedValidator.RuleFor(c => c.UiLanguage).Custom((value, context) =>
{
if (!Language.All.Any(o => o.Id == value))
@@ -30,21 +24,8 @@ public class UiSettingsController : SettingsController<UiSettingsResource>
.WithMessage("The UI Language value cannot be less than 1");
}
[RestPutById]
public override ActionResult<UiSettingsResource> SaveConfig([FromBody] UiSettingsResource resource)
protected override UiSettingsResource ToResource(IConfigFileProvider configFile, IConfigService model)
{
var dictionary = resource.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
_configFileProvider.SaveConfigDictionary(dictionary);
_configService.SaveConfigDictionary(dictionary);
return Accepted(resource.Id);
}
protected override UiSettingsResource ToResource(IConfigService model)
{
return UiSettingsResourceMapper.ToResource(_configFileProvider, model);
return UiSettingsResourceMapper.ToResource(configFile, model);
}
}
@@ -1,50 +1,24 @@
using System.Reflection;
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Update;
using NzbDrone.Core.Validation.Paths;
using Sonarr.Http;
using Sonarr.Http.REST;
namespace Sonarr.Api.V5.Settings;
[V5ApiController("settings/update")]
public class UpdateSettingsController : RestController<UpdateSettingsResource>
public class UpdateSettingsController : SettingsController<UpdateSettingsResource>
{
private readonly IConfigFileProvider _configFileProvider;
public UpdateSettingsController(IConfigFileProvider configFileProvider)
public UpdateSettingsController(IConfigFileProvider configFileProvider, IConfigService configService)
: base(configFileProvider, configService)
{
_configFileProvider = configFileProvider;
SharedValidator.RuleFor(c => c.UpdateScriptPath)
.IsValidPath()
.When(c => c.UpdateMechanism == UpdateMechanism.Script);
}
[HttpGet]
public UpdateSettingsResource GetUpdateSettings()
protected override UpdateSettingsResource ToResource(IConfigFileProvider configFile, IConfigService model)
{
var resource = new UpdateSettingsResource
{
Branch = _configFileProvider.Branch,
UpdateAutomatically = _configFileProvider.UpdateAutomatically,
UpdateMechanism = _configFileProvider.UpdateMechanism,
UpdateScriptPath = _configFileProvider.UpdateScriptPath
};
return resource;
}
[HttpPut]
public ActionResult<UpdateSettingsResource> SaveUpdateSettings([FromBody] UpdateSettingsResource resource)
{
var dictionary = resource.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
_configFileProvider.SaveConfigDictionary(dictionary);
return Accepted(resource);
return UpdateSettingsResourceMapper.ToResource(configFile);
}
}
@@ -1,3 +1,4 @@
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Update;
using Sonarr.Http.REST;
@@ -10,3 +11,17 @@ public class UpdateSettingsResource : RestResource
public UpdateMechanism UpdateMechanism { get; set; }
public string? UpdateScriptPath { get; set; }
}
public static class UpdateSettingsResourceMapper
{
public static UpdateSettingsResource ToResource(IConfigFileProvider config)
{
return new UpdateSettingsResource
{
Branch = config.Branch,
UpdateAutomatically = config.UpdateAutomatically,
UpdateMechanism = config.UpdateMechanism,
UpdateScriptPath = config.UpdateScriptPath
};
}
}