]> git.plutz.net Git - serve0/blob - pages/list.sh
styling for pagination links
[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     { tr -d '\n' |tee "$cache"; } <<-VIDEOend
83         <li id="$attrinfo" class="thumb">
84           <button class="watchlink" name="${linkinfo}" formaction="?action=watch&i=${linkinfo}">
85             <img src="$thumb" width="212" height="162" 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)
101       find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf '%i\t%p\n' \
102       | sort -n \
103       | while read i file; do printf '%s\000' "$file"; done \
104       | xargs -0 stat -c '%Y    %n' \
105       | sort -nr \
106       | cut -f2- \
107       | sed 's;^.*/;;g;' \
108       | egrep -i "$file_pattern"
109       ;;
110     Length)
111       sed -sn 1p "${_DATA}"/meta/*.meta \
112       | sort -n \
113       | sed -r 's;^[0-9\t]+\t;;' \
114       | while read file; do
115         [ -f "${_DATA}/videos/${file}" ] && printf '%s\n' "$file"
116       done
117       ;;
118     Name)
119       find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf     '%f\n' \
120       | sort -f \
121       | egrep -i "$file_pattern"
122       ;;
123   esac
124 }
125
126 selectionlist() {
127   cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
128   cache="${_DATA}/cache/${cachebase}"
129   if [ -s "$cache" -a "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
130     cat "$cache"
131   else
132     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
133     printf '%s\n' "$filter" |tr '^' '\n' \
134     | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
135     | while read each; do
136       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
137                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
138     done
139       
140     genlist \
141     | if [ -n "$search" ] ; then
142       debug "Applying search: ${search}"
143       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
144       | xargs -d '\n' egrep -sil "$search" \
145       | sed -r 's;^.*/;;;s;\.meta$;;'
146     elif [ -n "$filter" ]; then
147       debug "Applying filters: ${filter} ++ ${filterex}"
148       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
149       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
150     else
151       debug "No search or filtering"
152       cat
153     fi \
154     | tee "$cache"
155   fi
156 }
157
158 thumblist() {
159   selectionlist \
160   | sed -n "$page,$(($page + $pagesize - 1))p" \
161   | while read line; do
162     _printVideo "$line"
163   done
164 }
165
166 pagecount() {
167   printf "$(( $(selectionlist | wc -l) / $pagesize + 1 ))"
168 }