Bloom.Api.EnhancedImageServer.MakeSimulatedPageFileInBookFolder C# (CSharp) Method

MakeSimulatedPageFileInBookFolder() public static method

This code sets things up so that we can edit (or make a thumbnail of, etc.) one page of a book. This is tricky because we have to satisfy several constraints: - We need to make this page content the 'src' of an iframe in a browser. So it has to be locatable by url. - It needs to appear to the browser to be a document in the book's folder. This allows local hrefs (e.g., src of images) that are normally relative to the whole-book file to locate the images. (We previously did this by making a file elsewhere and setting the 'base' for interpreting urls. But this fails for internal hrefs (starting with #)). - We don't want to risk leaving junk page files in the real book folder if anything goes wrong. - There may be several of these simulated pages around at the same time (e.g., when the thumbnailer is working on several threads). - The simulated files need to hang around for an unpredictable time (until the browser is done with them). The solution we have adopted is to make this server simulate files in the book folder. That is, the src for the page iframe is set to a localhost: url which maps to a file in the book folder. This means that any local hrefs (e.g., to images) will become server requests for the right file in the right folder. However, the page file never exists as a real file system file; instead, a request for the page file itself will be intercepted, and this server simply returns the content it has remembered. To manage the lifetime of the page data, we use a SimulatedPageFile object, which the Browser disposes of when it is no longer looking at that URL. Its dispose method tells this class to discard the simulated page data. To handle the need for multiple simulated page files and quickly check whether a particular url is one of them, we have a dictionary in which the urls are keys. A marker is inserted into the generated urls if the input HtmlDom wants to use original images.
public static MakeSimulatedPageFileInBookFolder ( HtmlDom dom, bool escapeUrlAsNeededForSrcAttribute = false, bool setAsCurrentPageForDebugging = false ) : SimulatedPageFile
dom Bloom.Book.HtmlDom
escapeUrlAsNeededForSrcAttribute bool If this is true, the url will be inserted by JavaScript into /// a src attr for an IFrame. We need to account for this because un-escaped quotation marks in the /// URL can cause errors in JavaScript strings.
setAsCurrentPageForDebugging bool
return SimulatedPageFile
        public static SimulatedPageFile MakeSimulatedPageFileInBookFolder(HtmlDom dom, bool escapeUrlAsNeededForSrcAttribute = false, bool setAsCurrentPageForDebugging = false)
        {
            var simulatedPageFileName = Path.ChangeExtension(Guid.NewGuid().ToString(), ".html");
            var pathToSimulatedPageFile = simulatedPageFileName; // a default, if there is no special folder
            if (dom.BaseForRelativePaths != null)
            {
                pathToSimulatedPageFile = Path.Combine(dom.BaseForRelativePaths, simulatedPageFileName).Replace('\\', '/');
            }
            // FromLocalHost is smart about doing nothing if it is not a localhost url. In case it is, we
            // want the OriginalImageMarker (if any) after the localhost stuff.
            pathToSimulatedPageFile = pathToSimulatedPageFile.FromLocalhost();
            if (dom.UseOriginalImages)
                pathToSimulatedPageFile = OriginalImageMarker + "/" + pathToSimulatedPageFile;
            var url = pathToSimulatedPageFile.ToLocalhost();
            var key = pathToSimulatedPageFile.Replace('\\', '/');
            if (escapeUrlAsNeededForSrcAttribute)
            {
                // We need to UrlEncode the single and double quote characters so they will play nicely with JavaScript.
                url = EscapeUrlQuotes(url);
                // When JavaScript inserts our path into the html it replaces the three magic html characters with these substitutes.
                // We need to modify our key so that when the JavaScript comes looking for the page its modified url will
                // generate the right key.
                key = SimulateJavaScriptHandlingOfHtml(key);
            }
            if(setAsCurrentPageForDebugging)
            {
                _keyToCurrentPage = key;
            }
            var html5String = TempFileUtils.CreateHtml5StringFromXml(dom.RawDom);
            lock (_urlToSimulatedPageContent)
            {
                _urlToSimulatedPageContent[key] = html5String;
            }
            return new SimulatedPageFile() {Key = url};
        }