Fix importing from TorBox, adding queued torrents

This commit is contained in:
Sam Heinz 2024-09-01 16:25:36 +10:00
parent e20dd84e1d
commit 91dcdb9d7c
2 changed files with 28 additions and 10 deletions

View file

@ -22,7 +22,7 @@
<PackageReference Include="Serilog" Version="4.0.0" /> <PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="SharpCompress" Version="0.37.2" /> <PackageReference Include="SharpCompress" Version="0.37.2" />
<PackageReference Include="TorBox.NET" Version="1.0.0.31" /> <PackageReference Include="TorBox.NET" Version="1.0.0.39" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -4,7 +4,7 @@ using TorBoxNET;
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 Microsoft.AspNetCore.Routing.Constraints; using MonoTorrent;
namespace RdtClient.Service.Services.TorrentClients; namespace RdtClient.Service.Services.TorrentClients;
@ -61,11 +61,11 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
} }
} }
private TorrentClientTorrent Map(Torrent torrent) private TorrentClientTorrent Map(TorrentInfoResult torrent)
{ {
return new() return new()
{ {
Id = torrent.Id.ToString(), Id = torrent.Hash,
Filename = torrent.Name, Filename = torrent.Name,
OriginalFilename = torrent.Name, OriginalFilename = torrent.Name,
Hash = torrent.Hash, Hash = torrent.Hash,
@ -92,7 +92,7 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
public async Task<IList<TorrentClientTorrent>> GetTorrents() public async Task<IList<TorrentClientTorrent>> GetTorrents()
{ {
var torrents = new List<Torrent>(); var torrents = new List<TorrentInfoResult>();
var currentTorrents = await GetClient().Torrents.GetCurrentAsync(true); var currentTorrents = await GetClient().Torrents.GetCurrentAsync(true);
if (currentTorrents != null) if (currentTorrents != null)
@ -124,14 +124,31 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
{ {
var result = await GetClient().Torrents.AddMagnetAsync(magnetLink, seeding: 2); 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<String> AddFile(Byte[] bytes) public async Task<String> AddFile(Byte[] bytes)
{ {
var result = await GetClient().Torrents.AddFileAsync(bytes, seeding: 2); var result = await GetClient().Torrents.AddFileAsync(bytes, seeding: 2);
if (result.Error == "ACTIVE_LIMIT")
return result.Data?.Hash?.ToString()!; {
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<IList<TorrentClientAvailableFile>> GetAvailableFiles(String hash) public async Task<IList<TorrentClientAvailableFile>> GetAvailableFiles(String hash)
@ -275,14 +292,15 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
} }
var rdTorrent = await GetInfo(torrent.Hash); var rdTorrent = await GetInfo(torrent.Hash);
var files = new List<String>(); var files = new List<String>();
var torrentId = await GetClient().Torrents.GetInfoAsync(torrent.Hash, skipCache: true);
if (rdTorrent.Files?.Count != 0) if (rdTorrent.Files?.Count != 0)
{ {
foreach (var file in rdTorrent.Files!) 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); files.Add(newFile);
} }
} }