# Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com # All files in this distribution are subject to the terms of the Ruby license. module Ramaze class Dispatcher # Generates a directory listing, see Ramaze::Controller::Directory for more # information and how to create your own directory listing page class Directory class << self # 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) def call(path) if Global.list_directories response = build_listing(path) Response.current.build(*response) if response end end # Makes a request for http://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. def build_listing(path) dir = ::File.expand_path(Global.public_root/::File.expand_path(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 # Nicely formatted listing for given path, template is "stolen" from # lighttpd def list_for(path) root = ::File.expand_path(Global.public_root) display = path.gsub(/^#{Regexp.escape(root)}\/?/, '/') wrapper = %( Directory listing of #{display}

Directory listing of #{display}

%s %s
Name Last Modified Size Type
Parent Directory/   -   Directory
%s
) dirs, files = Dir[path/'*'].partition{|file| ::File.directory?(file) } dir_body, file_body = [], [] dirs.sort.each do |dir| basename = ::File.basename(dir) dir_body << %[ #{basename}/   -   Directory ] 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 << %[ #{basename} #{time} #{size} #{mime} ] end version = "ramaze/#{Ramaze::VERSION}" wrapper % [ dir_body. join("\n "), file_body.join("\n "), version ] end end end end end