/
directory-publisher.lisp
174 lines (152 loc) · 7.42 KB
/
directory-publisher.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
;;;; restas-direcotry-publisher.lisp
;;;;
;;;; This file is part of the restas-directory-publisher library, released under Lisp-LGPL.
;;;; See file COPYING for details.
;;;;
;;;; Author: Moskvitin Andrey <archimag@gmail.com>
(restas:define-module #:restas.directory-publisher
(:use :cl :iter)
(:export #:*directory*
#:*directory-index-files*
#:*autoindex*
#:*ignore-pathname-p*
#:pathname-info
#:hidden-pathname-p)
(:render-method #'restas.directory-publisher.view:autoindex))
(in-package #:restas.directory-publisher)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; module params
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *directory* nil)
(defvar *directory-index-files* '("index.html" "index.htm"))
(defvar *autoindex* nil)
(defvar *ignore-pathname-p* nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; native namestrings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun native-namestring (path)
#+sbcl (sb-ext:native-namestring path)
#-sbcl (namestring path))
(defun parse-native-namestring (thing)
#+sbcl (sb-ext:parse-native-namestring thing)
#-sbcl (parse-namestring thing))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; directory info
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun path-last-name (path)
"File or directory name"
(if (fad:directory-pathname-p path)
(car (last (cdr (pathname-directory path))))
(let ((name (pathname-name path))
(type (pathname-type path)))
(if type
(format nil "~A.~A" name type)
name))))
(defparameter *byte-units* '("kB" "MB" "GB" "TB" "PB" "EB" "ZB" "YB")
"Symbols for show size of files in human readable format")
(defun format-size (bytes)
"Convert number of bytes to human readable format (e.g. 1K 234M 2G)"
(let* ((unit (if (> bytes 0)
(floor (log bytes 1024))
bytes))
(symbol (if (> unit 0)
(nth (1- unit)
*byte-units*))))
(if symbol
(format nil
"~,1F ~A"
(/ bytes (expt 1024 unit))
symbol)
(format nil "~A B" bytes))))
(defun format-last-modified (timestamp)
(local-time:format-timestring
nil
timestamp
:format '((:day 2) #\- :short-month #\- :year #\Space (:hour 2) #\: (:min 2))))
#+(and sbcl unix)
(defun pathname-info (path)
(let ((stat (sb-posix:stat path))
(isdir (fad:directory-pathname-p path)))
(list :mime-type (if (not isdir) (hunchentoot:mime-type path))
:name (path-last-name path)
:last-modified (format-last-modified (local-time:unix-to-timestamp (sb-posix:stat-mtime stat)))
:size (if (not isdir) (format-size (sb-posix:stat-size stat))))))
#-(and sbcl unix)
(defun pathname-info (path)
(let ((isdir (fad:directory-pathname-p path)))
(list :mime-type (if (not isdir) (hunchentoot:mime-type path))
:name (path-last-name path)
:last-modified (format-last-modified (local-time:universal-to-timestamp (file-write-date path)))
:size (if (not isdir) (ignore-errors
(format-size
(with-open-file (s path) (file-length s))))))))
(defun hidden-pathname-p (path)
(if (not (equal path
(make-pathname :directory (list :absolute))))
(char= (char (path-last-name path) 0)
#\.)))
(defun ignore-pathname-p (path)
(if *ignore-pathname-p*
(funcall *ignore-pathname-p* path)
(hidden-pathname-p path)))
(defun directory-autoindex-info (path rpath)
"Info on directory for autoindex"
(labels ((sort-by-name (seq)
(sort seq
#'(lambda (a b)
(string< (getf a :name)
(getf b :name))))))
(let (directories files)
(iter (for item in (fad:list-directory (merge-pathnames path *directory*)))
(unless (ignore-pathname-p item)
(if (fad:directory-pathname-p item)
(push item directories)
(push item files))))
(list :title (format nil "Index of /~A" rpath)
:curdir (or (car (last (cdr (pathname-directory rpath))))
"/")
:parents (if (not (equal path *directory*))
(cons (list :href (restas:genurl 'route
:path '("")
))
(iter (for item in (butlast (cdr (pathname-directory rpath))))
(collect item into curpath)
(collect (list :href (restas:genurl 'route
:path (append curpath
'("")))
:name item)))))
:directories (sort-by-name (iter (for item in directories)
(collect (list* :href (restas:genurl 'route
:path (append (cdr (pathname-directory rpath))
(list (path-last-name item) "")))
(pathname-info item)))))
:files (sort-by-name (iter (for item in files)
(collect (list* :href (restas:genurl 'route
:path (append (cdr (pathname-directory rpath))
(list (path-last-name item))))
(pathname-info item)))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; routes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(restas:define-route route ("*path" :method :get)
(let* ((relative-path (parse-native-namestring (format nil "~{~A~^/~}" path)))
(path (merge-pathnames relative-path
*directory*)))
(cond
((or (find :up (pathname-directory relative-path))
(find :absolute (pathname-directory relative-path)))
hunchentoot:+http-bad-request+)
((ignore-pathname-p path)
hunchentoot:+http-not-found+)
((and (fad:directory-pathname-p path)
(fad:directory-exists-p path))
(or (iter (for index in *directory-index-files*)
(let ((index-path (merge-pathnames index path)))
(finding index-path
such-that (fad:file-exists-p index-path))))
(if *autoindex*
(directory-autoindex-info path relative-path)
hunchentoot:+http-not-found+)))
((not (fad:file-exists-p path))
hunchentoot:+http-not-found+)
(t path))))