diff --git a/server/RdtClient.Data/Data/DownloadData.cs b/server/RdtClient.Data/Data/DownloadData.cs index 475d491..6a7f566 100644 --- a/server/RdtClient.Data/Data/DownloadData.cs +++ b/server/RdtClient.Data/Data/DownloadData.cs @@ -56,6 +56,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.Link = unrestrictedLink; await _dataContext.SaveChangesAsync(); @@ -68,6 +73,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.DownloadStarted = dateTime; await _dataContext.SaveChangesAsync(); @@ -97,6 +107,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.UnpackingQueued = dateTime; await _dataContext.SaveChangesAsync(); @@ -109,6 +124,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.UnpackingStarted = dateTime; await _dataContext.SaveChangesAsync(); @@ -121,6 +141,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.UnpackingFinished = dateTime; await _dataContext.SaveChangesAsync(); @@ -133,6 +158,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.Completed = dateTime; await _dataContext.SaveChangesAsync(); @@ -145,6 +175,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.Error = error; await _dataContext.SaveChangesAsync(); @@ -157,6 +192,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.RetryCount = retryCount; await _dataContext.SaveChangesAsync(); @@ -169,6 +209,11 @@ namespace RdtClient.Data.Data var dbDownload = await _dataContext.Downloads .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + if (dbDownload == null) + { + return; + } + dbDownload.RemoteId = remoteId; await _dataContext.SaveChangesAsync(); diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 6a40ea0..d1b0638 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -104,6 +104,14 @@ namespace RdtClient.Service.Services Log.Debug($"Processing active download {downloadId}: error {downloadClient.Error}"); var download = await _downloads.GetById(downloadId); + if (download == null) + { + ActiveDownloadClients.TryRemove(downloadId, out _); + + Log.Debug($"Download with ID {downloadId} not found! Removed from queue"); + continue; + } + if (download.RetryCount < DownloadRetryCount) { Log.Debug($"Processing active download {downloadId}: error {downloadClient.Error}, download retry count {download.RetryCount}/{DownloadRetryCount}, torrent retry count {download.Torrent.RetryCount}/{TorrentRetryCount}, retrying download"); diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index ca0bcf6..8664bbd 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -164,6 +164,11 @@ namespace RdtClient.Service.Services { var torrent = await GetById(torrentId); + if (torrent == null) + { + return; + } + await GetRdNetClient().Torrents.SelectFilesAsync(torrent.RdId, fileIds.ToArray()); } @@ -171,6 +176,11 @@ namespace RdtClient.Service.Services { var torrent = await GetById(torrentId); + if (torrent == null) + { + return; + } + var rdTorrent = await GetRdNetClient().Torrents.GetInfoAsync(torrent.RdId); var torrentLinks = rdTorrent.Links.Where(m => !String.IsNullOrWhiteSpace(m)).ToList(); @@ -199,61 +209,63 @@ namespace RdtClient.Service.Services { var torrent = await GetById(torrentId); - if (torrent != null) + if (torrent == null) { - foreach (var download in torrent.Downloads) - { - if (TorrentRunner.ActiveUnpackClients.TryRemove(download.DownloadId, out var activeUnpack)) - { - activeUnpack.Cancel(); - } + return; + } - if (TorrentRunner.ActiveDownloadClients.TryRemove(download.DownloadId, out var activeDownload)) - { - activeDownload.Cancel(); - } + foreach (var download in torrent.Downloads) + { + if (TorrentRunner.ActiveUnpackClients.TryRemove(download.DownloadId, out var activeUnpack)) + { + activeUnpack.Cancel(); } - if (deleteData) + if (TorrentRunner.ActiveDownloadClients.TryRemove(download.DownloadId, out var activeDownload)) { - await _torrentData.UpdateComplete(torrent.TorrentId, DateTimeOffset.UtcNow); - await _downloads.DeleteForTorrent(torrent.TorrentId); - await _torrentData.Delete(torrentId); + activeDownload.Cancel(); } + } - if (deleteRdTorrent) + if (deleteData) + { + await _torrentData.UpdateComplete(torrent.TorrentId, DateTimeOffset.UtcNow); + await _downloads.DeleteForTorrent(torrent.TorrentId); + await _torrentData.Delete(torrentId); + } + + if (deleteRdTorrent) + { + await GetRdNetClient().Torrents.DeleteAsync(torrent.RdId); + } + + if (deleteLocalFiles) + { + var downloadPath = DownloadPath(torrent); + downloadPath = Path.Combine(downloadPath, torrent.RdName); + + if (Directory.Exists(downloadPath)) { - await GetRdNetClient().Torrents.DeleteAsync(torrent.RdId); - } + var retry = 0; - if (deleteLocalFiles) - { - var downloadPath = DownloadPath(torrent); - downloadPath = Path.Combine(downloadPath, torrent.RdName); - - if (Directory.Exists(downloadPath)) + while (true) { - var retry = 0; - - while (true) + try { - try + Directory.Delete(downloadPath, true); + + break; + } + catch + { + retry++; + + if (retry >= 3) { - Directory.Delete(downloadPath, true); - - break; + throw; } - catch - { - retry++; - if (retry >= 3) - { - throw; - } - - await Task.Delay(1000); - } + await Task.Delay(1000); } } } @@ -263,7 +275,18 @@ namespace RdtClient.Service.Services private async Task DeleteDownload(Guid torrentId, Guid downloadId) { var torrent = await GetById(torrentId); + + if (torrent == null) + { + return; + } + var download = await _downloads.GetById(downloadId); + + if (download == null) + { + return; + } var downloadPath = DownloadPath(torrent); @@ -305,6 +328,11 @@ namespace RdtClient.Service.Services { var download = await _downloads.GetById(downloadId); + if (download == null) + { + throw new Exception($"Download with ID {downloadId} not found"); + } + var unrestrictedLink = await GetRdNetClient().Unrestrict.LinkAsync(download.Path); await _downloads.UpdateUnrestrictedLink(downloadId, unrestrictedLink.Download); @@ -388,6 +416,11 @@ namespace RdtClient.Service.Services { var torrent = await _torrentData.GetById(torrentId); + if (torrent == null) + { + return; + } + var newRetryCount = torrent.RetryCount + 1; foreach (var download in torrent.Downloads) @@ -498,24 +531,26 @@ namespace RdtClient.Service.Services { var torrent = await _torrentData.GetById(torrentId); - if (torrent != null) + if (torrent == null) { - await Update(torrent); + return null; + } - foreach (var download in torrent.Downloads) + await Update(torrent); + + foreach (var download in torrent.Downloads) + { + if (TorrentRunner.ActiveDownloadClients.TryGetValue(download.DownloadId, out var downloadClient)) { - if (TorrentRunner.ActiveDownloadClients.TryGetValue(download.DownloadId, out var downloadClient)) - { - download.Speed = downloadClient.Speed; - download.BytesTotal = downloadClient.BytesTotal; - download.BytesDone = downloadClient.BytesDone; - } + download.Speed = downloadClient.Speed; + download.BytesTotal = downloadClient.BytesTotal; + download.BytesDone = downloadClient.BytesDone; + } - if (TorrentRunner.ActiveUnpackClients.TryGetValue(download.DownloadId, out var unpackClient)) - { - download.BytesTotal = unpackClient.BytesTotal; - download.BytesDone = unpackClient.BytesDone; - } + if (TorrentRunner.ActiveUnpackClients.TryGetValue(download.DownloadId, out var unpackClient)) + { + download.BytesTotal = unpackClient.BytesTotal; + download.BytesDone = unpackClient.BytesDone; } } diff --git a/server/RdtClient.Web/RdtClient.Web.csproj b/server/RdtClient.Web/RdtClient.Web.csproj index d267ee0..1bbe37f 100644 --- a/server/RdtClient.Web/RdtClient.Web.csproj +++ b/server/RdtClient.Web/RdtClient.Web.csproj @@ -4,7 +4,7 @@ net5.0 Exe 94c24cba-f03f-4453-a671-3640b517c573 - 1.8.8 + 1.8.9