private async void StartDownload() { ResetStatusesAndMessages(); if (!_validationService.IsFolderPathValid(SaveFolderPath)) { ValidationError = Constants.ValidationErrors.FolderPathNotValid; return; } IsDownloading = true; _cancellationTokenSource = new CancellationTokenSource(); foreach (var source in Sources) { if (!source.HasUrl) { continue; } if (_validationService.IsUrlValid(source.Url)) { try { if (_cancellationTokenSource.IsCancellationRequested) { continue; } SetSourceStatusAndMessage(source, DownloadStatus.Downloading, Constants.Messages.Downloading); var result = await _pageLoader.LoadPageAsync(source.Url, _cancellationTokenSource); if (_cancellationTokenSource.IsCancellationRequested) { continue; } SetSourceStatusAndMessage(source, DownloadStatus.Saving, Constants.Messages.Saving); var fileName = await _pageSaver.SavePageAsync(SaveFolderPath, result, _cancellationTokenSource); SetSourceStatusAndMessage(source, DownloadStatus.Finished, string.Format(Constants.Messages.SucessfullyDownloadedAndSavedFormat, fileName)); } catch (OperationCanceledException) { // Downloading was cancelled so nothing is required to do. } catch (Exception e) { source.Message = e.ToString(); } } else { source.Message = Constants.ValidationErrors.UrlNotValid; } } IsDownloading = false; }
async Task <CrawlerPageNode> IPageLoader.LoadPageAsync(string url, CrawlerPageNode parentPage) { if (_alreadyLoadedUrls.Contains(url)) { return(null); } var page = await _baseLoader.LoadPageAsync(url, parentPage); _alreadyLoadedUrls.Add(url); return(page); }