diff --git a/server/RdtClient.Service.Test/GlobalTestConfig.cs b/server/RdtClient.Service.Test/GlobalTestConfig.cs index 2171200..e5cc5d4 100644 --- a/server/RdtClient.Service.Test/GlobalTestConfig.cs +++ b/server/RdtClient.Service.Test/GlobalTestConfig.cs @@ -1,3 +1 @@ -using Xunit; - [assembly: CollectionBehavior(DisableTestParallelization = true)] diff --git a/server/RdtClient.Service.Test/RdtClient.Service.Test.csproj b/server/RdtClient.Service.Test/RdtClient.Service.Test.csproj index dba5280..cc64fb1 100644 --- a/server/RdtClient.Service.Test/RdtClient.Service.Test.csproj +++ b/server/RdtClient.Service.Test/RdtClient.Service.Test.csproj @@ -11,17 +11,17 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + all diff --git a/server/RdtClient.Service/RdtClient.Service.csproj b/server/RdtClient.Service/RdtClient.Service.csproj index 6f375a4..758787a 100644 --- a/server/RdtClient.Service/RdtClient.Service.csproj +++ b/server/RdtClient.Service/RdtClient.Service.csproj @@ -26,7 +26,7 @@ - + diff --git a/server/RdtClient.Service/Services/QBittorrent.cs b/server/RdtClient.Service/Services/QBittorrent.cs index 0192ee4..c46b85d 100644 --- a/server/RdtClient.Service/Services/QBittorrent.cs +++ b/server/RdtClient.Service/Services/QBittorrent.cs @@ -633,7 +633,7 @@ public class QBittorrent(ILogger logger, Settings settings, Authent } } - public async Task TorrentsAddMagnet(String magnetLink, String? category, Int32? priority) + public async Task TorrentsAddMagnet(String magnetLink, String? category, Int32? priority) { logger.LogDebug($"Add magnet {category}"); @@ -655,10 +655,10 @@ public class QBittorrent(ILogger logger, Settings settings, Authent Priority = priority ?? (Settings.Get.Integrations.Default.Priority > 0 ? Settings.Get.Integrations.Default.Priority : null) }; - await torrents.AddMagnetToDebridQueue(magnetLink, torrent); + return await torrents.AddMagnetToDebridQueue(magnetLink, torrent); } - public async Task TorrentsAddFile(Byte[] fileBytes, String? category, Int32? priority) + public async Task TorrentsAddFile(Byte[] fileBytes, String? category, Int32? priority) { logger.LogDebug($"Add file {category}"); @@ -680,7 +680,7 @@ public class QBittorrent(ILogger logger, Settings settings, Authent Priority = priority ?? (Settings.Get.Integrations.Default.Priority > 0 ? Settings.Get.Integrations.Default.Priority : null) }; - await torrents.AddFileToDebridQueue(fileBytes, torrent); + return await torrents.AddFileToDebridQueue(fileBytes, torrent); } public async Task TorrentsSetCategory(String hash, String? category) diff --git a/server/RdtClient.Web.Test/Controllers/QBittorrentControllerTest.cs b/server/RdtClient.Web.Test/Controllers/QBittorrentControllerTest.cs index 9e43df1..7e18a7c 100644 --- a/server/RdtClient.Web.Test/Controllers/QBittorrentControllerTest.cs +++ b/server/RdtClient.Web.Test/Controllers/QBittorrentControllerTest.cs @@ -12,16 +12,14 @@ public class QBittorrentControllerTest { private readonly QBittorrentController _controller; private readonly Mock _qBittorrentMock; + private readonly Mock _torrentsMock; public QBittorrentControllerTest() { _qBittorrentMock = new(new Mock>().Object, null!, null!, null!, null!); + _torrentsMock = new(null!, null!, null!, null!, null!, null!, null!, null!, null!, null!, null!); - _controller = new( - new Mock>().Object, - _qBittorrentMock.Object, - new Mock().Object); - + _controller = new(new Mock>().Object, _qBittorrentMock.Object, new Mock().Object, _torrentsMock.Object); _controller.ControllerContext = new() { HttpContext = new DefaultHttpContext() diff --git a/server/RdtClient.Web.Test/GlobalTestConfig.cs b/server/RdtClient.Web.Test/GlobalTestConfig.cs index 2171200..e5cc5d4 100644 --- a/server/RdtClient.Web.Test/GlobalTestConfig.cs +++ b/server/RdtClient.Web.Test/GlobalTestConfig.cs @@ -1,3 +1 @@ -using Xunit; - [assembly: CollectionBehavior(DisableTestParallelization = true)] diff --git a/server/RdtClient.Web.Test/RdtClient.Web.Test.csproj b/server/RdtClient.Web.Test/RdtClient.Web.Test.csproj index 7cf368c..b46c67a 100644 --- a/server/RdtClient.Web.Test/RdtClient.Web.Test.csproj +++ b/server/RdtClient.Web.Test/RdtClient.Web.Test.csproj @@ -10,7 +10,7 @@ - + diff --git a/server/RdtClient.Web/Controllers/QBittorrentController.cs b/server/RdtClient.Web/Controllers/QBittorrentController.cs index 110e775..79d4693 100644 --- a/server/RdtClient.Web/Controllers/QBittorrentController.cs +++ b/server/RdtClient.Web/Controllers/QBittorrentController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using RdtClient.Data.Enums; using RdtClient.Data.Models.QBittorrent; using RdtClient.Service.Services; -using RealDebridException = RDNET.RealDebridException; +using Torrent = RdtClient.Data.Models.Data.Torrent; namespace RdtClient.Web.Controllers; @@ -14,7 +14,7 @@ namespace RdtClient.Web.Controllers; [ApiController] [Route("api/v2")] [Route("qbittorrent/api/v2")] -public class QBittorrentController(ILogger logger, QBittorrent qBittorrent, IHttpClientFactory httpClientFactory) : Controller +public class QBittorrentController(ILogger logger, QBittorrent qBittorrent, IHttpClientFactory httpClientFactory, Torrents torrents) : Controller { [AllowAnonymous] [Route("/version/api")] @@ -368,36 +368,33 @@ public class QBittorrentController(ILogger logger, QBitto foreach (var url in urls) { - try + Torrent? torrent; + if (url.StartsWith("magnet")) { - if (url.StartsWith("magnet")) - { - await qBittorrent.TorrentsAddMagnet(url.Trim(), request.Category, null); - } - else if (url.StartsWith("http")) - { - var httpClient = httpClientFactory.CreateClient(); - var result = await httpClient.GetByteArrayAsync(url); - await qBittorrent.TorrentsAddFile(result, request.Category, null); - } - else - { - return BadRequest($"Invalid torrent link format {url}"); - } + torrent = await qBittorrent.TorrentsAddMagnet(url.Trim(), request.Category, null); } - catch (RealDebridException ex) + else if (url.StartsWith("http")) { - // Infringing file. - if (ex.ErrorCode == 35) - { - return Ok("Fails."); - } + var httpClient = httpClientFactory.CreateClient(); + var result = await httpClient.GetByteArrayAsync(url); + torrent = await qBittorrent.TorrentsAddFile(result, request.Category, null); + } + else + { + return BadRequest($"Invalid torrent link format {url}"); + } + + var addResult = await WaitForTorrent(torrent.TorrentId); + + if (!addResult) + { + return Ok("Fails."); } } return Ok(); } - + [Authorize(Policy = "AuthSetting")] [Route("torrents/add")] [HttpPost] @@ -412,7 +409,14 @@ public class QBittorrentController(ILogger logger, QBitto await file.CopyToAsync(target); var fileBytes = target.ToArray(); - await qBittorrent.TorrentsAddFile(fileBytes, request.Category, request.Priority); + var torrent = await qBittorrent.TorrentsAddFile(fileBytes, request.Category, request.Priority); + + var addResult = await WaitForTorrent(torrent.TorrentId); + + if (!addResult) + { + return Ok("Fails."); + } } } @@ -671,6 +675,31 @@ public class QBittorrentController(ILogger logger, QBitto _ => String.Equals(torrent.State, filter, StringComparison.OrdinalIgnoreCase) }; } + + private async Task WaitForTorrent(Guid torrentId) + { + while (true) + { + var torrent = await torrents.GetById(torrentId); + + if (torrent == null) + { + throw new($"Failed to add torrent: Not Found"); + } + + if (torrent.RdStatus == TorrentStatus.Error || torrent.Error != null) + { + return false; + } + + if (torrent.RdStatus != TorrentStatus.Queued) + { + return true; + } + + await Task.Delay(1000); + } + } } public class QBAuthLoginRequest