Blacker.Scraper.Downloader.DownloadChapter C# (CSharp) Метод

DownloadChapter() приватный Метод

private DownloadChapter ( BackgroundWorker backgroundWorker, DoWorkEventArgs e, IChapterRecord chapter, string outputFolder, IDownloadFormatProvider formatProvider ) : void
backgroundWorker System.ComponentModel.BackgroundWorker
e System.ComponentModel.DoWorkEventArgs
chapter IChapterRecord
outputFolder string
formatProvider IDownloadFormatProvider
Результат void
        private void DownloadChapter(BackgroundWorker backgroundWorker, DoWorkEventArgs e, IChapterRecord chapter, string outputFolder, IDownloadFormatProvider formatProvider)
        {
            // add task -> export result
            AddTask();

            var directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "MangaScraper", Guid.NewGuid().ToString()));

            try
            {
                AddTask();
                backgroundWorker.ReportProgress(GetPercentComplete(), "Resolving list of pages.");

                var pages = _pageResolver(chapter);

                AddTask(pages.Count);

                TaskDone();
                backgroundWorker.ReportProgress(GetPercentComplete(), String.Format("List of pages resolved, chapter has {0} pages.", pages.Count));

                int current = 1;

                foreach (var page in pages)
                {

                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    backgroundWorker.ReportProgress(GetPercentComplete(), String.Format("Downloading page {0} from {1}", current, pages.Count));

                    string imgUrl = _imageFinder(page.Value);
                    string filePath = GetUniqueFileName(directory.FullName, page.Key, Path.GetExtension(imgUrl));

                    try
                    {
                        RetryHelper.Retry(() => WebHelper.DownloadImage(imgUrl, filePath));
                    }
                    catch (Exception ex)
                    {
                        _log.Error("Unable to download image from url: '" + imgUrl + "' to '" + filePath + "'", ex);
                    }

                    current++;
                    TaskDone();
                }

                backgroundWorker.ReportProgress(GetPercentComplete(), "All pages downloaded.");
                backgroundWorker.ReportProgress(GetPercentComplete(), "Exporting chapter");

                if (backgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                string path;
                formatProvider.SaveDownloadedChapter(chapter, directory, outputFolder, out path);

                // save result path of the downloaded file
                e.Result = path;

                TaskDone();
                backgroundWorker.ReportProgress(GetPercentComplete(), "Download completed");
            }
            finally
            {
                // remove temp dir
                directory.Delete(true);
            }
        }