+#!/bin/zsh
+
+# Copyright 2014 Paul Hänsch
+#
+# This file is part of Serve0.
+#
+# Serve0 is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Serve0 is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Serve0. If not, see <http://www.gnu.org/licenses/>.
+
+filterdiag() {
+ . ${_EXEC}/templates/filterdiag.html.sh
+}
+
+tagger() {
+ n=foot
+ hideall="$(sed -rn "s;^([^:]+):.*$;hide_inline('${n}_\\1')\\;;p" <<<"$taglist" |sort -u)hide_inline('${n}_none');"
+
+ . ${_EXEC}/templates/tagger.html.sh
+}
+
+view() {
+ meta="${_DATA}/meta/$info.meta"
+ video="${_DATA}/videos/$info"
+ #reenc="vp8/$(sed -r 's:^(.*)\.[a-zA-Z0-9]{3,4}$:\1:' <<<"$info" )"
+ #[ -f "$reenc.mp4" ] && reenc="$reenc.mp4" || reenc="$reenc.mkv"
+ tags="$(sed -n '2p' "$meta")"
+ description="$(sed -n '3,$p' "$meta")"
+ length=0; width=0; height=0;
+ eval $(mplayer -noconsolecontrols -input file=/dev/null -slave -really-quiet -identify -frames 0 -ao null -vo null "$video" 2>/dev/null |sed -rn 's:ID_VIDEO_WIDTH=(.*):width=\1:p;s:ID_VIDEO_HEIGHT=(.*):height=\1:p;s:ID_LENGTH=(.*)(\..*)$:length=\1:p;')
+
+ taglist=$(
+ for each in ${_DATA}/meta/*.meta; do
+ sed -n '2s:|:\n:gp' "$each"
+ done |sort -u |grep -xv '')
+
+ . ${_EXEC}/templates/view.html.sh
+}
+
+_printVideo(){
+ info="$1"
+ meta="${_DATA}/meta/$info.meta"
+ tags="$(sed -n 2p "$meta")"
+ thumb="${_DATA}/thumbs/$info.jpg"
+ head -n1 "$meta" |read length width height
+ [ -z "$length" ] && length=0
+ [ -z "$width" ] && width=0
+ [ -z "$height" ] && height=0
+
+ video="${_DATA}/videos/$info"
+# reenc="vp8/$(sed -r 's:^(.*)\.[a-zA-Z0-9]{3,4}$:\1:' <<<"$info" )"
+ mpfake="${_DATA}/mp4/$(sed -r 's:^(.*)\.[a-zA-Z0-9]{3,4}$:\1.mp4:' <<<"$info" )"
+# [ -f "$reenc.mp4" ] && reenc="$reenc.mp4" || reenc="$reenc.mkv"
+ [ -r "$meta" ] || ./genmeta.sh "$video" "$meta"
+ [ -r "$thumb" ] || ./genthumb.sh "$video" "$thumb"
+ [ -L "$mpfake" ] || ln -s "../$video" "$mpfake"
+# anchor=$(ls videos |grep -xA2 "$info" |tail -n1)
+ video="$(echo "$video" |sed 's/\?/\%6F/g')"
+ thumb="$(echo "$thumb" |sed 's/\?/%6F/g')"
+ mpfake="$(echo "$mpfake" |sed 's/\?/%6F/g')"
+
+ [ "$(($length % 60))" -lt 10 ] &&\
+ minutes="$(($length / 60)):0$(($length % 60))" ||\
+ minutes="$(($length / 60)):$(($length % 60))"
+
+ cat <<VIDEOend
+ <div class="thumb">
+ <a name="$info" href="?i=$info&p=$page&f=$filter&s=$search&o=$order#$anchor"><img src="$thumb"></a>
+ <div class="thumbinfo">
+ <a href="$video"><b>$info</b></a><br>
+ <!-- a href="$reenc">VP8</a -->
+ <a href="$mpfake">fake-mp4</a><br>
+ <input type="checkbox" name="tagsel" value="$info">
+ <span class="coord">${minutes}min</span>
+ <span class="coord">${width}x${height}</span><br>
+ <!--a class="function" href='write/lock.cgi?p=Locations&e=$info&i=$info&f=$filter#$anchor'>edit</a-->
+ <span class="tag">$(sed 's,|$,,;s,|,</span> <span class="tag">,g' <<<"$tags")</span>
+ </div>
+ </div>
+VIDEOend
+}
+
+_by_name(){ ls ${_DATA}/videos/ }
+_by_date(){ ls -c ${_DATA}/videos/ }
+_by_length(){ ls ${_DATA}/by_length/ |sed -r 's:[0-9]{5} - ::g' }
+
+thumblist() {
+ cache="${_DATA}/cache/?o=${order}&s=${search}&f=${filter}&p=${page}"
+ [ -f "$cache" ] && [ "$cache" -ot ${_DATA}/videos -o "$cache" -ot ${_DATA}/meta ] && rm "$cache"
+ if [ -r "$cache" ]; then
+ cat "$cache"
+ exit 0
+ fi
+
+ filterex="p"
+ for each in $(sed 's,\^, ,g' <<<"$filter"); do
+ if egrep -q '^!' <<<"$each"; then
+ this="$(sed 's,^!,,' <<<"$each")"
+ filterex="/${this}/q;{${filterex}}"
+ else
+ filterex="/${each}/{${filterex}}"
+ fi
+ done
+
+ case "$order" in
+ Date) genlist="_by_date"
+ ;;
+ Length) genlist="_by_length"
+ ;;
+ *) genlist="_by_name"
+ ;;
+ esac
+
+ $genlist |while read video; do
+ #video="$(basename "$each")"
+ if [ -n "$search" ] ; then
+ egrep -iq "$search" <<<"$video" <"${_DATA}/meta/$video.meta" && (echo "$video" || exit 0)
+ elif [ "$filterex" != p ]; then
+ [ -n "$(sed -rn "2{$filterex}" "${_DATA}/meta/$video.meta")" ] && (echo "$video" || exit 0)
+ else
+ echo "$video" || exit 0
+ fi
+ done |sed -n $(($page * $pagesize + 1)),$(($page * $pagesize + $pagesize))p | while read line; do
+ _printVideo "$line"
+ done |tee "$cache"
+}
+
+case "$1" in
+ title)
+ echo "Videos"
+ ;;
+ css)
+ . ${_EXEC}/templates/videoview.css.sh
+ ;;
+ body)
+ . ${_EXEC}/templates/videoview.html.sh
+ ;;
+esac