using Cleanuparr.Domain.Enums; using Cleanuparr.Persistence.Models.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using DelugeService = Cleanuparr.Infrastructure.Features.DownloadClient.Deluge.DelugeService; using QBitService = Cleanuparr.Infrastructure.Features.DownloadClient.QBittorrent.QBitService; using RTorrentService = Cleanuparr.Infrastructure.Features.DownloadClient.RTorrent.RTorrentService; using TransmissionService = Cleanuparr.Infrastructure.Features.DownloadClient.Transmission.TransmissionService; using UTorrentService = Cleanuparr.Infrastructure.Features.DownloadClient.UTorrent.UTorrentService; namespace Cleanuparr.Infrastructure.Features.DownloadClient; /// /// Factory responsible for creating download client service instances /// public sealed class DownloadServiceFactory : IDownloadServiceFactory { private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; public DownloadServiceFactory( ILogger logger, IServiceProvider serviceProvider ) { _serviceProvider = serviceProvider; _logger = logger; } /// /// Creates a download service using the specified client configuration /// /// The client configuration to use /// An implementation of IDownloadService public IDownloadService GetDownloadService(DownloadClientConfig downloadClientConfig) { if (!downloadClientConfig.Enabled) { _logger.LogWarning("Download client {clientId} is disabled, but a service was requested", downloadClientConfig.Id); } return downloadClientConfig.TypeName switch { DownloadClientTypeName.qBittorrent => ActivatorUtilities.CreateInstance(_serviceProvider, downloadClientConfig), DownloadClientTypeName.Deluge => ActivatorUtilities.CreateInstance(_serviceProvider, downloadClientConfig), DownloadClientTypeName.Transmission => ActivatorUtilities.CreateInstance(_serviceProvider, downloadClientConfig), DownloadClientTypeName.uTorrent => ActivatorUtilities.CreateInstance(_serviceProvider, downloadClientConfig), DownloadClientTypeName.rTorrent => ActivatorUtilities.CreateInstance(_serviceProvider, downloadClientConfig), _ => throw new NotSupportedException($"Download client type {downloadClientConfig.TypeName} is not supported") }; } }