diff --git a/server/RdtClient.Data/Data/TorrentData.cs b/server/RdtClient.Data/Data/TorrentData.cs index 4d7f22c..2411848 100644 --- a/server/RdtClient.Data/Data/TorrentData.cs +++ b/server/RdtClient.Data/Data/TorrentData.cs @@ -11,7 +11,7 @@ namespace RdtClient.Data.Data Task> Get(); Task GetById(Guid torrentId); Task GetByHash(String hash); - Task Add(String realDebridId, String hash, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); + Task Add(String realDebridId, String hash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task UpdateRdData(Torrent torrent); Task UpdateCategory(Guid torrentId, String category); Task UpdateComplete(Guid torrentId, DateTimeOffset datetime); @@ -77,7 +77,7 @@ namespace RdtClient.Data.Data return dbTorrent; } - public async Task Add(String realDebridId, String hash, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) + public async Task Add(String realDebridId, String hash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { var torrent = new Torrent { @@ -85,6 +85,7 @@ namespace RdtClient.Data.Data Added = DateTimeOffset.UtcNow, RdId = realDebridId, Hash = hash.ToLower(), + Category = category, AutoDownload = autoDownload, AutoUnpack = autoUnpack, AutoDelete = autoDelete diff --git a/server/RdtClient.Service/Services/DownloadClient.cs b/server/RdtClient.Service/Services/DownloadClient.cs index ac2d1c6..62a7f41 100644 --- a/server/RdtClient.Service/Services/DownloadClient.cs +++ b/server/RdtClient.Service/Services/DownloadClient.cs @@ -20,6 +20,8 @@ namespace RdtClient.Service.Services private readonly Download _download; private readonly String _destinationPath; private readonly Torrent _torrent; + + private Boolean _cancelled = false; private Int64 _bytesLastUpdate; private DateTime _nextUpdate; @@ -64,7 +66,10 @@ namespace RdtClient.Service.Services await Task.Factory.StartNew(async delegate { - await Download(uri, filePath); + if (!_cancelled) + { + await Download(uri, filePath); + } }); } catch (Exception ex) @@ -74,6 +79,11 @@ namespace RdtClient.Service.Services } } + public void Cancel() + { + _cancelled = true; + } + private async Task Download(Uri uri, String filePath) { try @@ -94,7 +104,7 @@ namespace RdtClient.Service.Services var timeout = DateTimeOffset.UtcNow.AddHours(1); - while (timeout > DateTimeOffset.UtcNow) + while (timeout > DateTimeOffset.UtcNow && !_cancelled) { try { @@ -111,7 +121,7 @@ namespace RdtClient.Service.Services await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write); var buffer = new Byte[4096]; - while (fileStream.Length < response.ContentLength) + while (fileStream.Length < response.ContentLength && !_cancelled) { var read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length)); diff --git a/server/RdtClient.Service/Services/QBittorrent.cs b/server/RdtClient.Service/Services/QBittorrent.cs index c1e9220..c55b72d 100644 --- a/server/RdtClient.Service/Services/QBittorrent.cs +++ b/server/RdtClient.Service/Services/QBittorrent.cs @@ -18,8 +18,8 @@ namespace RdtClient.Service.Services Task> TorrentFileContents(String hash); Task TorrentProperties(String hash); Task TorrentsDelete(String hash, Boolean deleteFiles); - Task TorrentsAdd(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); - Task TorrentsAddFile(Byte[] fileBytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); + Task TorrentsAddMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); + Task TorrentsAddFile(Byte[] fileBytes, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task TorrentsSetCategory(String hash, String category); Task> TorrentsCategories(); } @@ -424,14 +424,14 @@ namespace RdtClient.Service.Services await _torrents.Delete(torrent.TorrentId, true, true, deleteFiles); } - public async Task TorrentsAdd(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) + public async Task TorrentsAddMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { - await _torrents.UploadMagnet(magnetLink, autoDownload, autoUnpack, autoDelete); + await _torrents.UploadMagnet(magnetLink, category, autoDownload, autoUnpack, autoDelete); } - public async Task TorrentsAddFile(Byte[] fileBytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) + public async Task TorrentsAddFile(Byte[] fileBytes, String category,Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { - await _torrents.UploadFile(fileBytes, autoDownload, autoUnpack, autoDelete); + await _torrents.UploadFile(fileBytes, category, autoDownload, autoUnpack, autoDelete); } public async Task TorrentsSetCategory(String hash, String category) diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index 0ff83c7..30eff12 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -20,8 +20,8 @@ namespace RdtClient.Service.Services Task GetById(Guid torrentId); Task GetByHash(String hash); Task UpdateCategory(String hash, String category); - Task UploadMagnet(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); - Task UploadFile(Byte[] bytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); + Task UploadMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); + Task UploadFile(Byte[] bytes, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete); Task SelectFiles(String torrentId, IList fileIds); Task Delete(Guid torrentId, Boolean deleteData, Boolean deleteRdTorrent, Boolean deleteLocalFiles); Task Unrestrict(Guid torrentId); @@ -134,22 +134,22 @@ namespace RdtClient.Service.Services await _torrentData.UpdateCategory(torrent.TorrentId, category); } - public async Task UploadMagnet(String magnetLink, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) + public async Task UploadMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { var magnet = MagnetLink.Parse(magnetLink); var rdTorrent = await GetRdNetClient().AddTorrentMagnetAsync(magnetLink); - await Add(rdTorrent.Id, magnet.InfoHash.ToHex(), autoDownload, autoUnpack, autoDelete); + await Add(rdTorrent.Id, magnet.InfoHash.ToHex(), category, autoDownload, autoUnpack, autoDelete); } - public async Task UploadFile(Byte[] bytes, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) + public async Task UploadFile(Byte[] bytes, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { var torrent = await MonoTorrent.Torrent.LoadAsync(bytes); var rdTorrent = await GetRdNetClient().AddTorrentFileAsync(bytes); - await Add(rdTorrent.Id, torrent.InfoHash.ToHex(), autoDownload, autoUnpack, autoDelete); + await Add(rdTorrent.Id, torrent.InfoHash.ToHex(), category, autoDownload, autoUnpack, autoDelete); } public async Task SelectFiles(String torrentId, IList fileIds) @@ -163,6 +163,19 @@ namespace RdtClient.Service.Services if (torrent != null) { + foreach (var download in torrent.Downloads) + { + if (TorrentRunner.ActiveUnpackClients.TryRemove(download.DownloadId, out var activeUnpack)) + { + activeUnpack.Cancel(); + } + + if (TorrentRunner.ActiveDownloadClients.TryRemove(download.DownloadId, out var activeDownload)) + { + activeDownload.Cancel(); + } + } + if (deleteLocalFiles) { var downloadPath = await DownloadPath(torrent); @@ -228,6 +241,11 @@ namespace RdtClient.Service.Services { return; } + + if (TorrentRunner.ActiveDownloadClients.ContainsKey(downloadId)) + { + return; + } download.DownloadStarted = DateTimeOffset.UtcNow; await _downloads.UpdateDownloadStarted(download.DownloadId, download.DownloadStarted); @@ -276,6 +294,11 @@ namespace RdtClient.Service.Services return; } + if (TorrentRunner.ActiveUnpackClients.ContainsKey(downloadId)) + { + return; + } + download.UnpackingStarted = DateTimeOffset.UtcNow; await _downloads.UpdateUnpackingStarted(download.DownloadId, download.UnpackingStarted); @@ -311,7 +334,7 @@ namespace RdtClient.Service.Services return profile; } - private async Task Add(String rdTorrentId, String infoHash, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) + private async Task Add(String rdTorrentId, String infoHash, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { var w = await SemaphoreSlim.WaitAsync(60000); if (!w) @@ -328,7 +351,7 @@ namespace RdtClient.Service.Services return; } - var newTorrent = await _torrentData.Add(rdTorrentId, infoHash, autoDownload, autoUnpack, autoDelete); + var newTorrent = await _torrentData.Add(rdTorrentId, infoHash, category, autoDownload, autoUnpack, autoDelete); var rdTorrent = await GetRdNetClient().GetTorrentInfoAsync(rdTorrentId); diff --git a/server/RdtClient.Service/Services/UnpackClient.cs b/server/RdtClient.Service/Services/UnpackClient.cs index 39551db..246f143 100644 --- a/server/RdtClient.Service/Services/UnpackClient.cs +++ b/server/RdtClient.Service/Services/UnpackClient.cs @@ -22,6 +22,8 @@ namespace RdtClient.Service.Services private readonly Download _download; private readonly String _destinationPath; private readonly Torrent _torrent; + + private Boolean _cancelled = false; private RarArchiveEntry _rarCurrentEntry; private Dictionary _rarfileStatus; @@ -60,7 +62,10 @@ namespace RdtClient.Service.Services await Task.Factory.StartNew(async delegate { - await Unpack(filePath); + if (!_cancelled) + { + await Unpack(filePath); + } }); } catch (Exception ex) @@ -70,6 +75,11 @@ namespace RdtClient.Service.Services } } + public void Cancel() + { + _cancelled = true; + } + private async Task Unpack(String filePath) { try @@ -101,6 +111,11 @@ namespace RdtClient.Service.Services foreach (var entry in entries) { + if (_cancelled) + { + return; + } + _rarCurrentEntry = entry; entry.WriteToDirectory(extractPath, @@ -136,7 +151,7 @@ namespace RdtClient.Service.Services Finished = true; } } - + private void ArchiveOnCompressedBytesRead(Object sender, CompressedBytesReadEventArgs e) { if (_rarCurrentEntry == null) diff --git a/server/RdtClient.Web/Controllers/QBittorrentController.cs b/server/RdtClient.Web/Controllers/QBittorrentController.cs index 00f81eb..a585755 100644 --- a/server/RdtClient.Web/Controllers/QBittorrentController.cs +++ b/server/RdtClient.Web/Controllers/QBittorrentController.cs @@ -245,7 +245,7 @@ namespace RdtClient.Web.Controllers foreach (var url in urls) { - await _qBittorrent.TorrentsAdd(url.Trim(), true, true, false); + await _qBittorrent.TorrentsAddMagnet(url.Trim(), request.Category, true, true, false); } return Ok(); @@ -265,10 +265,10 @@ namespace RdtClient.Web.Controllers await file.CopyToAsync(target); var fileBytes = target.ToArray(); - await _qBittorrent.TorrentsAddFile(fileBytes, true, true, false); + await _qBittorrent.TorrentsAddFile(fileBytes, request.Category, true, true, false); } } - + if (request.Urls != null) { return await TorrentsAdd(request); @@ -386,6 +386,7 @@ namespace RdtClient.Web.Controllers public class QBTorrentsAddRequest { public String Urls { get; set; } + public String Category { get; set; } } public class QBTorrentsSetCategoryRequest diff --git a/server/RdtClient.Web/Controllers/TorrentsController.cs b/server/RdtClient.Web/Controllers/TorrentsController.cs index 8777674..1e93e9a 100644 --- a/server/RdtClient.Web/Controllers/TorrentsController.cs +++ b/server/RdtClient.Web/Controllers/TorrentsController.cs @@ -74,7 +74,7 @@ namespace RdtClient.Web.Controllers var bytes = memoryStream.ToArray(); - await _torrents.UploadFile(bytes, formData.AutoDownload, formData.AutoUnpack, formData.AutoDelete); + await _torrents.UploadFile(bytes, null, formData.AutoDownload, formData.AutoUnpack, formData.AutoDelete); return Ok(); } @@ -83,7 +83,7 @@ namespace RdtClient.Web.Controllers [Route("UploadMagnet")] public async Task UploadMagnet([FromBody] TorrentControllerUploadMagnetRequest request) { - await _torrents.UploadMagnet(request.MagnetLink, request.AutoDownload, request.AutoUnpack, request.AutoDelete); + await _torrents.UploadMagnet(request.MagnetLink, null, request.AutoDownload, request.AutoUnpack, request.AutoDelete); return Ok(); }