/// <summary> /// Download a torrent /// </summary> /// <returns><see cref="Task"/></returns> public async Task Download(T media, TorrentType torrentType, MediaType mediaType, string torrentPath, int uploadLimit, int downloadLimit, IProgress <double> downloadProgress, IProgress <BandwidthRate> bandwidthRate, IProgress <int> nbSeeds, IProgress <int> nbPeers, Action buffered, Action cancelled, CancellationTokenSource cts) { _logger.Info( $"Start downloading : {torrentPath}"); await Task.Run(async() => { using (var session = new session()) { var settings = session.settings(); settings.anonymous_mode = true; downloadProgress.Report(0d); bandwidthRate.Report(new BandwidthRate { DownloadRate = 0d, UploadRate = 0d }); nbSeeds.Report(0); nbPeers.Report(0); session.listen_on(Constants.TorrentMinPort, Constants.TorrentMaxPort); string savePath = string.Empty; switch (mediaType) { case MediaType.Movie: savePath = Constants.MovieDownloads; break; case MediaType.Show: savePath = Constants.ShowDownloads; break; case MediaType.Unkown: savePath = Constants.DropFilesDownloads; break; } if (torrentType == TorrentType.File) { using (var addParams = new add_torrent_params { save_path = savePath, ti = new torrent_info(torrentPath) }) using (var handle = session.add_torrent(addParams)) { await HandleDownload(media, mediaType, uploadLimit, downloadLimit, downloadProgress, bandwidthRate, nbSeeds, nbPeers, handle, session, buffered, cancelled, cts); } } else { var magnet = new magnet_uri(); using (var error = new error_code()) { var addParams = new add_torrent_params { save_path = savePath, }; magnet.parse_magnet_uri(torrentPath, addParams, error); using (var handle = session.add_torrent(addParams)) { await HandleDownload(media, mediaType, uploadLimit, downloadLimit, downloadProgress, bandwidthRate, nbSeeds, nbPeers, handle, session, buffered, cancelled, cts); } } } } }); }