-#!/bin/zsh
-
-# Copyright 2014 - 2017 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/>.
-
-directory="$(invalidate "${_GET[d]}" '(.*/)?\.\.(/.*)?|' /)"
-page_link="p=browse&d=$(urlsafe "$directory")&"
-
-. $_EXEC/pages/common.sh
-
-[ -d "${treeroot%/}${directory%/}/.index" ] && indexed=true || indexed=false
-filters="${_GET[f]}"
-debug "FILTERS: $filters"
-
-present_filter(){
- printf %s\\n "$*" \
- | sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^;\n;g' \
- | sort -r \
- | sed -r '$q;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
-
-category_selected(){
- cat="${1}:"
- filter="$2"
- [ "$cat" = "none:" ] && cat=''
-
- printf %s "$taglist_filter" \
- | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
- | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
- | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
-}
-
-_printVideo(){
- name="$1"
-
- video="${treeroot%/}${directory%/}/${name}"
- thumb="${treeroot%/}${directory%/}/.index/${name}.jpg"
- meta="${treeroot%/}${directory%/}/.index/${name}.meta"
-
- [ -f "$thumb" ] || genthumb "$video" "$thumb"
- [ -f "$meta" ] || genmeta "$video" "$meta"
-
- { read -r length width height filename
- read -r tags
- } <"$meta"
- read length width height <<-EOF
- $(validate "$length $width $height" '[0-9]+ [0-9]+ [0-9]+' "0 0 0")
- EOF
-
- location="$(attribsafe "${directory%/}/${name}")"
- thumblocation="$(attribsafe "${directory%/}/.index/${name}.jpg")"
- display="$(htmlsafe "$name")"
-
- minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
-
- cat <<VIDEOend
- <li id="${location}" class="thumb">
- <button class="watchlink" name="${location}" formaction="?action=watch&l=${location}">
- <img src="${thumblocation}" width="212" height="162" alt="Preview not yet available">
- </button>
- <h2><a href="?action=watch&l=${location}">${display}</a></h2>
-
- <input type="checkbox" name="tagsel" value="${location}">
- <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
-}
-
-selectionlist() {
- cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
- cache="${_DATA}/cache/${cachebase}"
- if [ -s "$cache" -a "$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 \
- | tee "$cache"
- fi
-}
-
-filelist(){
- dir="${treeroot%/}${directory%/}"
-
- case "$order" in
- Name)
- find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
- | sort -fz \
- | xargs -0 printf 'directory %s\n'
-
- find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
- | grep -ziE "$file_pattern" \
- | sort -fz \
- | xargs -0 printf 'video %s\n'
-
- find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
- | grep -zviE "$file_pattern" \
- | sort -fz \
- | xargs -0 printf 'plain %s\n'
- ;;
- Date)
- find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
- | xargs -0 stat -c '%Z directory %n' \
- | sort -r |cut -c12-
-
- find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
- | grep -ziE "$file_pattern" \
- | xargs -0 stat -c '%Y video %n' \
- | sort -r |cut -c12-
-
- find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
- | grep -zviE "$file_pattern" \
- | xargs -0 stat -c '%Y plain %n' \
- | sort -r |cut -c12-
- ;;
- Length)
- find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
- | sort -z \
- | xargs -0 printf 'directory %s\n'
-
- head -qn1 "${dir}"/.index/*.meta \
- | sort \
- | cut -f4- \
- | tr \\n \\0 \
- | xargs -0 printf 'video %s\n'
-
- find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
- | grep -zviE "$file_pattern" \
- | sort -z \
- | xargs -0 printf 'plain %s\n'
- ;;
- esac
-}
-
-thumblist(){
- debug "Filelist with dir = $directory"
-
- [ -n "${directory#/}" ] && printf %s "<a class='file directory' href='?p=browse&d=$(urlsafe "${directory%/*}")'>..</a>"
-
- filelist \
- | sed -n "$page,$(($page + $pagesize - 1))p" \
- | while read -r type name; do
- case $type in
- directory)
- printf '<a class="file directory" href="?p=browse&d=%s">%s</a>\n' \
- "$(urlsafe "${directory%/}/${name##*/}")" "$(htmlsafe "${name##*/}")"
- ;;
- video)
- $indexed && _printVideo "${name##*/}" \
- || printf '<a class="file video" href="?action=watch&l=%s">%s</a>\n' \
- "$(urlsafe "${directory%/}/${name##*/}")" "$(htmlsafe "${name##*/}")"
- ;;
- plain)
- printf '<span class="file plain">%s</span>\n' "$(htmlsafe "${name##*/}")"
- ;;
- esac
- done
-}
-
-pagecount() {
- printf "$(( $(filelist | wc -l) / $pagesize + 1 ))"
-}