[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] [Fact]
public async Task GetDownloadLinks_WhenTorrentRdIdNull_ReturnsNull() public async Task GetDownloadInfos_WhenTorrentRdIdNull_ReturnsNull()
{ {
// Arrange // Arrange
var mocks = new Mocks(); var mocks = new Mocks();
@ -418,7 +418,7 @@ public class AllDebridTorrentClientTest
var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object); var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object);
// Act // Act
var result = await allDebridTorrentClient.GetDownloadLinks(torrent); var result = await allDebridTorrentClient.GetDownloadInfos(torrent);
// Assert // Assert
Assert.Null(result); Assert.Null(result);
@ -426,7 +426,7 @@ public class AllDebridTorrentClientTest
} }
[Fact] [Fact]
public async Task GetDownloadLinks_UsesFileFilter() public async Task GetDownloadInfos_UsesFileFilter()
{ {
// Arrange // Arrange
var mocks = new Mocks(); var mocks = new Mocks();
@ -467,16 +467,16 @@ public class AllDebridTorrentClientTest
var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object); var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object);
// Act // Act
var result = await allDebridTorrentClient.GetDownloadLinks(torrent); var result = await allDebridTorrentClient.GetDownloadInfos(torrent);
// Assert // Assert
Assert.NotNull(result); Assert.NotNull(result);
Assert.Single(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] [Fact]
public async Task GetDownloadLinks_WhenAllFilesExcluded_ReturnsAllFiles() public async Task GetDownloadInfos_WhenAllFilesExcluded_ReturnsEmptyList()
{ {
// Arrange // Arrange
var mocks = new Mocks(); 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.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); 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); var allDebridTorrentClient = new AllDebridTorrentClient(mocks.LoggerMock.Object, mocks.AllDebridClientFactoryMock.Object, mocks.FileFilterMock.Object);
// Act // Act
var result = await allDebridTorrentClient.GetDownloadLinks(torrent); var result = await allDebridTorrentClient.GetDownloadInfos(torrent);
// Assert // Assert
Assert.NotNull(result); Assert.NotNull(result);
var linksSet = new HashSet<String>(result); Assert.Empty(result);
Assert.Equal(expectedLinksSet, linksSet);
mocks.FileFilterMock.Verify(f => f.IsDownloadable(torrent, "file-1.txt", 100)); mocks.FileFilterMock.Verify(f => f.IsDownloadable(torrent, "file-1.txt", 100));
mocks.FileFilterMock.Verify(f => f.IsDownloadable(torrent, "file-2.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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
using RdtClient.Data.Models.TorrentClient; using RdtClient.Data.Models.TorrentClient;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using System.Web;
using RdtClient.Data.Models.Data; using RdtClient.Data.Models.Data;
using File = AllDebridNET.File; using File = AllDebridNET.File;
using Torrent = RdtClient.Data.Models.Data.Torrent; using Torrent = RdtClient.Data.Models.Data.Torrent;
@ -245,48 +245,32 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IAll
return torrent; return torrent;
} }
public async Task<IList<String>?> GetDownloadLinks(Torrent torrent) public async Task<IList<DownloadInfo>?> GetDownloadInfos(Torrent torrent)
{ {
if (torrent.RdId == null) if (torrent.RdId == null)
{ {
return null; return null;
} }
Log($"Getting download links", torrent);
var allFiles = await allDebridNetClientFactory.GetClient().Magnet.FilesAsync(Int64.Parse(torrent.RdId)); var allFiles = await allDebridNetClientFactory.GetClient().Magnet.FilesAsync(Int64.Parse(torrent.RdId));
var files = GetFiles(allFiles); var files = GetFiles(allFiles);
files = files.Where(f => fileFilter.IsDownloadable(torrent, f.Path, f.Bytes)).ToList(); return files.Where(f => fileFilter.IsDownloadable(torrent, f.Path, f.Bytes) && f.DownloadLink != null).Select(f => new DownloadInfo
Log($"Getting download links", torrent);
if (files.Count == 0)
{ {
Log($"Filtered all files out! Downloading ALL files instead!", torrent); FileName = Path.GetFileName(f.Path),
RestrictedLink = f.DownloadLink!
files = GetFiles(allFiles); }).ToList();
}
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();
} }
public Task<String> GetFileName(String downloadUrl) public Task<String> GetFileName(Download download)
{ {
if (String.IsNullOrWhiteSpace(downloadUrl)) // FileName is set in GetDownlaadInfos
{ Debug.Assert(download.FileName != null);
return Task.FromResult("");
} return Task.FromResult(download.FileName);
var uri = new Uri(downloadUrl);
return Task.FromResult(HttpUtility.UrlDecode(uri.Segments.Last()));
} }
private async Task<TorrentClientTorrent> GetInfo(String torrentId) private async Task<TorrentClientTorrent> GetInfo(String torrentId)