C# Class Bloom.Api.EnhancedImageServer

A local http server that can serve (low-res) images plus other files.
geckofx makes concurrent requests of URLs which this class handles. This means that the methods of this class get called on different threads, so it has to be thread-safe.
Inheritance: Bloom.ImageProcessing.ImageServer
Exibir arquivo Open project: BloomBooks/BloomDesktop Class Usage Examples

Public Methods

Method Description
EnhancedImageServer ( RuntimeImageProcessor cache, BookThumbNailer thumbNailer, BookSelection bookSelection, BloomFileLocator fileLocator = null ) : System
LocalHostPathToFilePath ( string localPath ) : string

Adjust the 'localPath' obtained from a request in a platform-dependent way to a path that can actually be used to retrieve a file (or test for its existence).

MakeSimulatedPageFileInBookFolder ( HtmlDom dom, bool escapeUrlAsNeededForSrcAttribute = false, bool setAsCurrentPageForDebugging = false ) : SimulatedPageFile

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.

RegisterEndpointHandler ( string pattern, EndpointHandler handler, bool handleOnUiThread = true ) : void

Get called when a client (i.e. javascript) does an HTTP api call

SimulateJavaScriptHandlingOfHtml ( string url ) : string

When JavaScript inserts a url into an html document, it replaces the three magic html characters with these substitutes.

Also used by PretendRequestInfo for testing

Protected Methods

Method Description
Dispose ( bool fDisposing ) : void
IsRecursiveRequestContext ( HttpListenerContext context ) : bool

Requests with ?generateThumbnaiIfNecessary=true are potentially recursive in that we may have to navigate a browser to the template page in order to construct the thumbnail.

IsSimulatedFileUrl ( string localPath ) : bool
ProcessRequest ( IRequestInfo info ) : bool

Private Methods

Method Description
CheckForSampleTextChanges ( IRequestInfo info ) : bool
EnhancedImageServer ( BookSelection bookSelection ) : System

This is only used in a few special cases where we need one to pass as an argument but it won't be fully used.

EscapeUrlQuotes ( string originalUrl ) : string
GetTopicList ( IRequestInfo info ) : bool
ProcessAnyFileContent ( IRequestInfo info, string localPath ) : bool
ProcessContent ( IRequestInfo info, string localPath ) : bool
ProcessCssFile ( IRequestInfo info, string localPath ) : bool
ProcessDirectoryWatcher ( IRequestInfo info ) : bool
ProcessError ( IRequestInfo info ) : void
ProcessI18N ( string localPath, IRequestInfo info ) : bool
RemoveSimulatedPageFile ( string key ) : void
ReplyWithFileContentAndType ( IRequestInfo info, string path ) : void
ReportMissingFile ( string localPath, string path ) : void
SampleTextsOnChange ( object sender, FileSystemEventArgs fileSystemEventArgs ) : void
UnescapeUrlQuotes ( string escapedUrl ) : string

Method Details

Dispose() protected method

protected Dispose ( bool fDisposing ) : void
fDisposing bool
return void

EnhancedImageServer() public method

public EnhancedImageServer ( RuntimeImageProcessor cache, BookThumbNailer thumbNailer, BookSelection bookSelection, BloomFileLocator fileLocator = null ) : System
cache Bloom.ImageProcessing.RuntimeImageProcessor
thumbNailer BookThumbNailer
bookSelection Bloom.Book.BookSelection
fileLocator BloomFileLocator
return System

IsRecursiveRequestContext() protected method

Requests with ?generateThumbnaiIfNecessary=true are potentially recursive in that we may have to navigate a browser to the template page in order to construct the thumbnail.
protected IsRecursiveRequestContext ( HttpListenerContext context ) : bool
context System.Net.HttpListenerContext
return bool

IsSimulatedFileUrl() protected method

protected IsSimulatedFileUrl ( string localPath ) : bool
localPath string
return bool

LocalHostPathToFilePath() public static method

Adjust the 'localPath' obtained from a request in a platform-dependent way to a path that can actually be used to retrieve a file (or test for its existence).
public static LocalHostPathToFilePath ( string localPath ) : string
localPath string
return string

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

ProcessRequest() protected method

protected ProcessRequest ( IRequestInfo info ) : bool
info IRequestInfo
return bool

RegisterEndpointHandler() public method

Get called when a client (i.e. javascript) does an HTTP api call
public RegisterEndpointHandler ( string pattern, EndpointHandler handler, bool handleOnUiThread = true ) : void
pattern string Simple string or regex to match APIs that this can handle. This must match what comes after the ".../api/" of the URL
handler EndpointHandler The method to call
handleOnUiThread bool For safety, this defaults to true, but that can kill performance if you don't need it (BL-3452)
return void

SimulateJavaScriptHandlingOfHtml() public static method

When JavaScript inserts a url into an html document, it replaces the three magic html characters with these substitutes.
Also used by PretendRequestInfo for testing
public static SimulateJavaScriptHandlingOfHtml ( string url ) : string
url string
return string