mediabox view index.cgi @ rev 6

Add mediabox cmdline tool and main CGI script
author Christophe Lincoln <pankso@slitaz.org>
date Thu Feb 23 21:48:35 2017 +0100 (2017-02-23)
parents
children 2c076cc8e7cb
line source
1 #!/bin/sh
2 #
3 # CGI/SHell MediaBox using HTML5 features. Coded with lightness and
4 # elegence in mind.
5 #
6 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
7 #
8 . /usr/lib/slitaz/httphelper.sh
9 header
11 #
12 # Internal variables
13 #
14 version="beta"
15 cache="$PWD/cache"
16 script="$SCRIPT_NAME"
18 # Set $home - Cache user ID and source configs
19 if [ "$(GET home)" ]; then
20 echo $(GET home) > ${cache}/home
21 rm -f ${cache}/audio ${cache}/videos
22 fi
23 home=$(cat $cache/home)
24 config="$home/.config/mediabox"
25 if [ -f "${config}/mediabox.conf" ]; then
26 . ${config}/mediabox.conf
27 else
28 gettext "Missing config file:"; echo " $config"; exit 1
29 fi
31 # i18n
32 export LANG=${LOCALE} LC_ALL=${LOCALE}
33 . /usr/bin/gettext.sh
34 export TEXTDOMAIN='mediabox'
36 #
37 # Functions
38 #
40 # Usage: html_header "Page Title"
41 html_header() {
42 cat lib/header.html | sed s"/%TITLE%/$1/"
43 cat << EOT
44 <header>
45 <h1>$1</h1>
46 EOT
47 }
49 html_footer() {
50 cat << EOT
51 <footer>
52 &hearts;
53 </footer>
54 </body>
55 </html>
56 EOT
57 }
59 # Header navigation
60 nav_menu() {
61 cat << EOT
62 <nav>
63 <a href="$script">$(gettext "Home")</a>
64 <a href="$script?music">$(gettext "Music")</a>
65 <a href="$script?videos">$(gettext "Videos")</a>
66 <a href="$script?playlists">$(gettext "Playlists")</a>
67 <a href="$script?radio">$(gettext "Radio")</a>
68 </nav>
69 </header>
70 EOT
71 }
73 # Page navigation
74 nav_page() {
75 cat << EOT
76 </header>
77 <div id="home">
78 <nav >
79 <a href="$script?music">$(gettext "Music")</a>
80 <a href="$script?videos">$(gettext "Videos")</a>
81 <a href="$script?playlists">$(gettext "Playlists")</a>
82 <a href="$script?radio">$(gettext "Radio")</a>
83 <a href="$script?settings">$(gettext "Settings")</a>
84 </nav>
85 </div>
86 EOT
87 }
89 # Find and list audio/videos files.
90 find_audio() {
91 [ ! -f "${cache}/audio" ] && find "${MUSIC}" \
92 -regex '.*\.\(mp3\|ogg\|wav\)' > ${cache}/audio
93 cat ${cache}/audio
94 }
95 find_videos() {
96 find "${VIDEOS}" -regex '.*\.\(mp4\|ogv\|avi\)' > ${cache}/videos
97 cat ${cache}/videos
98 }
100 list_audio() {
101 count="$(wc -l ${cache}/audio | cut -d " " -f 1)"
102 echo "<ul id='audio-list'>"
103 cat << EOT
104 <li><span>&#9835 &#9835 &#9835 $count $(gettext "Tracks found")</span></li>
105 EOT
106 find_audio | while read a
107 do
108 filename="$(basename "${a}")"
109 echo " <li><a href='$script?music&amp;play=${a}'>${filename%.*}</a></li>"
110 done
111 echo "</ul>"
112 }
114 list_videos() {
115 count="$(wc -l ${cache}/videos | cut -d " " -f 1)"
116 cat << EOT
117 <ul id='videos-list'>
118 <li><span>$count $(gettext "Videos found")</span></li>
119 EOT
120 find_videos | while read v
121 do
122 filename="$(basename "${v}")"
123 echo " <li><a href='$script?videos&amp;play=${v}'>${filename%.*}</a></li>"
124 done
125 echo "</ul>"
126 }
128 list_radio() {
129 if [ ! -f "$config/radio.list" ]; then
130 cp lib/radio.list ${config} && chmod 0666 ${config}/radio.list
131 fi
132 echo "<ul id='radio-list'>"
133 IFS="|"
134 cat ${config}/radio.list | while read url info
135 do
136 [ "$info" ] || info="N/A"
137 cat << EOT
138 <li><a href='$script?radio&amp;play=${url}&info=$info'>${url#http://}<span>$info</span></a></li>
139 EOT
140 done
141 unset IFS info
142 echo "</ul>"
143 }
145 list_playlists() {
146 echo "<ul id='radio-list'>"
147 IFS="|"
148 cat ${config}/playlists.list | while read file info
149 do
150 [ "$info" ] || info="N/A"
151 cat << EOT
152 <li><a href='$script?playlists&amp;play=${file}&info=$info'>$info</a></li>
153 EOT
154 done
155 unset IFS info
156 echo "</ul>"
157 }
159 # HTML5 audio/video attributes: autoplay loop controls preload="auto"
160 #
161 # Usage: audio_player [/path/audio.ogg|http://url]
162 audio_player() {
163 case "$1" in
164 http://*)
165 source="$1" title="$2" ;;
166 *)
167 filepath="$1"
168 filename="$(basename "$1")"
169 title="$(gettext "No track playing")"
170 [ "$1" ] && title="${filename%.*}"
171 # We need to get file via http url
172 source="cache/play/$filename"
173 rm -rf ${cache}/play && mkdir ${cache}/play
174 [ "$filename" ] && ln -s "$filepath" "$cache/play/$filename" ;;
175 esac
176 cat << EOT
177 <div id="audio-player">
178 <div id="audio-title">$title</div>
179 <audio controls autoplay="true" preload="auto">
180 <source src="$source">
181 <div>Your browser does not support audio</div>
182 </audio>
183 </div>
184 EOT
185 }
187 playlist_player() {
188 playlist="$1"
189 }
191 # Usage: video_player "/path/video.mp4"
192 video_player() {
193 filepath="$1"
194 filename="$(basename "$1")"
195 title="$(gettext "No video playing")"
196 [ "$1" ] && title="${filename%.*}"
197 # We need to get file via http url
198 rm -rf ${cache}/play && mkdir ${cache}/play
199 [ "$filename" ] && ln -s "$filepath" "$cache/play/$filename"
200 cat << EOT
201 <div id="video-player">
202 <video width="560px" height="315px"
203 controls autoplay="true" poster="images/poster.png">
204 <source src="cache/play/$filename">
205 <div>Unsupported video file format</div>
206 </video>
207 <div id="video-title">$title</div>
208 </div>
209 EOT
210 }
212 #
213 # Media Box Tools
214 #
216 case " $(GET) " in
217 *\ music\ *)
218 html_header "$(gettext "Music")"
219 nav_menu
220 audio_player "$(GET play)"
221 list_audio
222 html_footer ;;
224 *\ videos\ *)
225 html_header "$(gettext "Videos")"
226 nav_menu
227 video_player "$(GET play)"
228 list_videos
229 html_footer ;;
231 *\ playlists\ *)
232 html_header "$(gettext "Playlists")"
233 nav_menu
234 playlist_player "$(GET play)" "$(GET info)"
235 list_playlists
236 html_footer ;;
238 *\ radio\ *)
239 html_header "$(gettext "Radio")"
240 nav_menu
241 audio_player "$(GET play)" "$(GET info)"
242 list_radio
243 html_footer ;;
245 *\ settings\ *)
246 html_header "$(gettext "Settings")"
247 nav_menu
248 cat << EOT
249 <div id="settings">
250 <pre>
251 Version : $version
252 Cache : $(du -sh $cache | cut -d " " -f 1)
253 Language : $LOCALE
254 Config : $config
255 Music : $MUSIC
256 Videos : $VIDEOS
257 EOT
258 echo -n "Tools : "
259 # Only small nd light tools!
260 for tool in mediainfo normalize sox
261 do
262 if [ -x "/usr/bin/$tool" ]; then
263 echo -n "$tool "
264 fi
265 done
266 cat << EOT
269 $(cat README)
270 </pre>
271 EOT
272 # End of <div id="settings">
273 echo "</div>"
274 html_footer ;;
276 *)
277 # Home page
278 html_header "MediaBox"
279 nav_page
280 html_footer ;;
281 esac && exit 0