[AD] GetDownloadLinks -> GetDownloadInfos

Also makes the tests work wiith this new method
This commit is contained in:
Cucumberrbob 2025-03-11 12:13:24 +00:00
parent e776236270
commit 88af15b3bc
No known key found for this signature in database
GPG key ID: 2B935C47401C3614
2 changed files with 24 additions and 42 deletions

View file

@ -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<String>(files.Select(n => n.DownloadLink)!);
mocks.AllDebridClientMock.Setup(c => c.Magnet.FilesAsync(Int64.Parse(torrent.RdId), It.IsAny<CancellationToken>())).ReturnsAsync(files);
mocks.FileFilterMock.Setup(f => f.IsDownloadable(torrent, It.IsAny<String>(), It.IsAny<Int64>())).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<String>(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));

View file

@ -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<AllDebridTorrentClient> logger, IAll
return torrent;
}
public async Task<IList<String>?> GetDownloadLinks(Torrent torrent)
public async Task<IList<DownloadInfo>?> 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<String> GetFileName(String downloadUrl)
public Task<String> 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<TorrentClientTorrent> GetInfo(String torrentId)