Beispiel #1
0
        public virtual Task ReadyFetchAsync(DownloadListenerContext context)
        {
#if NETSTANDARD2_0
            return(Task.CompletedTask);
#else
            return(Task.FromResult(false));
#endif
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
 public Task BeginFetchPageAsync(DownloadListenerContext context)
 {
     return(Task.WhenAll(this.Where(x => x != null).Select(x => x.BeginFetchPageAsync(context))));
 }
Beispiel #4
0
 public Task ReadyFetchAsync(DownloadListenerContext context)
 {
     return(Task.WhenAll(this.Where(x => x != null).Select(x => x.ReadyFetchAsync(context))));
 }
Beispiel #5
0
 public Task NotNeedToSaveAsync(DownloadListenerContext context)
 {
     return(Task.WhenAll(this.Where(x => x != null).Select(x => x.NotNeedToSaveAsync(context))));
 }
Beispiel #6
0
 public Task ComplatedSaveAsync(DownloadListenerContext context)
 {
     return(Task.WhenAll(this.Where(x => x != null).Select(x => x.ComplatedSaveAsync(context))));
 }