]> git.plutz.net Git - serve0/blob - pages/list.sh
7dbe2d0bcfb449e42ebbec44f2008b07bc6850f9
[serve0] / pages / list.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2017 Paul Hänsch
4 #
5 # This file is part of Serve0.
6
7 # Serve0 is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # Serve0 is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Serve0.  If not, see <http://www.gnu.org/licenses/>. 
19
20 page_link="p=list&"
21
22 . $_EXEC/pages/common.sh
23
24 LF='
25 '
26 filters="${_GET[f]}"
27 debug "FILTERS: $filters"
28
29 present_filter(){
30   printf %s\\n "$*" \
31   | sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^;\n;g' \
32   | sort -r \
33   | sed -r '$q;s;$; \&amp\; ;g'
34 }
35
36 if [ -n "$search" ]; then
37   TITLE="$search by $order"
38 elif [ -n $filter ]; then
39   TITLE="$(present_filter "$filter") by $order"
40 else
41   TITLE="List by $order"
42 fi
43
44 category_selected(){
45   cat="${1}:"
46   filter="$2"
47   [ "$cat" = "none:" ] && cat=''
48
49   printf %s "$taglist_filter" \
50   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
51   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
52   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
53 }
54
55 _printVideo(){
56   info="$1"
57   cache="${_DATA}/cache/$info.cache"
58   meta="${_DATA}/meta/$info.meta"
59
60   if [ "$cache" -nt "$meta" ]; then
61     cat "$cache"
62   else
63     videofile="${_DATA}/videos/$info"
64     thumb="${_DATA}/thumbs/${info}.jpg"
65     [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
66     [ -r "$meta"  ] || genmeta "$videofile" "$meta"
67
68     head -n1 "$meta" |read length width height filename
69     length=$(validate "$length" '[0-9]+' 0)
70     width=$(validate "$width" '[0-9]+' 0)
71     height=$(validate "$height" '[0-9]+' 0)
72
73     tags="$(sed -n 2p "$meta")"
74     video="$(urlsafe "videos/$info")"
75     thumb="$(urlsafe "thumbs/$info.jpg")"
76     linkinfo="$(urlsafe "$info")"
77     htmlinfo="$(htmlsafe "$info")"
78     attrinfo="$(attribsafe "$info")"
79
80     minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
81
82     tee "$cache" <<VIDEOend
83       <li id="$attrinfo" class="thumb">
84         <button class="watchlink" name="${linkinfo}" formaction="?action=watch&i=${linkinfo}">
85           <img src="$thumb" alt="Preview not yet available">
86         </button>
87         <h2><a href="?action=watch&i=${linkinfo}">${htmlinfo}</a></h2>
88         
89         <input type="checkbox" name="tagsel" value="${htmlinfo}">
90         <span class="info property">${minutes}min</span>
91         <span class="info property">${width}x${height}</span>
92         $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
93       </li>
94 VIDEOend
95   fi
96 }
97
98 genlist(){
99   case "$order" in
100     Date)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf '%T@ %f\n' |sort -r |cut -d\  -f2- |egrep -i "$file_pattern";;
101     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
102     Name)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf     '%f\n' |sort -f |egrep -i "$file_pattern";;
103   esac
104 }
105
106 selectionlist() {
107   cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
108   cache="${_DATA}/cache/${cachebase}"
109   if [ -s "$cache" -a "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
110     cat "$cache"
111   else
112     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
113     printf '%s\n' "$filter" |tr '^' '\n' \
114     | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
115     | while read each; do
116       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
117                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
118     done
119       
120     genlist \
121     | if [ -n "$search" ] ; then
122       debug "Applying search: ${search}"
123       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
124       | xargs -d '\n' egrep -sil "$search" \
125       | sed -r 's;^.*/;;;s;\.meta$;;'
126     elif [ -n "$filter" ]; then
127       debug "Applying filters: ${filter} ++ ${filterex}"
128       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
129       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
130     else
131       debug "No search or filtering"
132       cat
133     fi \
134     | tee "$cache"
135   fi
136 }
137
138 thumblist() {
139   selectionlist \
140   | sed -n "$page,$(($page + $pagesize - 1))p" \
141   | while read line; do
142     _printVideo "$line"
143   done
144 }
145
146 pagecount() {
147   printf "$(( $(selectionlist | wc -l) / $pagesize + 1 ))"
148 }