From ce223d3e2cb24b2b9d27413ad84aceeabc1d8a81 Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sun, 19 Jan 2025 20:43:28 -0700 Subject: [PATCH] Add qBittorrent endpoint transfer/info. --- .../Models/QBittorrent/TransferInfo.cs | 30 +++++++++++++ .../Services/DownloadClient.cs | 27 +++++++++++ .../RdtClient.Service/Services/QBittorrent.cs | 17 ++++++- .../Controllers/QBittorrentController.cs | 45 +++++++++++++------ 4 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 server/RdtClient.Data/Models/QBittorrent/TransferInfo.cs diff --git a/server/RdtClient.Data/Models/QBittorrent/TransferInfo.cs b/server/RdtClient.Data/Models/QBittorrent/TransferInfo.cs new file mode 100644 index 0000000..8af9f38 --- /dev/null +++ b/server/RdtClient.Data/Models/QBittorrent/TransferInfo.cs @@ -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; } +} \ No newline at end of file diff --git a/server/RdtClient.Service/Services/DownloadClient.cs b/server/RdtClient.Service/Services/DownloadClient.cs index f35ba1c..6fdc3de 100644 --- a/server/RdtClient.Service/Services/DownloadClient.cs +++ b/server/RdtClient.Service/Services/DownloadClient.cs @@ -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 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; + } + } } \ No newline at end of file diff --git a/server/RdtClient.Service/Services/QBittorrent.cs b/server/RdtClient.Service/Services/QBittorrent.cs index b001ead..d066fab 100644 --- a/server/RdtClient.Service/Services/QBittorrent.cs +++ b/server/RdtClient.Service/Services/QBittorrent.cs @@ -636,7 +636,7 @@ public class QBittorrent(ILogger logger, Settings settings, Authent public async Task SyncMainData() { - var torrents = await TorrentInfo(); + var torrentInfo = await TorrentInfo(); var categories = await TorrentsCategories(); @@ -649,7 +649,7 @@ public class QBittorrent(ILogger logger, Settings settings, Authent Rid = 0, Tags = null, Trackers = new Dictionary>(), - 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 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 + }; + } } \ No newline at end of file diff --git a/server/RdtClient.Web/Controllers/QBittorrentController.cs b/server/RdtClient.Web/Controllers/QBittorrentController.cs index fe162b2..118b89e 100644 --- a/server/RdtClient.Web/Controllers/QBittorrentController.cs +++ b/server/RdtClient.Web/Controllers/QBittorrentController.cs @@ -322,21 +322,22 @@ public class QBittorrentController(ILogger 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 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