public void Add(string link, CancellationToken cancellationToken) { var guid = Guid.NewGuid().ToString().Substring(0, 8); var filename = _outputFileNameProvider.GetAbsoluteFileName(link); var info = new DownloadProcessInfo(DateTime.Now, guid, link, filename, new CancellationTokenSource()); _downloadProgressCache[guid] = info; _linksCache.Add(info, cancellationToken); _logger.LogInformation($"Inserted new link {link}."); }
public DownloadProcessInfoEventArgs(long received, long toReceive, int percentage, DownloadProcessInfo info) { Info = new DownloadProcessInfo( info.StartTime, info.Key, info.Link, info.Filename, received, toReceive, percentage, running: true, info.TokenSource); }
public DownloadProcessCompletedEventArgs(Exception ex, bool cancelled, DownloadProcessInfo info) { Info = new DownloadProcessInfo( info.StartTime, info.Key, info.Link, info.Filename, info.BytesReceived, info.TotalBytesToReceive, info.ProgressPercentage, cancelled, ex, completed: true, DateTime.Now, info.TokenSource); }
private async Task DownloadAsync(DownloadProcessInfo info) { try { _logger.LogInformation($"Downloading data from link: {info.Link} to {info.Filename}..."); if (!File.Exists(info.Filename) || (File.Exists(info.Filename) && _configuration.OverwriteResults)) { using var client = _webClientFactory.CreateWebClient(info); await client.DownloadFile(info.Key, info.Link, info.Filename, info.TokenSource.Token); _logger.LogInformation($"File {info.Filename} successfully downloaded"); } else { _logger.LogWarning($"File {info.Filename} already exists and overwrite is not allowed!"); } } catch (Exception ex) { await _notificationService.ProccessError(_logger, ex); } }
public IWebClient CreateWebClient(DownloadProcessInfo info) { // Get first not running client if (_webClientsData.FirstOrDefault(wc => !wc.Info.Running) is WebClientData data) { return(data.WebClient); } // New client var newclient = new WebClient(_downloadProgressCache, _configurationService); _webClientsData.Add(new WebClientData { WebClient = newclient, Info = info }); // Max is _configurationService.MaxNumberOfDownload if (_webClientsData.Count > _configurationService.MaxNumberOfDownload) { throw new InvalidOperationException($"Create web client: unexpected number of web clients " + $"(current: {_webClientsData.Count}, max: '{_configurationService.MaxNumberOfDownload}')."); } return(newclient); }