From 88af15b3bcda58fc82fad6f2db03f1723046bb08 Mon Sep 17 00:00:00 2001 From: Cucumberrbob <128094686+Cucumberrbob@users.noreply.github.com> Date: Tue, 11 Mar 2025 12:13:24 +0000 Subject: [PATCH] [AD] `GetDownloadLinks` -> `GetDownloadInfos` Also makes the tests work wiith this new method --- .../AllDebridTorrentClientTest.cs | 18 ++++--- .../TorrentClients/AllDebridTorrentClient.cs | 48 +++++++------------ 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/server/RdtClient.Service.Test/Services/TorrentClients/AllDebridTorrentClientTest.cs b/server/RdtClient.Service.Test/Services/TorrentClients/AllDebridTorrentClientTest.cs index 29d740f..1ec1f88 100644 --- a/server/RdtClient.Service.Test/Services/TorrentClients/AllDebridTorrentClientTest.cs +++ b/server/RdtClient.Service.Test/Services/TorrentClients/AllDebridTorrentClientTest.cs @@ -405,7 +405,7 @@ public class AllDebridTorrentClientTest } [Fact] - public async Task GetDownloadLinks_WhenTorrentRdIdNull_ReturnsNull() + public async Task GetDownloadInfos_WhenTorrentRdIdNull_ReturnsNull() { // Arrange var mocks = new Mocks(); @@ -418,7 +418,7 @@ public class AllDebridTorrentClientTest var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object); // Act - var result = await allDebridTorrentClient.GetDownloadLinks(torrent); + var result = await allDebridTorrentClient.GetDownloadInfos(torrent); // Assert Assert.Null(result); @@ -426,7 +426,7 @@ public class AllDebridTorrentClientTest } [Fact] - public async Task GetDownloadLinks_UsesFileFilter() + public async Task GetDownloadInfos_UsesFileFilter() { // Arrange var mocks = new Mocks(); @@ -467,16 +467,16 @@ public class AllDebridTorrentClientTest var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object); // Act - var result = await allDebridTorrentClient.GetDownloadLinks(torrent); + var result = await allDebridTorrentClient.GetDownloadInfos(torrent); // Assert Assert.NotNull(result); Assert.Single(result); - Assert.Contains("https://fake.url/file1.txt", result); + Assert.Single(result.Where(info => info.RestrictedLink == "https://fake.url/file1.txt")); } [Fact] - public async Task GetDownloadLinks_WhenAllFilesExcluded_ReturnsAllFiles() + public async Task GetDownloadInfos_WhenAllFilesExcluded_ReturnsEmptyList() { // Arrange var mocks = new Mocks(); @@ -502,19 +502,17 @@ public class AllDebridTorrentClientTest } ]; - var expectedLinksSet = new HashSet(files.Select(n => n.DownloadLink)!); mocks.AllDebridClientMock.Setup(c => c.Magnet.FilesAsync(Int64.Parse(torrent.RdId), It.IsAny())).ReturnsAsync(files); mocks.FileFilterMock.Setup(f => f.IsDownloadable(torrent, It.IsAny(), It.IsAny())).Returns(false); var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object); // Act - var result = await allDebridTorrentClient.GetDownloadLinks(torrent); + var result = await allDebridTorrentClient.GetDownloadInfos(torrent); // Assert Assert.NotNull(result); - var linksSet = new HashSet(result); - Assert.Equal(expectedLinksSet, linksSet); + Assert.Empty(result); mocks.FileFilterMock.Verify(f => f.IsDownloadable(torrent, "file-1.txt", 100)); mocks.FileFilterMock.Verify(f => f.IsDownloadable(torrent, "file-2.txt", 100)); diff --git a/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs index b1415cc..da4998b 100644 --- a/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs @@ -1,10 +1,10 @@ -using AllDebridNET; +using System.Diagnostics; +using AllDebridNET; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using RdtClient.Data.Enums; using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.Helpers; -using System.Web; using RdtClient.Data.Models.Data; using File = AllDebridNET.File; using Torrent = RdtClient.Data.Models.Data.Torrent; @@ -245,48 +245,32 @@ public class AllDebridTorrentClient(ILogger logger, IAll return torrent; } - public async Task?> GetDownloadLinks(Torrent torrent) + public async Task?> GetDownloadInfos(Torrent torrent) { if (torrent.RdId == null) { return null; } - + + Log($"Getting download links", torrent); + var allFiles = await allDebridNetClientFactory.GetClient().Magnet.FilesAsync(Int64.Parse(torrent.RdId)); var files = GetFiles(allFiles); - - files = files.Where(f => fileFilter.IsDownloadable(torrent, f.Path, f.Bytes)).ToList(); - - Log($"Getting download links", torrent); - - if (files.Count == 0) + + return files.Where(f => fileFilter.IsDownloadable(torrent, f.Path, f.Bytes) && f.DownloadLink != null).Select(f => new DownloadInfo { - Log($"Filtered all files out! Downloading ALL files instead!", torrent); - - files = GetFiles(allFiles); - } - - Log($"Selecting links:"); - - foreach (var file in files) - { - Log($"{file.Path} ({file.Bytes}b) {file.DownloadLink}"); - } - - return files.Where(m => m.DownloadLink != null).Select(m => m.DownloadLink!.ToString()).ToList(); + FileName = Path.GetFileName(f.Path), + RestrictedLink = f.DownloadLink! + }).ToList(); } - public Task GetFileName(String downloadUrl) + public Task GetFileName(Download download) { - if (String.IsNullOrWhiteSpace(downloadUrl)) - { - return Task.FromResult(""); - } - - var uri = new Uri(downloadUrl); - - return Task.FromResult(HttpUtility.UrlDecode(uri.Segments.Last())); + // FileName is set in GetDownlaadInfos + Debug.Assert(download.FileName != null); + + return Task.FromResult(download.FileName); } private async Task GetInfo(String torrentId)