From c546bd0173087fd7b0cd371a6b18df598237e7a0 Mon Sep 17 00:00:00 2001 From: Cucumberrbob <128094686+Cucumberrbob@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:59:56 +0000 Subject: [PATCH] Use `IProcessFactory`, `IFileSystem` in `Service.Torrents#RunTorrentComplete` Also adds dependency injection to `Torrents#RunTorrentComplete` for `DbSettings` (`Settings.Get`) --- server/RdtClient.Data/DiConfig.cs | 2 +- server/RdtClient.Service/DiConfig.cs | 9 ++++- server/RdtClient.Service/Services/Torrents.cs | 36 +++++++++++-------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/server/RdtClient.Data/DiConfig.cs b/server/RdtClient.Data/DiConfig.cs index 1432338..1a1b29e 100644 --- a/server/RdtClient.Data/DiConfig.cs +++ b/server/RdtClient.Data/DiConfig.cs @@ -19,7 +19,7 @@ public static class DiConfig services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); } } \ No newline at end of file diff --git a/server/RdtClient.Service/DiConfig.cs b/server/RdtClient.Service/DiConfig.cs index 42c2437..6035b28 100644 --- a/server/RdtClient.Service/DiConfig.cs +++ b/server/RdtClient.Service/DiConfig.cs @@ -1,4 +1,5 @@ -using System.Net; +using System.IO.Abstractions; +using System.Net; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using Polly; @@ -7,6 +8,7 @@ using RdtClient.Service.BackgroundServices; using RdtClient.Service.Middleware; using RdtClient.Service.Services; using RdtClient.Service.Services.TorrentClients; +using RdtClient.Service.Wrappers; namespace RdtClient.Service; @@ -18,7 +20,12 @@ public static class DiConfig { services.AddSingleton(); services.AddScoped(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index 0edb19f..146c185 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -1,25 +1,29 @@ -using System.Diagnostics; -using System.Globalization; +using System.Globalization; +using System.IO.Abstractions; using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.Extensions.Logging; using MonoTorrent; -using System.Text.Json.Serialization; using RdtClient.Data.Data; using RdtClient.Data.Enums; +using RdtClient.Data.Models.Data; using RdtClient.Data.Models.Internal; using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.BackgroundServices; using RdtClient.Service.Helpers; using RdtClient.Service.Services.TorrentClients; +using RdtClient.Service.Wrappers; using Torrent = RdtClient.Data.Models.Data.Torrent; namespace RdtClient.Service.Services; public class Torrents( ILogger logger, - TorrentData torrentData, - Downloads downloads, + ITorrentData torrentData, + IDownloads downloads, + IProcessFactory processFactory, + IFileSystem fileSystem, AllDebridTorrentClient allDebridTorrentClient, PremiumizeTorrentClient premiumizeTorrentClient, RealDebridTorrentClient realDebridTorrentClient, @@ -691,9 +695,11 @@ public class Torrents( await torrentData.Update(torrent); } - public async Task RunTorrentComplete(Guid torrentId) + public async Task RunTorrentComplete(Guid torrentId, DbSettings? settings = null) { - if (String.IsNullOrWhiteSpace(Settings.Get.General.RunOnTorrentCompleteFileName)) + settings ??= Settings.Get; + + if (String.IsNullOrWhiteSpace(settings.General.RunOnTorrentCompleteFileName)) { return; } @@ -702,8 +708,8 @@ public class Torrents( var downloadsForTorrent = await downloads.GetForTorrent(torrentId); - var fileName = Settings.Get.General.RunOnTorrentCompleteFileName; - var arguments = Settings.Get.General.RunOnTorrentCompleteArguments ?? ""; + var fileName = settings.General.RunOnTorrentCompleteFileName; + var arguments = settings.General.RunOnTorrentCompleteArguments ?? ""; Log($"Parsing external program {fileName} with arguments {arguments}", torrent); @@ -712,7 +718,7 @@ public class Torrents( var filePath = torrentPath; - var files = Directory.GetFiles(filePath); + var files = fileSystem.Directory.GetFiles(filePath); if (files.Length == 1) { @@ -733,7 +739,7 @@ public class Torrents( var errorSb = new StringBuilder(); var outputSb = new StringBuilder(); - using var process = new Process(); + using var process = processFactory.NewProcess(); process.StartInfo.FileName = fileName; process.StartInfo.Arguments = arguments; @@ -744,21 +750,21 @@ public class Torrents( process.OutputDataReceived += (_, data) => { - if (data.Data == null) + if (data == null) { return; } - outputSb.AppendLine(data.Data.Trim()); + outputSb.AppendLine(data.Trim()); }; process.ErrorDataReceived += (_, data) => { - if (data.Data == null) + if (data == null) { return; } - errorSb.AppendLine(data.Data.Trim()); + errorSb.AppendLine(data.Trim()); }; process.Start();