-
Notifications
You must be signed in to change notification settings - Fork 0
/
ambar.lisp
150 lines (128 loc) · 4.82 KB
/
ambar.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
;;;; ambar.lisp
(in-package #:ambar)
(defparameter *stores* '(""))
;; directory where files are located
(defparameter *root* #p"/home/pi/Downloads/")
(defparameter *Download* "/Download/")
(defparameter *static* (asdf:system-relative-pathname 'ambar "static/"))
(setf (cl-who:html-mode) :html5)
(defmacro with-html-page (&rest body)
"Basic template for a general-purpose Web page."
(let ((out (gensym)))
`(cl-who:with-html-output-to-string (,out nil :prologue t :indent t)
(:html
(:head
(:title "Ambar")
(:meta :name "viewport" :content "width=device-width, initial-scale=1.0")
(:link :href "/static/bootstrap/css/bootstrap.min.css" :rel "stylesheet"))
(:body
(:div :class "container-fluid"
,@body)))
,out)))
(defun make-breadcrumbs (file-name)
"Generates breadcrumbs for the given location."
(cl-who:with-html-output-to-string (out nil :indent t)
(:nav :aria-label "breadcrumb"
(:ol :class "breadcrumb"
(:li :class "breadcrumb-item"
;; use Bootstrap icon 'house' to label Root directory
(:a :href "/dir?dir-name=."
(:svg :class "bi" :width "19" :height "19" :fill "currentColor"
(:use :|xlink:href| "/static/icons/bootstrap-icons.svg#house"))))
(loop for dir in (cdr (pathname-directory file-name))
for dir-name = (concatenate 'string "/dir?dir-name=" dir "/")
then (concatenate 'string dir-name dir "/")
do
(cl-who:htm
(:li :class "breadcrumb-item"
(:a :href dir-name
(cl-who:str dir)))))))
out))
(defun make-download-button (file-name)
(cl-who:with-html-output-to-string (out nil :indent t)
(:a :class "btn btn-primary" :href (format nil "~a~a" *Download* (quri:url-encode file-name))
(:svg :class "bi" :width "19" :height "19" :fill "currentColor"
(:use :|xlink:href| "/static/icons/bootstrap-icons.svg#download")))))
(defun image-file-p (file-name)
(find (string-downcase (pathname-type file-name))
'("jpg" "gif" "png" "tiff" "jpeg")
:test #'string-equal))
(defun video-file-p (file-name)
(find (string-downcase (pathname-type file-name))
'("avi" "mpg" "mp4" "mov" "3gp")
:test #'string-equal))
(hunchentoot:define-easy-handler (view :uri "/view")
((file-name :parameter-type 'string))
(cond
((image-file-p file-name)
(with-html-page
(cl-who:str (make-breadcrumbs file-name))
(:div :class "row justify-content-start"
(:img :src (format nil "~a~a" *Download* file-name)
:class "img-fluid"))
(:div :class "row justify-content-start"
(cl-who:str (make-download-button file-name)))))
((video-file-p file-name)
(with-html-page
(cl-who:str (make-breadcrumbs file-name))
(:video
:controls t
(:source :src (format nil "~a~a" *Download* file-name)
:type "video/x-msvideo"))
(cl-who:str (make-download-button file-name))
;; todo: more file types support, add support for playback with
;; OMX Player on the Pi
))
(t
(with-html-page
(cl-who:str (make-breadcrumbs file-name))
(cl-who:str (make-download-button file-name))))))
(hunchentoot:define-easy-handler (dir :uri "/dir")
((dir-name :parameter-type 'string :init-form "."))
(with-html-page
(:div :class "row justify-content-start"
(:div :class "col"
(cl-who:str (make-breadcrumbs dir-name))))
(:div :class "row justify-content-start"
(:div :class "col"
(:table :class "table table-sm"
(:thead
(:tr
(:th :scope "col" "Name")))
(:tbody
(dolist (entry (sort (cl-fad:list-directory
(cl-fad:merge-pathnames-as-directory *root* dir-name))
#'(lambda (a b)
(string-lessp (namestring a)
(namestring b)))))
(if (cl-fad:directory-pathname-p entry)
;; directory
(let ((name (car (last (pathname-directory entry))))
(link (format nil "?dir-name=~a" (enough-namestring entry *root*))))
(cl-who:htm
(:tr
(:td
(:svg :class "bi" :width "17" :height "17" :fill "currentColor"
(:use :|xlink:href| "/static/icons/bootstrap-icons.svg#folder"))
(:a :href link
(cl-who:str name))))))
;; ordinary file
(let ((name (file-namestring entry))
(link (format nil "/view?file-name=~a" (enough-namestring entry *root*))))
(cl-who:htm
(:tr
(:td
(:a :href link
(cl-who:str name))))))))))))))
(defun run (&optional (root-dir #p"/home/pi/Downloads/"))
(setf *root* root-dir)
(format t "root directory: ~a" *root*)
(setf hunchentoot:*dispatch-table*
`(hunchentoot:dispatch-easy-handlers
;; /Download/ will serve files in the *root* directory
,(hunchentoot:create-folder-dispatcher-and-handler
*Download* *root*)
,(hunchentoot:create-folder-dispatcher-and-handler
"/static/" *static*)))
(hunchentoot:start (make-instance 'hunchentoot:easy-acceptor
:port 4242)))