#!/bin/zsh
-# Copyright 2014, 2015 Paul Hänsch
+# Copyright 2014 - 2016 Paul Hänsch
#
# This file is part of Serve0.
#
# You should have received a copy of the GNU Affero General Public License
# along with Serve0. If not, see <http://www.gnu.org/licenses/>.
-cgi_cookie
. $_EXEC/pages/common.sh
-filters="$(printf %s "${_GET[f]}" |sed -r 's;^!*\^*;;; s;\^*!*$;;; s;!+;!;g; s;\^+;^;g')"
+LF='
+'
+filters="${_GET[f]}"
debug "FILTERS: $filters"
+present_filter(){ printf %s "$*" |sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^; \&\; ;g' }
+
+if [ -n "$search" ]; then
+ TITLE="$search by $order"
+elif [ -n $filter ]; then
+ TITLE="$(present_filter "$filter") by $order"
+else
+ TITLE="List by $order"
+fi
+
quicklinks(){
output=''
- tac ${_DATA}/meta/recent |while [ "$(echo "$output" |wc -l)" -lt 10 ] && read line; do
- line="<a href="$line">$line</a>"
- echo "$output" |grep -qF "$line" || output="$output$line\n"
+ tac ${_DATA}/meta/recent \
+ | while [ "$(printf %s "$output" |wc -l)" -lt 10 ] && read line; do
+ printf %s "$output" |grep -qF "$line" || output="$output$line$LF"
done
- echo $output
-}
-
-list_categories(){
- echo none
- printf %s "$taglist" |sed -rn 's;^([^:]+):.*$;\1;p' |sort -u
+ printf %s "$output" |while read line; do
+ linef=$(printf %s "$line" |sed -r 's;^.*(\?|&)f=([^&]+)&?.*$;\2;g')
+ lineo=$(printf %s "$line" |sed -r 's;^.*(\?|&)o=([^&]+)&?.*$;\2;g')
+ printf %s "<a href="$line">by ${lineo}: $(present_filter "$linef")</a>"
+ done
}
category_selected(){
| sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
}
-case "$1" in
- title)
- echo "Videos"
- ;;
- css)
- . ${_EXEC}/templates/common.css.sh
- ;;
- body)
- . ${_EXEC}/templates/list.html.sh
- ;;
-esac
+_printVideo(){
+ info="$1"
+ cache="${_DATA}/cache/$info.cache"
+ meta="${_DATA}/meta/$info.meta"
+
+ if [ "$cache" -nt "$meta" ]; then
+ cat "$cache"
+ else
+ videofile="${_DATA}/videos/$info"
+ thumb="${_DATA}/thumbs/${info}.jpg"
+ [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
+ [ -r "$meta" ] || genmeta "$videofile" "$meta"
+
+ head -n1 "$meta" |read length width height filename
+ length=$(validate "$length" '[0-9]+' 0)
+ width=$(validate "$width" '[0-9]+' 0)
+ height=$(validate "$height" '[0-9]+' 0)
+
+ tags="$(sed -n 2p "$meta")"
+ video="$(urlsafe "videos/$info")"
+ thumb="$(urlsafe "thumbs/$info.jpg")"
+ linkinfo="$(urlsafe "$info")"
+ htmlinfo="$(htmlsafe "$info")"
+
+ [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
+ || minutes="$(($length / 60)):$(($length % 60))"
+
+ tee "$cache" <<VIDEOend
+ <li class="thumb">
+ <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
+ <img src="$thumb" alt="Preview not yet available">
+ </a>
+ <h2>${htmlinfo}</h2>
+
+ <input type="checkbox" name="tagsel" value="${htmlinfo}">
+ <span class="info property">${minutes}min</span>
+ <span class="info property">${width}x${height}</span>
+ $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
+ </li>
+VIDEOend
+ fi
+}
+
+genlist(){
+ case "$order" in
+ Date) ls -c "${_DATA}"/videos/ |egrep -i "$file_pattern";;
+ Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
+ Name) printf '%s\n' "${_DATA}"/videos/* |sed -r 's;^.*/;;;';;
+ esac
+}
+
+thumblist() {
+ cachebase="${pagesize}?o=${order}&s=${search//\//}&f=${filter//\//}&pn=${page}"
+ cache="${_DATA}/cache/${cachebase}"
+ if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
+ cat "$cache"
+ else
+ filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
+ printf '%s\n' "$filter" |tr '^' '\n' \
+ | sed -r 's;[]\/\(\)\\\^\$\|\?\.\+\*\;\[\{\}];\\\\&;g' \
+ | while read each; do
+ [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
+ || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
+ done
+
+ genlist \
+ | if [ -n "$search" ] ; then
+ debug "Applying search: ${search}"
+ xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
+ | xargs -d '\n' egrep -sil "$search" \
+ | sed -r 's;^.*/;;;s;\.meta$;;'
+ elif [ -n "$filter" ]; then
+ debug "Applying filters: ${filter} ++ ${filterex}"
+ xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
+ | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
+ else
+ debug "No search or filtering"
+ cat
+ fi \
+ | sed -n "$page,$(($page + $pagesize - 1))p" \
+ | tee "$cache"
+ fi \
+ | while read line; do
+ _printVideo "$line"
+ done
+}