diff --git a/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs index dd3f424..6e9e178 100644 --- a/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs @@ -1,4 +1,5 @@ -using AllDebridNET; +using System.Text.RegularExpressions; +using AllDebridNET; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using RdtClient.Data.Enums; @@ -6,6 +7,7 @@ using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.Helpers; using System.Web; using File = AllDebridNET.File; +using LogLevel = Microsoft.Extensions.Logging.LogLevel; using Torrent = RdtClient.Data.Models.Data.Torrent; namespace RdtClient.Service.Services.TorrentClients; @@ -62,7 +64,7 @@ public class AllDebridTorrentClient(ILogger logger, IHtt Added = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.UploadDate), Files = torrent.Links.Select((m, i) => new TorrentClientFile { - Path = m.Filename, + Path = GetFiles(m.Files), Bytes = m.Size, Id = i, Selected = true, @@ -242,6 +244,53 @@ public class AllDebridTorrentClient(ILogger logger, IHtt Log($"Found {links.Count} files that match the minimum file size criterea", torrent); } + + if (!String.IsNullOrWhiteSpace(torrent.IncludeRegex)) + { + Log($"Using regular expression {torrent.IncludeRegex} to include only files matching this regex", torrent); + + var newLinks = new List(); + foreach (var link in links) + { + var path = GetFiles(link.Files); + if (Regex.IsMatch(path, torrent.IncludeRegex)) + { + Log($"* Including {path}", torrent); + newLinks.Add(link); + } + else + { + Log($"* Excluding {path}", torrent); + } + } + + links = newLinks; + + Log($"Found {newLinks.Count} files that match the regex", torrent); + } + else if (!String.IsNullOrWhiteSpace(torrent.ExcludeRegex)) + { + Log($"Using regular expression {torrent.IncludeRegex} to ignore files matching this regex", torrent); + + var newLinks = new List(); + foreach (var link in links) + { + var path = GetFiles(link.Files); + if (!Regex.IsMatch(path, torrent.ExcludeRegex)) + { + Log($"* Including {path}", torrent); + newLinks.Add(link); + } + else + { + Log($"* Excluding {path}", torrent); + } + } + + links = newLinks; + + Log($"Found {newLinks.Count} files that match the regex", torrent); + } if (links.Count == 0) { @@ -250,15 +299,16 @@ public class AllDebridTorrentClient(ILogger logger, IHtt links = magnet.Links; } - Log($"Selecting links:"); - - foreach (var link in links) + if (logger.IsEnabled(LogLevel.Debug)) { - var fileList = GetFiles(link.Files, ""); + Log($"Selecting links:"); - Log($"{link.Filename} ({link.Size}b) {link.LinkUrl}, contains files:{Environment.NewLine}{String.Join(Environment.NewLine, fileList)}"); + foreach (var link in links) + { + Log($"{GetFiles(link.Files)} ({link.Size}b) {link.LinkUrl}"); + } } - + Log("", torrent); return links.Select(m => m.LinkUrl.ToString()).ToList(); @@ -282,7 +332,7 @@ public class AllDebridTorrentClient(ILogger logger, IHtt return Map(result); } - private static List GetFiles(IList files, String parent) + private static String GetFiles(IList files) { var result = new List(); @@ -290,19 +340,24 @@ public class AllDebridTorrentClient(ILogger logger, IHtt { if (!String.IsNullOrWhiteSpace(file.N)) { - result.Add($"{parent}/{file.N}"); + result.Add(file.N); } if (file.E != null && file.E.Value.PurpleEArray != null && file.E.Value.PurpleEArray.Count > 0) { - result.AddRange(GetFiles(file.E.Value.PurpleEArray, file.N)); + if (file.E.Value.PurpleEArray.Count != 1) + { + throw new("Unexpected number of nested files"); + } + + result.AddRange(GetFiles(file.E.Value.PurpleEArray)); } } - return result; + return String.Join("/", result); } - private static List GetFiles(IList files, String parent) + private static List GetFiles(IList files) { var result = new List(); @@ -310,19 +365,19 @@ public class AllDebridTorrentClient(ILogger logger, IHtt { if (!String.IsNullOrWhiteSpace(file.N)) { - result.Add($"{parent}/{file.N}"); + result.Add(file.N); } if (file.E != null && file.E.Count > 0) { - result.AddRange(GetFiles(file.E, file.N)); + result.AddRange(GetFiles(file.E)); } } return result; } - private static List GetFiles(IList files, String parent) + private static List GetFiles(IList files) { var result = new List(); @@ -330,7 +385,7 @@ public class AllDebridTorrentClient(ILogger logger, IHtt { if (!String.IsNullOrWhiteSpace(file.N)) { - result.Add($"{parent}/{file.N}"); + result.Add(file.N); } }