public async void DownloadChapterAsync() { plugin.Setup(p => p.GetImages("URL1", It.IsAny <Progress <string> >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(new string[] { "IMG1", "IMG2", "IMG3", "IMG4", "IMG5", }.AsEnumerable())); var task = new DownloadChapterRequest("CHAP1", "URL1", "C:\\TEST", new OutputFormat[] { OutputFormat.Folder, OutputFormat.CBZ }); var result = await worker.GetChapterAsync(task, new Progress <string>(), new CancellationToken()); Assert.False(result.Error); pluginManager.Verify(pm => pm.GetPlugin("URL1")); plugin.Verify(p => p.GetImages("URL1", It.IsAny <Progress <string> >(), It.IsAny <CancellationToken>())); for (int i = 0; i < 5; i++) { httpDownloader.Verify(hd => hd.GetFileAsync("IMG" + (i + 1), It.IsAny <string>(), It.IsAny <CancellationToken>())); } outputFactory.Verify(of => of.Create(OutputFormat.Folder), Times.Once); outputFactory.Verify(of => of.Create(OutputFormat.CBZ), Times.Once); outputFolder.Verify(o => o.Save(It.IsAny <string>(), It.IsAny <string>()), Times.Once); outputCbz.Verify(o => o.Save(It.IsAny <string>(), It.IsAny <string>()), Times.Once); }
private async Task DownloadChapterImpl(DownloadChapterRequest task, IProgress <string> progress, CancellationToken cancellationToken) { progress.Report("Starting..."); var plugin = pluginManager.GetPlugin(task.Url); var images = await plugin.GetImages(task.Url, new Progress <string>(count => { progress.Report(count.ToString()); }), cancellationToken); var tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); var index = 1; foreach (var image in images) { progress.Report($"Download: {index}/{images.Count()}"); await downloader.GetFileAsync(image, tempFolder, cancellationToken); index++; } foreach (var format in task.Formats) { var factory = outputFactory.Create(format); factory.Save(tempFolder, task.SaveToFolder); } progress.Report("Done"); }
internal async Task StartDownloadChaptersAsync() { cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; while (downloadRows.Count > 0 && cancellationToken.IsCancellationRequested == false) { var downloading = downloadRows.First(); var request = new DownloadChapterRequest(downloading.Name, downloading.Url, downloading.SaveToFolder, downloading.Formats); var updateProgress = new Progress <string>(c => { downloading.Progress = c; View.SetDownloadRows(downloadRows); }); downloading.IsBusy = true; var taskResult = await worker.GetChapterAsync(request, updateProgress, cancellationToken); downloading.IsBusy = false; downloading.Progress = ""; if (!taskResult.Error) { downloadRows.Remove(downloading); } View.SetDownloadRows(downloadRows); } }
/// <summary> /// Run task download a chapter /// </summary> /// <param name="task">Contain chapter and save to path</param> /// <param name="progress">Callback to report progress</param> /// <returns></returns> public async Task <DownloadChapterResponse> GetChapterAsync(DownloadChapterRequest task, IProgress <string> progress, CancellationToken cancellationToken) { logger.Info($"> DownloadChapter: {task.Url} To: {task.SaveToFolder}"); return(await Task.Run(async() => { var taskResult = new DownloadChapterResponse(); try { await DownloadChapterImpl(task, progress, cancellationToken); } catch (Exception ex) { logger.Error(ex, $"Failed to download chapter: {task.Url}"); taskResult.Error = true; taskResult.Exception = ex; } return taskResult; })); }