public async Task <IList <Torrent> > Update() { var w = await SemaphoreSlim.WaitAsync(1); if (!w) { return(await _torrentData.Get()); } var torrents = await _torrentData.Get(); try { var rdTorrents = await RdNetClient.GetTorrentsAsync(0, 100); foreach (var rdTorrent in rdTorrents) { var torrent = torrents.FirstOrDefault(m => m.RdId == rdTorrent.Id); if (torrent == null) { var newTorrent = await _torrentData.Add(rdTorrent.Id, rdTorrent.Hash, false, false); await GetById(newTorrent.TorrentId); } else if (rdTorrent.Files == null) { var rdTorrent2 = await RdNetClient.GetTorrentInfoAsync(rdTorrent.Id); await Update(torrent, rdTorrent2); } else { await Update(torrent, rdTorrent); } } foreach (var torrent in torrents) { var rdTorrent = rdTorrents.FirstOrDefault(m => m.Id == torrent.RdId); if (rdTorrent == null) { await _downloads.DeleteForTorrent(torrent.TorrentId); await _torrentData.Delete(torrent.TorrentId); } } return(torrents); } finally { SemaphoreSlim.Release(); } }
public async Task Delete(Guid torrentId, Boolean deleteData, Boolean deleteRdTorrent, Boolean deleteLocalFiles) { var torrent = await GetById(torrentId); 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); downloadPath = Path.Combine(downloadPath, torrent.RdName); if (Directory.Exists(downloadPath)) { Directory.Delete(downloadPath, true); } } if (deleteData) { await _downloads.DeleteForTorrent(torrent.TorrentId); await _torrentData.Delete(torrentId); } if (deleteRdTorrent) { await GetRdNetClient().DeleteTorrentAsync(torrent.RdId); } } }