WebStack

docs/file-resource.html

733:26865b172666
2007-11-12 paulb [project @ 2007-11-12 00:51:34 by paulb] Added a StringResource class for simple static resources. Introduced base classes for common authentication activities. Merged "app", "path" and "qs" fields into a single "app" field for login and redirection. Added support for OpenID authentication.
     1 <?xml version="1.0" encoding="iso-8859-1"?>     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     3 <html xmlns="http://www.w3.org/1999/xhtml"><head>     4   <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />     5   <title>DirectoryResource - Serving Static Content</title>     6   <link href="styles.css" rel="stylesheet" type="text/css" /></head>     7 <body>     8 <h1>FileResource - Serving Individual Files</h1>     9 <p>The <code>FileResource</code> class provides a means to    10 serve individual static files from the    11 filesystem. Although a <a href="directory-resource.html"><code>DirectoryResource</code></a>    12 object may serve files having specific filename extensions as    13 particular types from a filesystem directory, we sometimes need to    14 select individual files from special locations and serve their    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>    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>