Add qBittorrent endpoint transfer/info.

This commit is contained in:
Roger Far 2025-01-19 20:43:28 -07:00
parent 3ed80909de
commit ce223d3e2c
4 changed files with 103 additions and 16 deletions

View file

@ -0,0 +1,30 @@
using System.Text.Json.Serialization;
namespace RdtClient.Data.Models.QBittorrent;
public class TransferInfo
{
[JsonPropertyName("connection_status")]
public String ConnectionStatus { get; set; } = default!;
[JsonPropertyName("dht_nodes")]
public Int64 DhtNodes { get; set; }
[JsonPropertyName("dl_info_data")]
public Int64 DlInfoData { get; set; }
[JsonPropertyName("dl_info_speed")]
public Int64 DlInfoSpeed { get; set; }
[JsonPropertyName("dl_rate_limit")]
public Int64 DlRateLimit { get; set; }
[JsonPropertyName("up_info_data")]
public Int64 UpInfoData { get; set; }
[JsonPropertyName("up_info_speed")]
public Int64 UpInfoSpeed { get; set; }
[JsonPropertyName("up_rate_limit")]
public Int64 UpRateLimit { get; set; }
}

View file

@ -6,6 +6,9 @@ namespace RdtClient.Service.Services;
public class DownloadClient(Download download, Torrent torrent, String destinationPath, String? category)
{
private static Int64 _totalBytesDownloadedThisSession;
private static readonly Lock TotalBytesDownloadedLock = new();
public IDownloader? Downloader;
public Data.Enums.DownloadClient Type { get; private set; }
@ -18,6 +21,8 @@ public class DownloadClient(Download download, Torrent torrent, String destinati
public Int64 BytesTotal { get; private set; }
public Int64 BytesDone { get; private set; }
private Int64 LastBytesDone { get; set; }
public async Task<String> Start()
{
BytesDone = 0;
@ -66,6 +71,12 @@ public class DownloadClient(Download download, Torrent torrent, String destinati
Speed = args.Speed;
BytesDone = args.BytesDone;
BytesTotal = args.BytesTotal;
var bytesAdded = BytesDone - LastBytesDone;
LastBytesDone = BytesDone;
AddToTotalBytesDownloadedThisSession(bytesAdded);
};
var result = await Downloader.Download();
@ -114,4 +125,20 @@ public class DownloadClient(Download download, Torrent torrent, String destinati
}
await Downloader.Resume();
}
public static Int64 GetTotalBytesDownloadedThisSession()
{
lock (TotalBytesDownloadedLock)
{
return _totalBytesDownloadedThisSession;
}
}
private static void AddToTotalBytesDownloadedThisSession(Int64 bytes)
{
lock (TotalBytesDownloadedLock)
{
_totalBytesDownloadedThisSession += bytes;
}
}
}

View file

@ -636,7 +636,7 @@ public class QBittorrent(ILogger<QBittorrent> logger, Settings settings, Authent
public async Task<SyncMetaData> SyncMainData()
{
var torrents = await TorrentInfo();
var torrentInfo = await TorrentInfo();
var categories = await TorrentsCategories();
@ -649,7 +649,7 @@ public class QBittorrent(ILogger<QBittorrent> logger, Settings settings, Authent
Rid = 0,
Tags = null,
Trackers = new Dictionary<String, List<String>>(),
Torrents = torrents.ToDictionary(m => m.Hash, m => m),
Torrents = torrentInfo.ToDictionary(m => m.Hash, m => m),
ServerState = new()
{
DlInfoSpeed = activeDownloads,
@ -657,4 +657,17 @@ public class QBittorrent(ILogger<QBittorrent> logger, Settings settings, Authent
}
};
}
public static TransferInfo TransferInfo()
{
var activeDownloads = TorrentRunner.ActiveDownloadClients.Sum(m => m.Value.Speed);
return new()
{
ConnectionStatus = "connected",
DlInfoData = DownloadClient.GetTotalBytesDownloadedThisSession(),
DlInfoSpeed = activeDownloads,
DlRateLimit = Settings.Get.DownloadClient.MaxSpeed
};
}
}

View file

@ -322,21 +322,22 @@ public class QBittorrentController(ILogger<QBittorrentController> logger, QBitto
foreach (var url in urls)
{
try{
if (url.StartsWith("magnet"))
try
{
await qBittorrent.TorrentsAddMagnet(url.Trim(), request.Category, null);
}
else if (url.StartsWith("http"))
{
var httpClient = new HttpClient();
var result = await httpClient.GetByteArrayAsync(url);
await qBittorrent.TorrentsAddFile(result, request.Category, null);
}
else
{
return BadRequest($"Invalid torrent link format {url}");
}
if (url.StartsWith("magnet"))
{
await qBittorrent.TorrentsAddMagnet(url.Trim(), request.Category, null);
}
else if (url.StartsWith("http"))
{
var httpClient = new HttpClient();
var result = await httpClient.GetByteArrayAsync(url);
await qBittorrent.TorrentsAddFile(result, request.Category, null);
}
else
{
return BadRequest($"Invalid torrent link format {url}");
}
}
catch (RDNET.RealDebridException ex)
{
@ -507,6 +508,22 @@ public class QBittorrentController(ILogger<QBittorrentController> logger, QBitto
{
return await SyncMainData();
}
[Authorize(Policy = "AuthSetting")]
[Route("transfer/info")]
[HttpGet]
public ActionResult TransferInfo()
{
return Ok(QBittorrent.TransferInfo());
}
[Authorize(Policy = "AuthSetting")]
[Route("transfer/info")]
[HttpPost]
public ActionResult TransferInfoPost()
{
return TransferInfo();
}
}
public class QBAuthLoginRequest