From 91dcdb9d7c4f2cdf475f30180845b04a382f60fd Mon Sep 17 00:00:00 2001 From: Sam Heinz <54530346+asylumexp@users.noreply.github.com> Date: Sun, 1 Sep 2024 16:25:36 +1000 Subject: [PATCH] Fix importing from TorBox, adding queued torrents --- .../RdtClient.Service.csproj | 2 +- .../TorrentClients/TorBoxTorrentClient.cs | 36 ++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/server/RdtClient.Service/RdtClient.Service.csproj b/server/RdtClient.Service/RdtClient.Service.csproj index 97a68fc..fba85e6 100644 --- a/server/RdtClient.Service/RdtClient.Service.csproj +++ b/server/RdtClient.Service/RdtClient.Service.csproj @@ -22,7 +22,7 @@ - + diff --git a/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs index c5529b8..3671be3 100644 --- a/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs @@ -4,7 +4,7 @@ using TorBoxNET; using RdtClient.Data.Enums; using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.Helpers; -using Microsoft.AspNetCore.Routing.Constraints; +using MonoTorrent; namespace RdtClient.Service.Services.TorrentClients; @@ -61,11 +61,11 @@ public class TorBoxTorrentClient(ILogger logger, IHttpClien } } - private TorrentClientTorrent Map(Torrent torrent) + private TorrentClientTorrent Map(TorrentInfoResult torrent) { return new() { - Id = torrent.Id.ToString(), + Id = torrent.Hash, Filename = torrent.Name, OriginalFilename = torrent.Name, Hash = torrent.Hash, @@ -92,7 +92,7 @@ public class TorBoxTorrentClient(ILogger logger, IHttpClien public async Task> GetTorrents() { - var torrents = new List(); + var torrents = new List(); var currentTorrents = await GetClient().Torrents.GetCurrentAsync(true); if (currentTorrents != null) @@ -124,14 +124,31 @@ public class TorBoxTorrentClient(ILogger logger, IHttpClien { var result = await GetClient().Torrents.AddMagnetAsync(magnetLink, seeding: 2); - return result.Data?.Hash?.ToString()!; + if (result.Error == "ACTIVE_LIMIT") + { + var magnetLinkInfo = MonoTorrent.MagnetLink.Parse(magnetLink); + return magnetLinkInfo.InfoHash.ToHex().ToLowerInvariant(); + } + else + { + return result.Data!.Hash!; + } } public async Task AddFile(Byte[] bytes) { var result = await GetClient().Torrents.AddFileAsync(bytes, seeding: 2); - - return result.Data?.Hash?.ToString()!; + if (result.Error == "ACTIVE_LIMIT") + { + using (var stream = new MemoryStream(bytes)) + { + var torrent = MonoTorrent.Torrent.Load(stream); + return torrent!.InfoHash.ToHex()!.ToLowerInvariant(); + } + } else + { + return result.Data!.Hash!; + } } public async Task> GetAvailableFiles(String hash) @@ -275,14 +292,15 @@ public class TorBoxTorrentClient(ILogger logger, IHttpClien } var rdTorrent = await GetInfo(torrent.Hash); - var files = new List(); + var torrentId = await GetClient().Torrents.GetInfoAsync(torrent.Hash, skipCache: true); + if (rdTorrent.Files?.Count != 0) { foreach (var file in rdTorrent.Files!) { - var newFile = $"https://torbox.app/fakedl/{rdTorrent.Id}/{file.Id}"; + var newFile = $"https://torbox.app/fakedl/{torrentId?.Id}/{file.Id}"; files.Add(newFile); } }