Ejemplo n.º 1
0
        public async Task DownloadAsync(
            Uri downloadUri,
            string targetPath,
            Credentials credentials,
            LinkHandlingOptions linkHandlingOption,
            Action <InformationGridEntryViewData> onNewInfo)
        {
            await Task.Run(
                () =>
            {
                _filePathServant.CleanPath(targetPath);
                using var webProxy = _webProxyFactory.Create(credentials);

                onNewInfo(new InformationGridEntryViewData("Downloading document.."));
                var htmlDocument = _htmlDocumentServant.CreateDocument(webProxy, downloadUri);
                foreach (var handler in _partHandlers)
                {
                    handler.HandlePart(webProxy, htmlDocument, downloadUri, targetPath, linkHandlingOption, onNewInfo);
                }

                _htmlDocumentServant.SaveDocument(targetPath, htmlDocument);
            });
        }
        public void HandlePart(
            IWebProxy webProxy,
            HtmlDocument htmlDoc,
            Uri downloadUri,
            string targetPath,
            LinkHandlingOptions linkHandlingOption,
            Action <InformationGridEntryViewData> onNewInfo)
        {
            var pathParts = GetParts(htmlDoc, onNewInfo);

            foreach (var part in pathParts)
            {
                // This indicates that we need no download and can just leave it
                if (part.Value.StartsWith("data:"))
                {
                    continue;
                }

                var absoluteUrlPath = _urlAligner.CreateAbsoluteUrl(downloadUri, part.Value);

                if (linkHandlingOption.DoDownloadLocally)
                {
                    var download = webProxy.DownloadData(absoluteUrlPath);
                    if (download != null)
                    {
                        var savePath = _filePathFactory.CreateAbsoluteSavePath(targetPath, part.Value);
                        part.WriteValue(savePath);
                        _fileRepo.SaveData(savePath, download);
                        PostProcessPart(webProxy, part, absoluteUrlPath, savePath);
                    }
                }
                else
                {
                    part.WriteValue(absoluteUrlPath);
                }
            }
        }