コード例 #1
0
 public DownloadExceptionListenerContext(ComicDownloadRequest request, ComicChapter chapter,
                                         ComicPage page, CancellationToken token,
                                         Exception exception)
     : base(request, chapter, page, token)
 {
     Exception = exception;
 }
コード例 #2
0
ファイル: DownloadListenerContext.cs プロジェクト: Cricle/Anf
 public DownloadListenerContext(ComicDownloadRequest request, ComicChapter chapter, ComicPage page, CancellationToken token)
 {
     Request = request;
     Chapter = chapter;
     Page    = page;
     Token   = token;
 }
コード例 #3
0
        public static async Task EmitAsync(this IComicDownloader downloader,
                                           ComicDownloadRequest request,
                                           CancellationToken token = default)
        {
            if (downloader is null)
            {
                throw new ArgumentNullException(nameof(downloader));
            }

            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var tasks = downloader.EmitTasks(request, token);

            foreach (var item in tasks)
            {
                await item();
            }
        }
コード例 #4
0
        public Func <Task>[] EmitTasks(ComicDownloadRequest request, CancellationToken token = default)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (request.Provider is null || request.Saver is null || request.DownloadRequests is null)
            {
                throw new ArgumentNullException("Any null for " + nameof(request));
            }
            var tasks = new List <Func <Task> >();

            foreach (var item in request.DownloadRequests)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }
                tasks.Add(() => DownloadPageAsync(request, item.Chapter, item.Page, token));
            }
            return(tasks.ToArray());
        }
コード例 #5
0
        public static Task BatchEmitAsync(this IComicDownloader downloader,
                                          ComicDownloadRequest request,
                                          int concurrent          = 5,
                                          CancellationToken token = default)
        {
            if (downloader is null)
            {
                throw new ArgumentNullException(nameof(downloader));
            }

            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (concurrent <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(concurrent));
            }

            var tasks = downloader.EmitTasks(request, token);

            return(TaskQuene.RunVoidAsync(tasks, concurrent));
        }
コード例 #6
0
        private async Task DownloadPageAsync(ComicDownloadRequest request, ComicChapter chapter, ComicPage page, CancellationToken token)
        {
            var listener = request.Listener;
            DownloadListenerContext listenerContext = null;

            if (listener != null)
            {
                listenerContext = new DownloadListenerContext(request, chapter, page, token);
                await listener.ReadyFetchAsync(listenerContext);
            }
            if (listener != null && token.IsCancellationRequested)
            {
                await listener.CanceledAsync(listenerContext);

                return;
            }
            var ctx = new ComicDownloadContext(request.Entity, chapter, page, null, token);

            if (!request.Saver.NeedToSave(ctx))
            {
                if (listener != null)
                {
                    await listener.NotNeedToSaveAsync(listenerContext);
                }
                return;
            }
            try
            {
                if (listener != null)
                {
                    await listener.BeginFetchPageAsync(listenerContext);
                }
                using (var stream = await request.Provider.GetImageStreamAsync(page.TargetUrl))
                    using (var destStream = streamManager.GetStream())
                    {
                        DownloadSaveListenerContext saveCtx = null;
                        if (listener != null)
                        {
                            saveCtx = new DownloadSaveListenerContext(request, chapter, page, token, destStream);
                            await listener.ReadySaveAsync(saveCtx);
                        }
                        await stream.CopyToAsync(destStream);

                        destStream.Seek(0, SeekOrigin.Begin);
                        ctx = new ComicDownloadContext(request.Entity, chapter, page, destStream, token);
                        await request.Saver.SaveAsync(ctx);

                        if (listener != null)
                        {
                            await listener.EndFetchPageAsync(saveCtx);
                        }
                    }
            }
            catch (Exception ex)
            {
                if (listener != null)
                {
                    var errCtx = new DownloadExceptionListenerContext(request, chapter, page, token, ex);
                    await listener.FetchPageExceptionAsync(errCtx);
                }
            }
            if (listener != null)
            {
                await listener.ComplatedSaveAsync(listenerContext);
            }
        }
コード例 #7
0
 public DownloadSaveListenerContext(ComicDownloadRequest request, ComicChapter chapter, ComicPage page, CancellationToken token,
                                    Stream targetStream)
     : base(request, chapter, page, token)
 {
     baseStream = targetStream;
 }