Example #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);
        }