Ejemplo n.º 1
0
        public async Task <bool> LoadCoverAsync(BookItem B, bool Cache)
        {
            if (Cache && B.CoverExist)
            {
                return(true);
            }

            if (!CoverProcess.AcquireLock(B.Entry, out var QToken))
            {
                goto AwaitToken;
            }

            if (B.IsEx())
            {
                X.Instance <IRuntimeCache>(XProto.WRuntimeCache).InitDownload(
                    B.ZItemId, X.Call <XKey[]>(XProto.WRequest, "GetBookCover", B.ZItemId)
                    , (e, id) =>
                {
                    B.SaveCover(e.ResponseBytes);
                    QToken.TrySetResult(true);
                }
                    , (c, i, ex) => QToken.TrySetResult(false)
                    , false
                    );

                goto AwaitToken;
            }

            if (string.IsNullOrEmpty(B.Info.CoverSrcUrl))
            {
                // Use bing service
                string ThumbUrl = await ImageService.GetProvider(B).GetImage();

                if (string.IsNullOrEmpty(ThumbUrl))
                {
                    QToken.TrySetResult(false);
                    goto AwaitToken;
                }

                B.Info.CoverSrcUrl = ThumbUrl;
            }

            // Set the referer, as it is required by some site such as fanfiction.net
            new RuntimeCache(a =>
            {
                HttpRequest R = new WHttpRequest(a)
                {
                    EN_UITHREAD = false
                };

                if (!string.IsNullOrEmpty(B.Info.OriginalUrl))
                {
                    R.RequestHeaders.Referrer = new Uri(B.Info.OriginalUrl);
                }

                return(R);
            }).GET(new Uri(B.Info.CoverSrcUrl)
                   , (e, id) =>
            {
                B.SaveCover(e.ResponseBytes);
                QToken.TrySetResult(true);
            }
                   , (c, i, ex) => QToken.TrySetResult(false)
                   , false);

AwaitToken:
            return(await QToken.Task);
        }
Ejemplo n.º 2
0
        private async Task CacheCover( BookItem B )
        {
            if( Shared.Storage.FileExists( CurrentBook.CoverPath ) )
            {
                SetCover( B );
                return;
            }

            if( !string.IsNullOrEmpty( B.CoverSrcUrl ) )
            {
                TaskCompletionSource<int> Awaitable = new TaskCompletionSource<int>();

                // Set the referer, as it is required by some site such as fanfiction.net
                new RuntimeCache( a => {
                    HttpRequest R = new WHttpRequest( a );
                    R.EN_UITHREAD = true;

                    if ( !string.IsNullOrEmpty( B.OriginalUrl ) )
                    {
                        R.RequestHeaders[ HttpRequestHeader.Referer ] = B.OriginalUrl;
                    }

                    return R;
                } ).GET( new Uri( B.CoverSrcUrl ), ( a, b ) => {
                    CoverDownloaded( a, b );
                    Awaitable.TrySetResult( 0 );
                }
                // Failed handler
                , ( a, b, c ) => {
                    Awaitable.TrySetResult( 0 );
                }, false );

                await Awaitable.Task;
            }
        }