private void ApplyCredentials(PageAndReferenceDownloader downloader, string url) { WinInetCredentialsContext credentialsContext = CreateCredentialsContext(url); if (credentialsContext != null) { downloader.CredentialsContext = credentialsContext; } }
private string DownloadTemplateFiles(string templateContents, string templateUrl, IProgressHost progress) { progress.UpdateProgress(Res.Get(StringId.ProgressDownloadingSupportingFiles)); FileBasedSiteStorage files = new FileBasedSiteStorage(_blogTemplateDir); // convert the string to a stream MemoryStream templateStream = new MemoryStream(); StreamWriter writer = new StreamWriter(templateStream, Encoding.UTF8); writer.Write(templateContents); writer.Flush(); templateStream.Seek(0, SeekOrigin.Begin); //read the stream into a lightweight HTML. Note that we use from LightWeightHTMLDocument.FromIHTMLDocument2 //instead of LightWeightHTMLDocument.FromStream because from stream improperly shoves a saveFrom declaration //above the docType (bug 289357) IHTMLDocument2 doc = HTMLDocumentHelper.StreamToHTMLDoc(templateStream, templateUrl, true); LightWeightHTMLDocument ldoc = LightWeightHTMLDocument.FromIHTMLDocument2(doc, templateUrl, true, false); PageDownloadContext downloadContext = new PageDownloadContext(0); ApplyCredentials(downloadContext, templateUrl); using (PageToDownloadFactory downloadFactory = new PageToDownloadFactory(ldoc, downloadContext, _parentControl)) { //calculate the dependent styles and resources ProgressTick tick = new ProgressTick(progress, 50, 100); downloadFactory.CreatePagesToDownload(tick); tick.UpdateProgress(100, 100); //download the dependent styles and resources tick = new ProgressTick(progress, 50, 100); PageAndReferenceDownloader downloader = new PageAndReferenceDownloader(downloadFactory.PagesToDownload, files); this.ApplyCredentials(downloader, templateUrl); downloader.Download(tick); tick.UpdateProgress(100, 100); //Expand out the relative paths in the downloaded HTML file with absolute paths. //Note: this is necessary so that template resources are not improperly resolved relative // to the location of the file the editor is editing. string blogTemplateFile = Path.Combine(_blogTemplateDir, files.RootFile); string origFile = blogTemplateFile + ".token"; File.Move(blogTemplateFile, origFile); string absPath = String.Format(CultureInfo.InvariantCulture, "file:///{0}/{1}", _blogTemplateDir.Replace('\\', '/'), downloader.PathToken); TextHelper.ReplaceInFile(origFile, downloader.PathToken, blogTemplateFile, absPath); File.Delete(origFile); //fix up the files FixupDownloadedFiles(blogTemplateFile, files, downloader.PathToken); //complete the progress. progress.UpdateProgress(100, 100); File.WriteAllText(blogTemplateFile + ".path", absPath); return(blogTemplateFile); } }