[hunchentoot-devel] How to enable directory browsing?

Ignas Mikalajunas ignas.mikalajunas at gmail.com
Sun Nov 26 20:26:26 UTC 2006


On 11/26/06, vedm <mlist at rogers.com> wrote:
>
> When I point the browser to a directory, I want to see a list of the
> files in that directory - how can I do this with hunchentoot?
>
> --
> vedm

(defun get-uploaded-files ()
  (let* ((pathnames nil))
    (cl-fad:walk-directory (get-config-path :upload-path)
                           (lambda (pathname)
                             (push pathname pathnames)))
    pathnames))

To get a list of all the files.

(create-dynamic-directory-dispatcher
           (get-config-path :upload-path)
           "/files/") in the *dispatch-table* to make them downloadable.

Dispatcher defined as:

(defun join-directories (root relative-path)
  (let ((root-directory (pathname-directory root))
        (relative-directory (pathname-directory relative-path)))
    (append root-directory
            (remove-if 'symbolp relative-directory))))

(defun create-dynamic-directory-dispatcher (directory prefix)
  (lambda (request)
    (let ((script-name (tbnl:script-name request))
          (mismatch (mismatch (tbnl:script-name request) prefix
                              :test #'char=)))
      (and mismatch
         (>= mismatch (length prefix))
         (let ((path (make-pathname :directory
                                    (join-directories directory
                                                      (subseq
script-name mismatch))
                                    :name (pathname-name script-name)
                                    :type (pathname-type script-name))))
           (unless (or (cl-fad:directory-pathname-p path)
                       (not (probe-file path)))
             (lambda ()
               (tbnl:handle-static-file path))))))))

depends on cl-fad. The code is not exceptionally beautiful, yet it
should give you enough ideas to do something more reasonable.

Ignas Mikalajūnas


More information about the Tbnl-devel mailing list