Use IProcessFactory, IFileSystem in Service.Torrents#RunTorrentComplete

Also adds dependency injection to `Torrents#RunTorrentComplete` for `DbSettings` (`Settings.Get`)
This commit is contained in:
Cucumberrbob 2025-02-17 21:59:56 +00:00
parent fd073b4762
commit c546bd0173
No known key found for this signature in database
GPG key ID: 2B935C47401C3614
3 changed files with 30 additions and 17 deletions

View file

@ -19,7 +19,7 @@ public static class DiConfig
services.AddScoped<DownloadData>(); services.AddScoped<DownloadData>();
services.AddScoped<SettingData>(); services.AddScoped<SettingData>();
services.AddScoped<TorrentData>(); services.AddScoped<ITorrentData, TorrentData>();
services.AddScoped<UserData>(); services.AddScoped<UserData>();
} }
} }

View file

@ -1,4 +1,5 @@
using System.Net; using System.IO.Abstractions;
using System.Net;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Polly; using Polly;
@ -7,6 +8,7 @@ using RdtClient.Service.BackgroundServices;
using RdtClient.Service.Middleware; using RdtClient.Service.Middleware;
using RdtClient.Service.Services; using RdtClient.Service.Services;
using RdtClient.Service.Services.TorrentClients; using RdtClient.Service.Services.TorrentClients;
using RdtClient.Service.Wrappers;
namespace RdtClient.Service; namespace RdtClient.Service;
@ -18,7 +20,12 @@ public static class DiConfig
{ {
services.AddSingleton<IAllDebridNetClientFactory, AllDebridNetClientFactory>(); services.AddSingleton<IAllDebridNetClientFactory, AllDebridNetClientFactory>();
services.AddScoped<AllDebridTorrentClient>(); services.AddScoped<AllDebridTorrentClient>();
services.AddSingleton<IProcessFactory, ProcessFactory>();
services.AddSingleton<IFileSystem, FileSystem>();
services.AddScoped<Authentication>(); services.AddScoped<Authentication>();
services.AddScoped<IDownloads, Downloads>();
services.AddScoped<Downloads>(); services.AddScoped<Downloads>();
services.AddScoped<PremiumizeTorrentClient>(); services.AddScoped<PremiumizeTorrentClient>();
services.AddScoped<QBittorrent>(); services.AddScoped<QBittorrent>();

View file

@ -1,25 +1,29 @@
using System.Diagnostics; using System.Globalization;
using System.Globalization; using System.IO.Abstractions;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MonoTorrent; using MonoTorrent;
using System.Text.Json.Serialization;
using RdtClient.Data.Data; using RdtClient.Data.Data;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
using RdtClient.Data.Models.Data;
using RdtClient.Data.Models.Internal; using RdtClient.Data.Models.Internal;
using RdtClient.Data.Models.TorrentClient; using RdtClient.Data.Models.TorrentClient;
using RdtClient.Service.BackgroundServices; using RdtClient.Service.BackgroundServices;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using RdtClient.Service.Services.TorrentClients; using RdtClient.Service.Services.TorrentClients;
using RdtClient.Service.Wrappers;
using Torrent = RdtClient.Data.Models.Data.Torrent; using Torrent = RdtClient.Data.Models.Data.Torrent;
namespace RdtClient.Service.Services; namespace RdtClient.Service.Services;
public class Torrents( public class Torrents(
ILogger<Torrents> logger, ILogger<Torrents> logger,
TorrentData torrentData, ITorrentData torrentData,
Downloads downloads, IDownloads downloads,
IProcessFactory processFactory,
IFileSystem fileSystem,
AllDebridTorrentClient allDebridTorrentClient, AllDebridTorrentClient allDebridTorrentClient,
PremiumizeTorrentClient premiumizeTorrentClient, PremiumizeTorrentClient premiumizeTorrentClient,
RealDebridTorrentClient realDebridTorrentClient, RealDebridTorrentClient realDebridTorrentClient,
@ -691,9 +695,11 @@ public class Torrents(
await torrentData.Update(torrent); 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; return;
} }
@ -702,8 +708,8 @@ public class Torrents(
var downloadsForTorrent = await downloads.GetForTorrent(torrentId); var downloadsForTorrent = await downloads.GetForTorrent(torrentId);
var fileName = Settings.Get.General.RunOnTorrentCompleteFileName; var fileName = settings.General.RunOnTorrentCompleteFileName;
var arguments = Settings.Get.General.RunOnTorrentCompleteArguments ?? ""; var arguments = settings.General.RunOnTorrentCompleteArguments ?? "";
Log($"Parsing external program {fileName} with arguments {arguments}", torrent); Log($"Parsing external program {fileName} with arguments {arguments}", torrent);
@ -712,7 +718,7 @@ public class Torrents(
var filePath = torrentPath; var filePath = torrentPath;
var files = Directory.GetFiles(filePath); var files = fileSystem.Directory.GetFiles(filePath);
if (files.Length == 1) if (files.Length == 1)
{ {
@ -733,7 +739,7 @@ public class Torrents(
var errorSb = new StringBuilder(); var errorSb = new StringBuilder();
var outputSb = new StringBuilder(); var outputSb = new StringBuilder();
using var process = new Process(); using var process = processFactory.NewProcess();
process.StartInfo.FileName = fileName; process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = arguments; process.StartInfo.Arguments = arguments;
@ -744,21 +750,21 @@ public class Torrents(
process.OutputDataReceived += (_, data) => process.OutputDataReceived += (_, data) =>
{ {
if (data.Data == null) if (data == null)
{ {
return; return;
} }
outputSb.AppendLine(data.Data.Trim()); outputSb.AppendLine(data.Trim());
}; };
process.ErrorDataReceived += (_, data) => process.ErrorDataReceived += (_, data) =>
{ {
if (data.Data == null) if (data == null)
{ {
return; return;
} }
errorSb.AppendLine(data.Data.Trim()); errorSb.AppendLine(data.Trim());
}; };
process.Start(); process.Start();