This commit is contained in:
Flaminel
2025-05-18 03:39:59 +03:00
parent 2bd8f69aff
commit c675924be7
33 changed files with 748 additions and 1280 deletions

View File

@@ -1,306 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using Infrastructure.Configuration;
namespace Executable.Controllers;
[ApiController]
[Route("api/config-files")]
public class ConfigFilesController : ControllerBase
{
private readonly ILogger<ConfigFilesController> _logger;
private readonly IConfigManager _configManager;
public ConfigFilesController(
ILogger<ConfigFilesController> logger,
IConfigManager configManager)
{
_logger = logger;
_configManager = configManager;
}
/// <summary>
/// Lists all available configuration files
/// </summary>
[HttpGet]
public IActionResult GetAllConfigFiles()
{
try
{
var files = _configManager.ListConfigurationFiles().ToList();
return Ok(new { Files = files, Count = files.Count });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error listing configuration files");
return StatusCode(500, "An error occurred while listing configuration files");
}
}
/// <summary>
/// Gets the content of a specific configuration file
/// </summary>
[HttpGet("{fileName}")]
public async Task<IActionResult> GetConfigFile(string fileName)
{
if (string.IsNullOrEmpty(fileName) || !fileName.EndsWith(".json"))
{
fileName = $"{fileName}.json";
}
try
{
// Read as dynamic to support any JSON structure
var config = await _configManager.GetConfigurationAsync<object>(fileName);
if (config == null)
{
return NotFound($"Configuration file '{fileName}' not found");
}
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error reading configuration file {fileName}", fileName);
return StatusCode(500, $"An error occurred while reading configuration file '{fileName}'");
}
}
/// <summary>
/// Creates or updates a configuration file
/// </summary>
[HttpPut("{fileName}")]
public async Task<IActionResult> SaveConfigFile(string fileName, [FromBody] JsonElement content)
{
if (string.IsNullOrEmpty(fileName) || !fileName.EndsWith(".json"))
{
fileName = $"{fileName}.json";
}
try
{
// Convert the JsonElement to an object
var configObject = JsonSerializer.Deserialize<object>(content.GetRawText());
if (configObject == null)
{
return BadRequest("Invalid JSON content");
}
var result = await _configManager.SaveConfigurationAsync(fileName, configObject);
if (!result)
{
return StatusCode(500, $"Failed to save configuration file '{fileName}'");
}
return Ok(new { Message = $"Configuration file '{fileName}' saved successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error saving configuration file {fileName}", fileName);
return StatusCode(500, $"An error occurred while saving configuration file '{fileName}'");
}
}
/// <summary>
/// Deletes a configuration file
/// </summary>
[HttpDelete("{fileName}")]
public async Task<IActionResult> DeleteConfigFile(string fileName)
{
if (string.IsNullOrEmpty(fileName) || !fileName.EndsWith(".json"))
{
fileName = $"{fileName}.json";
}
try
{
var result = await _configManager.DeleteConfigurationAsync(fileName);
if (!result)
{
return StatusCode(500, $"Failed to delete configuration file '{fileName}'");
}
return Ok(new { Message = $"Configuration file '{fileName}' deleted successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting configuration file {fileName}", fileName);
return StatusCode(500, $"An error occurred while deleting configuration file '{fileName}'");
}
}
/// <summary>
/// Updates a specific property in a configuration file
/// </summary>
[HttpPatch("{fileName}")]
public async Task<IActionResult> UpdateConfigProperty(
string fileName,
[FromQuery] string propertyPath,
[FromBody] JsonElement value)
{
if (string.IsNullOrEmpty(fileName) || !fileName.EndsWith(".json"))
{
fileName = $"{fileName}.json";
}
if (string.IsNullOrEmpty(propertyPath))
{
return BadRequest("Property path is required");
}
try
{
// Convert the JsonElement to an object
var valueObject = JsonSerializer.Deserialize<object>(value.GetRawText());
if (valueObject == null)
{
return BadRequest("Invalid value");
}
var result = await _configManager.UpdateConfigurationPropertyAsync(fileName, propertyPath, valueObject);
if (!result)
{
return StatusCode(500, $"Failed to update property '{propertyPath}' in configuration file '{fileName}'");
}
return Ok(new { Message = $"Property '{propertyPath}' in configuration file '{fileName}' updated successfully" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating property {propertyPath} in configuration file {fileName}", propertyPath, fileName);
return StatusCode(500, $"An error occurred while updating property '{propertyPath}' in configuration file '{fileName}'");
}
}
/// <summary>
/// Gets the Sonarr configuration
/// </summary>
[HttpGet("sonarr")]
public async Task<IActionResult> GetSonarrConfig()
{
try
{
var config = await _configManager.GetSonarrConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting Sonarr configuration");
return StatusCode(500, "An error occurred while getting Sonarr configuration");
}
}
/// <summary>
/// Gets the Radarr configuration
/// </summary>
[HttpGet("radarr")]
public async Task<IActionResult> GetRadarrConfig()
{
try
{
var config = await _configManager.GetRadarrConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting Radarr configuration");
return StatusCode(500, "An error occurred while getting Radarr configuration");
}
}
/// <summary>
/// Gets the Lidarr configuration
/// </summary>
[HttpGet("lidarr")]
public async Task<IActionResult> GetLidarrConfig()
{
try
{
var config = await _configManager.GetLidarrConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting Lidarr configuration");
return StatusCode(500, "An error occurred while getting Lidarr configuration");
}
}
/// <summary>
/// Gets the ContentBlocker configuration
/// </summary>
[HttpGet("contentblocker")]
public async Task<IActionResult> GetContentBlockerConfig()
{
try
{
var config = await _configManager.GetContentBlockerConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting ContentBlocker configuration");
return StatusCode(500, "An error occurred while getting ContentBlocker configuration");
}
}
/// <summary>
/// Gets the QueueCleaner configuration
/// </summary>
[HttpGet("queuecleaner")]
public async Task<IActionResult> GetQueueCleanerConfig()
{
try
{
var config = await _configManager.GetQueueCleanerConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting QueueCleaner configuration");
return StatusCode(500, "An error occurred while getting QueueCleaner configuration");
}
}
/// <summary>
/// Gets the DownloadCleaner configuration
/// </summary>
[HttpGet("downloadcleaner")]
public async Task<IActionResult> GetDownloadCleanerConfig()
{
try
{
var config = await _configManager.GetDownloadCleanerConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting DownloadCleaner configuration");
return StatusCode(500, "An error occurred while getting DownloadCleaner configuration");
}
}
/// <summary>
/// Gets the DownloadClient configuration
/// </summary>
[HttpGet("downloadclient")]
public async Task<IActionResult> GetDownloadClientConfig()
{
try
{
var config = await _configManager.GetDownloadClientConfigAsync();
return Ok(config);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting DownloadClient configuration");
return StatusCode(500, "An error occurred while getting DownloadClient configuration");
}
}
}

View File

@@ -3,6 +3,7 @@ using Common.Configuration.DownloadCleaner;
using Common.Configuration.DownloadClient;
using Common.Configuration.IgnoredDownloads;
using Common.Configuration.QueueCleaner;
using Infrastructure.Configuration;
using Infrastructure.Services;
using Microsoft.AspNetCore.Mvc;
@@ -13,22 +14,22 @@ namespace Executable.Controllers;
public class ConfigurationController : ControllerBase
{
private readonly ILogger<ConfigurationController> _logger;
private readonly IConfigurationService _configService;
private readonly IConfigManager _configManager;
public ConfigurationController(
ILogger<ConfigurationController> logger,
IConfigurationService configService)
IConfigManager configManager)
{
_logger = logger;
_configService = configService;
_configManager = configManager;
}
[HttpGet("queuecleaner")]
[HttpGet("queue_cleaner")]
public async Task<IActionResult> GetQueueCleanerConfig()
{
try
{
var config = await _configService.GetQueueCleanerConfigAsync();
var config = await _configManager.GetConfigurationAsync<QueueCleanerConfig>();
return Ok(config);
}
catch (Exception ex)
@@ -38,12 +39,12 @@ public class ConfigurationController : ControllerBase
}
}
[HttpGet("contentblocker")]
[HttpGet("content_blocker")]
public async Task<IActionResult> GetContentBlockerConfig()
{
try
{
var config = await _configService.GetContentBlockerConfigAsync();
var config = await _configManager.GetConfigurationAsync<ContentBlockerConfig>();
return Ok(config);
}
catch (Exception ex)
@@ -53,12 +54,12 @@ public class ConfigurationController : ControllerBase
}
}
[HttpGet("downloadcleaner")]
[HttpGet("download_cleaner")]
public async Task<IActionResult> GetDownloadCleanerConfig()
{
try
{
var config = await _configService.GetDownloadCleanerConfigAsync();
var config = await _configManager.GetConfigurationAsync<DownloadCleanerConfig>();
return Ok(config);
}
catch (Exception ex)
@@ -68,12 +69,12 @@ public class ConfigurationController : ControllerBase
}
}
[HttpGet("downloadclient")]
[HttpGet("download_client")]
public async Task<IActionResult> GetDownloadClientConfig()
{
try
{
var config = await _configService.GetDownloadClientConfigAsync();
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
return Ok(config);
}
catch (Exception ex)
@@ -83,12 +84,12 @@ public class ConfigurationController : ControllerBase
}
}
[HttpGet("ignoreddownloads")]
[HttpGet("ignored_downloads")]
public async Task<IActionResult> GetIgnoredDownloadsConfig()
{
try
{
var config = await _configService.GetIgnoredDownloadsConfigAsync();
var config = await _configManager.GetConfigurationAsync<IgnoredDownloadsConfig>();
return Ok(config);
}
catch (Exception ex)
@@ -98,7 +99,7 @@ public class ConfigurationController : ControllerBase
}
}
[HttpPut("queuecleaner")]
[HttpPut("queue_cleaner")]
public async Task<IActionResult> UpdateQueueCleanerConfig([FromBody] QueueCleanerConfig config)
{
try
@@ -107,7 +108,7 @@ public class ConfigurationController : ControllerBase
config.Validate();
// Persist the configuration
var result = await _configService.UpdateQueueCleanerConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, "Failed to save QueueCleaner configuration");
@@ -123,7 +124,7 @@ public class ConfigurationController : ControllerBase
}
}
[HttpPut("contentblocker")]
[HttpPut("content_blocker")]
public async Task<IActionResult> UpdateContentBlockerConfig([FromBody] ContentBlockerConfig config)
{
try
@@ -132,7 +133,7 @@ public class ConfigurationController : ControllerBase
config.Validate();
// Persist the configuration
var result = await _configService.UpdateContentBlockerConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, "Failed to save ContentBlocker configuration");
@@ -148,7 +149,7 @@ public class ConfigurationController : ControllerBase
}
}
[HttpPut("downloadcleaner")]
[HttpPut("download_cleaner")]
public async Task<IActionResult> UpdateDownloadCleanerConfig([FromBody] DownloadCleanerConfig config)
{
try
@@ -157,7 +158,7 @@ public class ConfigurationController : ControllerBase
config.Validate();
// Persist the configuration
var result = await _configService.UpdateDownloadCleanerConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, "Failed to save DownloadCleaner configuration");
@@ -173,7 +174,7 @@ public class ConfigurationController : ControllerBase
}
}
[HttpPut("downloadclient")]
[HttpPut("download_client")]
public async Task<IActionResult> UpdateDownloadClientConfig([FromBody] DownloadClientConfig config)
{
try
@@ -182,7 +183,7 @@ public class ConfigurationController : ControllerBase
config.Validate();
// Persist the configuration
var result = await _configService.UpdateDownloadClientConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, "Failed to save DownloadClient configuration");
@@ -198,13 +199,13 @@ public class ConfigurationController : ControllerBase
}
}
[HttpPut("ignoreddownloads")]
[HttpPut("ignored_downloads")]
public async Task<IActionResult> UpdateIgnoredDownloadsConfig([FromBody] IgnoredDownloadsConfig config)
{
try
{
// Persist the configuration
var result = await _configService.UpdateIgnoredDownloadsConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, "Failed to save IgnoredDownloads configuration");
@@ -219,4 +220,7 @@ public class ConfigurationController : ControllerBase
return StatusCode(500, "An error occurred while updating IgnoredDownloads configuration");
}
}
// TODO add missing configs
// TODO do not return passwords
}

View File

@@ -39,12 +39,9 @@ public class DownloadClientsController : ControllerBase
{
try
{
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
return NotFound(new { Message = "No download client configuration found" });
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
// TODO don't expose passwords
return Ok(config.Clients);
}
catch (Exception ex)
@@ -62,11 +59,7 @@ public class DownloadClientsController : ControllerBase
{
try
{
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
return NotFound(new { Message = "No download client configuration found" });
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
var client = config.GetClientConfig(id);
if (client == null)
@@ -95,14 +88,7 @@ public class DownloadClientsController : ControllerBase
clientConfig.Validate();
// Get the current configuration
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
config = new DownloadClientConfig
{
Clients = new List<ClientConfig>()
};
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
// Check if a client with the same ID already exists
if (config.GetClientConfig(clientConfig.Id) != null)
@@ -114,7 +100,7 @@ public class DownloadClientsController : ControllerBase
config.Clients.Add(clientConfig);
// Persist the updated configuration
var result = await _configManager.SaveDownloadClientConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, new { Error = "Failed to save download client configuration" });
@@ -148,11 +134,7 @@ public class DownloadClientsController : ControllerBase
clientConfig.Validate();
// Get the current configuration
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
return NotFound(new { Message = "No download client configuration found" });
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
// Find the client to update
var existingClientIndex = config.Clients.FindIndex(c => c.Id == id);
@@ -165,7 +147,7 @@ public class DownloadClientsController : ControllerBase
config.Clients[existingClientIndex] = clientConfig;
// Persist the updated configuration
var result = await _configManager.SaveDownloadClientConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, new { Error = "Failed to save download client configuration" });
@@ -190,11 +172,7 @@ public class DownloadClientsController : ControllerBase
try
{
// Get the current configuration
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
return NotFound(new { Message = "No download client configuration found" });
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
// Find the client to delete
var existingClientIndex = config.Clients.FindIndex(c => c.Id == id);
@@ -207,7 +185,7 @@ public class DownloadClientsController : ControllerBase
config.Clients.RemoveAt(existingClientIndex);
// Persist the updated configuration
var result = await _configManager.SaveDownloadClientConfigAsync(config);
var result = await _configManager.SaveConfigurationAsync(config);
if (!result)
{
return StatusCode(500, new { Error = "Failed to save download client configuration" });
@@ -232,11 +210,7 @@ public class DownloadClientsController : ControllerBase
try
{
// Get the client configuration
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
return NotFound(new { Message = "No download client configuration found" });
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
var clientConfig = config.GetClientConfig(id);
if (clientConfig == null)
@@ -277,11 +251,7 @@ public class DownloadClientsController : ControllerBase
{
try
{
var config = await _configManager.GetDownloadClientConfigAsync();
if (config == null)
{
return NotFound(new { Message = "No download client configuration found" });
}
var config = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
var clients = config.Clients.Where(c => c.Type == type).ToList();
return Ok(clients);

View File

@@ -1,5 +1,4 @@
using Infrastructure.Models;
using Infrastructure.Services;
using Infrastructure.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace Executable.Controllers;

View File

@@ -37,23 +37,17 @@ public class StatusController : ControllerBase
var process = Process.GetCurrentProcess();
// Get configuration
var downloadClientConfig = await _configManager.GetDownloadClientConfigAsync();
var sonarrConfig = await _configManager.GetSonarrConfigAsync();
var radarrConfig = await _configManager.GetRadarrConfigAsync();
var lidarrConfig = await _configManager.GetLidarrConfigAsync();
// Default if configs are null
downloadClientConfig ??= new DownloadClientConfig();
sonarrConfig ??= new SonarrConfig();
radarrConfig ??= new RadarrConfig();
lidarrConfig ??= new LidarrConfig();
var downloadClientConfig = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
var sonarrConfig = await _configManager.GetConfigurationAsync<SonarrConfig>();
var radarrConfig = await _configManager.GetConfigurationAsync<RadarrConfig>();
var lidarrConfig = await _configManager.GetConfigurationAsync<LidarrConfig>();
var status = new
{
Application = new
{
Version = GetType().Assembly.GetName().Version?.ToString() ?? "Unknown",
StartTime = process.StartTime,
process.StartTime,
UpTime = DateTime.Now - process.StartTime,
MemoryUsageMB = Math.Round(process.WorkingSet64 / 1024.0 / 1024.0, 2),
ProcessorTime = process.TotalProcessorTime
@@ -96,13 +90,7 @@ public class StatusController : ControllerBase
{
try
{
var downloadClientConfig = await _configManager.GetDownloadClientConfigAsync();
if (downloadClientConfig == null)
{
return NotFound("Download client configuration not found");
}
var downloadClientConfig = await _configManager.GetConfigurationAsync<DownloadClientConfig>();
var result = new Dictionary<string, object>();
// Check for configured clients
@@ -142,12 +130,12 @@ public class StatusController : ControllerBase
var status = new Dictionary<string, object>();
// Get configurations
var sonarrConfig = await _configManager.GetSonarrConfigAsync();
var radarrConfig = await _configManager.GetRadarrConfigAsync();
var lidarrConfig = await _configManager.GetLidarrConfigAsync();
var sonarrConfig = await _configManager.GetConfigurationAsync<SonarrConfig>();
var radarrConfig = await _configManager.GetConfigurationAsync<RadarrConfig>();
var lidarrConfig = await _configManager.GetConfigurationAsync<LidarrConfig>();
// Check Sonarr instances
if (sonarrConfig?.Enabled == true && sonarrConfig.Instances?.Count > 0)
if (sonarrConfig is { Enabled: true, Instances.Count: > 0 })
{
var sonarrStatus = new List<object>();
@@ -160,8 +148,8 @@ public class StatusController : ControllerBase
sonarrStatus.Add(new
{
Name = instance.Name,
Url = instance.Url,
instance.Name,
instance.Url,
IsConnected = true,
Message = "Successfully connected"
});
@@ -170,8 +158,8 @@ public class StatusController : ControllerBase
{
sonarrStatus.Add(new
{
Name = instance.Name,
Url = instance.Url,
instance.Name,
instance.Url,
IsConnected = false,
Message = $"Connection failed: {ex.Message}"
});
@@ -182,7 +170,7 @@ public class StatusController : ControllerBase
}
// Check Radarr instances
if (radarrConfig?.Enabled == true && radarrConfig.Instances?.Count > 0)
if (radarrConfig is { Enabled: true, Instances.Count: > 0 })
{
var radarrStatus = new List<object>();
@@ -195,8 +183,8 @@ public class StatusController : ControllerBase
radarrStatus.Add(new
{
Name = instance.Name,
Url = instance.Url,
instance.Name,
instance.Url,
IsConnected = true,
Message = "Successfully connected"
});
@@ -205,8 +193,8 @@ public class StatusController : ControllerBase
{
radarrStatus.Add(new
{
Name = instance.Name,
Url = instance.Url,
instance.Name,
instance.Url,
IsConnected = false,
Message = $"Connection failed: {ex.Message}"
});
@@ -217,7 +205,7 @@ public class StatusController : ControllerBase
}
// Check Lidarr instances
if (lidarrConfig?.Enabled == true && lidarrConfig.Instances?.Count > 0)
if (lidarrConfig.Enabled == true && lidarrConfig.Instances?.Count > 0)
{
var lidarrStatus = new List<object>();
@@ -230,8 +218,8 @@ public class StatusController : ControllerBase
lidarrStatus.Add(new
{
Name = instance.Name,
Url = instance.Url,
instance.Name,
instance.Url,
IsConnected = true,
Message = "Successfully connected"
});
@@ -240,8 +228,8 @@ public class StatusController : ControllerBase
{
lidarrStatus.Add(new
{
Name = instance.Name,
Url = instance.Url,
instance.Name,
instance.Url,
IsConnected = false,
Message = $"Connection failed: {ex.Message}"
});