mirror of
https://github.com/Cleanuparr/Cleanuparr.git
synced 2026-01-16 17:58:22 -05:00
124 lines
3.5 KiB
C#
124 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Common.Enums;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Common.Configuration.DownloadClient;
|
|
|
|
public sealed record DownloadClientConfig : IConfig
|
|
{
|
|
public const string SectionName = "DownloadClient";
|
|
|
|
/// <summary>
|
|
/// Collection of download clients configured for the application
|
|
/// </summary>
|
|
public List<ClientConfig> Clients { get; init; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets a client configuration by id
|
|
/// </summary>
|
|
/// <param name="id">The client id</param>
|
|
/// <returns>The client configuration or null if not found</returns>
|
|
public ClientConfig? GetClientConfig(string id)
|
|
{
|
|
return Clients.FirstOrDefault(c => c.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all enabled clients
|
|
/// </summary>
|
|
/// <returns>Collection of enabled client configurations</returns>
|
|
public IEnumerable<ClientConfig> GetEnabledClients()
|
|
{
|
|
return Clients.Where(c => c.Enabled);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates the configuration to ensure it meets requirements
|
|
/// </summary>
|
|
public void Validate()
|
|
{
|
|
// Validate clients have unique IDs
|
|
var duplicateIds = Clients
|
|
.GroupBy(c => c.Id)
|
|
.Where(g => g.Count() > 1)
|
|
.Select(g => g.Key)
|
|
.ToList();
|
|
|
|
if (duplicateIds.Any())
|
|
{
|
|
throw new InvalidOperationException($"Duplicate client IDs found: {string.Join(", ", duplicateIds)}");
|
|
}
|
|
|
|
// Validate each client configuration
|
|
foreach (var client in Clients)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(client.Id))
|
|
{
|
|
throw new InvalidOperationException("Client ID cannot be empty");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(client.Name))
|
|
{
|
|
throw new InvalidOperationException($"Client name cannot be empty for client ID: {client.Id}");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(client.Host))
|
|
{
|
|
throw new InvalidOperationException($"Host cannot be empty for client ID: {client.Id}");
|
|
}
|
|
|
|
if (client.Port <= 0)
|
|
{
|
|
throw new InvalidOperationException($"Port must be greater than 0 for client ID: {client.Id}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Represents a download client configuration
|
|
/// </summary>
|
|
public class ClientConfig
|
|
{
|
|
/// <summary>
|
|
/// Unique identifier for the client
|
|
/// </summary>
|
|
public string Id { get; init; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Display name of the client
|
|
/// </summary>
|
|
public string Name { get; init; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Host address (IP or hostname)
|
|
/// </summary>
|
|
public string Host { get; init; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Port number
|
|
/// </summary>
|
|
public int Port { get; init; }
|
|
|
|
/// <summary>
|
|
/// Username for authentication (if required)
|
|
/// </summary>
|
|
public string? Username { get; init; }
|
|
|
|
/// <summary>
|
|
/// Password for authentication (if required)
|
|
/// </summary>
|
|
public string? Password { get; init; }
|
|
|
|
/// <summary>
|
|
/// Type of download client
|
|
/// </summary>
|
|
public DownloadClientType Type { get; init; } = DownloadClientType.QBittorrent;
|
|
|
|
/// <summary>
|
|
/// Whether the client is enabled
|
|
/// </summary>
|
|
public bool Enabled { get; init; } = true;
|
|
} |