1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 3 4 <title>DirectoryResource - Serving Static Content</title><meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 5 <link href="styles.css" rel="stylesheet" type="text/css" /></head> 6 <body> 7 <h1>FileResource - Serving Individual Files</h1> 8 <p>The <code>FileResource</code> class provides a means to 9 serve individual static files from the 10 filesystem. Although a <a href="directory-resource.html"><code>DirectoryResource</code></a> 11 object may serve files having specific filename extensions as 12 particular types from a filesystem directory, we sometimes need to 13 select individual files from special locations and serve their 14 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> 15 <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>