Class: Ramaze::Dispatcher::Directory
- Defined in:
- lib/ramaze/dispatcher/directory.rb
Overview
Generates a directory listing, see Ramaze::Controller::Directory for more information and how to create your own directory listing page
Class Method Summary collapse
-
.build_listing(path) ⇒ Object
Makes a request for yourserver/dirlist/path and returns the result.
-
.call(path) ⇒ Object
Entry point from Dispatcher::filter.
-
.list_for(path) ⇒ Object
Nicely formatted listing for given path, template is “stolen” from lighttpd.
Class Method Details
.build_listing(path) ⇒ Object
Makes a request for yourserver/dirlist/path and returns the result. Due to this method, you can overwrite the action and create your own page. See Ramaze::Controller::Directory for more.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ramaze/dispatcher/directory.rb', line 25 def build_listing(path) dir = ::File.(Global.public_root/::File.(path, '/')) if ::File.directory?(dir) Log.debug("Serving directory listing: #{dir}") Session.current.drop! if Session.current status = Ramaze::STATUS_CODE['OK'] header = {'Content-Type' => "text/html"} body = list_for(dir) [body, status, header] end end |
.call(path) ⇒ Object
Entry point from Dispatcher::filter. Just a forwarder to build_listing, automatticly exiting if there is an error (defined by returning false in build_listing)
15 16 17 18 19 20 |
# File 'lib/ramaze/dispatcher/directory.rb', line 15 def call(path) if Global.list_directories response = build_listing(path) Response.current.build(*response) if response end end |
.list_for(path) ⇒ Object
Nicely formatted listing for given path, template is “stolen” from lighttpd
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ramaze/dispatcher/directory.rb', line 41 def list_for(path) root = ::File.(Global.public_root) display = path.gsub(/^#{Regexp.escape(root)}\/?/, '/') wrapper = %(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Directory listing of #{display}</title> <style type="text/css"> /*<![CDATA[*/ a, a:active {text-decoration: none; color: blue;} a:visited {color: #48468F;} a:hover, a:focus {text-decoration: underline; color: red;} body {background-color: #F5F5F5;} h2 {margin-bottom: 12px;} table {margin-left: 12px;} th, td { font-family: "Courier New", Courier, monospace; font-size: 10pt; text-align: left;} th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;} td {padding-right: 14px;} td.s, th.s {text-align: right;} div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;} div.foot { font-family: "Courier New", Courier, monospace; font-size: 10pt; color: #787878; padding-top: 4px;} /*]]>*/ </style> </head> <body> <h2>Directory listing of #{display}</h2> <div class="list"> <table summary="Directory Listing" cellpadding="0" cellspacing="0"> <thead> <tr> <th class="n">Name</th> <th class="m">Last Modified</th> <th class="s">Size</th> <th class="t">Type</th> </tr> </thead> <tbody> <tr> <td class="n"><a href="#{display/'..'}/">Parent Directory</a>/</td> <td class="m"> </td> <td class="s">- </td> <td class="t">Directory</td> </tr> %s %s </tbody> </table> </div> <div class="foot">%s</div> </body> </html> ) dirs, files = Dir[path/'*'].partition{|file| ::File.directory?(file) } dir_body, file_body = [], [] dirs.sort.each do |dir| basename = ::File.basename(dir) dir_body << %[<tr> <td class="n"><a href="#{display/basename}">#{basename}/</a></td> <td class="m"> </td> <td class="s">- </td> <td class="t">Directory</td> </tr>] end time_format = "%Y-%b-%d %H:%M:%S" files.sort.each do |file| basename = ::File.basename(file) time = ::File.mtime(file).strftime(time_format) size = ::File.size(file).filesize_format mime = Tool::MIME.type_for(file) file_body << %[<tr> <td class="n"><a href="#{display/basename}">#{basename}</a></td> <td class="m">#{time}</td> <td class="s">#{size}</td> <td class="t">#{mime}</td> </tr>] end version = "ramaze/#{Ramaze::VERSION}" wrapper % [ dir_body. join("\n "), file_body.join("\n "), version ] end |