paulb@654 | 1 | <?xml version="1.0" encoding="iso-8859-1"?> |
paulb@582 | 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@654 | 3 | <html xmlns="http://www.w3.org/1999/xhtml"><head> |
paulb@654 | 4 | <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> |
paulb@654 | 5 | <title>DirectoryResource - Serving Static Content</title> |
paulb@582 | 6 | <link href="styles.css" rel="stylesheet" type="text/css" /></head> |
paulb@582 | 7 | <body> |
paulb@582 | 8 | <h1>FileResource - Serving Individual Files</h1> |
paulb@654 | 9 | <p>The <code>FileResource</code> class provides a means to |
paulb@654 | 10 | serve individual static files from the |
paulb@582 | 11 | filesystem. Although a <a href="directory-resource.html"><code>DirectoryResource</code></a> |
paulb@582 | 12 | object may serve files having specific filename extensions as |
paulb@582 | 13 | particular types from a filesystem directory, we sometimes need to |
paulb@654 | 14 | select individual files from special locations and serve their |
paulb@654 | 15 | contents as part of a response.</p><div class="WebStack"><h3>WebStack API - FileResource Initialisation</h3><p>The <span style="font-family: monospace;">File</span><code>Resource</code> class (found in the <code>WebStack.Resources.Static</code> module) accepts the following parameters when being initialised:</p> |
paulb@654 | 16 | <dl><dt><code>filename</code></dt><dd>The full path to the particular static file being served.</dd><dt><code>content_type</code></dt><dd>A content type object (such as <code>WebStack.Generic.ContentType</code>) providing sufficient information for user agents to interpret the file's contents.</dd></dl></div><h2>Combining MapResource with FileResource</h2><p>One might combine <code>MapResource</code> with <code>FileResource</code> to provide a <code>favicon.ico</code> image for an application as follows:</p><pre>from WebStack.Resources.ResourceMap import MapResource<br />from WebStack.Resources.Static import FileResource<br /><br /># This is where the application's resources would be obtained.<br /><br />app_resource = ...<br /><br /># Here is where we combine MapResource and FileResource.<br /># Note that one would not necessarily hard-code directory paths into the application.<br /><br />top_resource = MapResource({<br /> "favicon.ico" : FileResource("/usr/share/apps/MyApp/images/py.ico"), ContentType("image/x-icon")),<br /> "" : app_resource<br /> })</pre><p>In the above example, the <code>favicon.ico</code> file in the <code>images</code> directory is served as <code>image/x-icon</code>.</p></body></html> |