]> git.plutz.net Git - serve0/blob - pages/browse.sh
styling for pagination links
[serve0] / pages / browse.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 directory="$(invalidate "${_GET[d]}" '(.*/)?\.\.(/.*)?|' /)"
21 page_link="p=browse&d=$(urlsafe "$directory")&"
22
23 . $_EXEC/pages/common.sh
24
25 [ -d "${treeroot%/}${directory%/}/.index" ] && indexed=true || indexed=false
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   name="$1"
57
58   video="${treeroot%/}${directory%/}/${name}"
59   thumb="${treeroot%/}${directory%/}/.index/${name}.jpg"
60    meta="${treeroot%/}${directory%/}/.index/${name}.meta"
61
62   [ -f "$thumb" ] || genthumb "$video" "$thumb"
63   [ -f "$meta"  ] || genmeta "$video" "$meta"
64
65   { read -r length width height filename
66     read -r tags
67   } <"$meta"
68   read length width height <<-EOF
69         $(validate "$length $width $height" '[0-9]+ [0-9]+ [0-9]+' "0 0 0")
70         EOF
71
72   location="$(attribsafe "${directory%/}/${name}")"
73   thumblocation="$(attribsafe "${directory%/}/.index/${name}.jpg")"
74   display="$(htmlsafe "$name")"
75
76   minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
77
78   cat <<VIDEOend
79     <li id="${location}" class="thumb">
80       <button class="watchlink" name="${location}" formaction="?action=watch&l=${location}">
81         <img src="${thumblocation}" width="212" height="162" alt="Preview not yet available">
82       </button>
83       <h2><a href="?action=watch&l=${location}">${display}</a></h2>
84       
85       <input type="checkbox" name="tagsel" value="${location}">
86       <span class="info property">${minutes}min</span>
87       <span class="info property">${width}x${height}</span>
88       $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
89     </li>
90 VIDEOend
91 }
92
93 selectionlist() {
94   cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
95   cache="${_DATA}/cache/${cachebase}"
96   if [ -s "$cache" -a "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
97     cat "$cache"
98   else
99     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
100     printf '%s\n' "$filter" |tr '^' '\n' \
101     | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
102     | while read each; do
103       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
104                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
105     done
106       
107     genlist \
108     | if [ -n "$search" ] ; then
109       debug "Applying search: ${search}"
110       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
111       | xargs -d '\n' egrep -sil "$search" \
112       | sed -r 's;^.*/;;;s;\.meta$;;'
113     elif [ -n "$filter" ]; then
114       debug "Applying filters: ${filter} ++ ${filterex}"
115       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
116       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
117     else
118       debug "No search or filtering"
119       cat
120     fi \
121     | tee "$cache"
122   fi
123 }
124
125 filelist(){
126   dir="${treeroot%/}${directory%/}"
127
128   case "$order" in
129     Name)
130       find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
131       | sort -fz \
132       | xargs -0 printf 'directory %s\n'
133
134       find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
135       | grep -ziE "$file_pattern" \
136       | sort -fz \
137       | xargs -0 printf 'video %s\n'
138
139       find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
140       | grep -zviE "$file_pattern" \
141       | sort -fz \
142       | xargs -0 printf 'plain %s\n'
143       ;;
144     Date)
145       find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
146       | xargs -0 stat -c '%Z directory %n' \
147       | sort -r |cut -c12-
148
149       find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
150       | grep -ziE "$file_pattern" \
151       | xargs -0 stat -c '%Y video %n' \
152       | sort -r |cut -c12-
153
154       find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
155       | grep -zviE "$file_pattern" \
156       | xargs -0 stat -c '%Y plain %n' \
157       | sort -r |cut -c12-
158       ;;
159     Length)
160       find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -print0 \
161       | sort -z \
162       | xargs -0 printf 'directory %s\n'
163
164       head -qn1 "${dir}"/.index/*.meta \
165       | sort \
166       | cut -f4- \
167       | tr \\n \\0 \
168       | xargs -0 printf 'video %s\n'
169
170       find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -print0 \
171       | grep -zviE "$file_pattern" \
172       | sort -z \
173       | xargs -0 printf 'plain %s\n'
174       ;;
175     esac
176 }
177
178 thumblist(){
179   debug "Filelist with dir = $directory"
180
181   [ -n "${directory#/}" ] && printf %s "<a class='file directory' href='?p=browse&d=$(urlsafe "${directory%/*}")'>..</a>"
182
183   filelist \
184   | sed -n "$page,$(($page + $pagesize - 1))p" \
185   | while read -r type name; do
186     case $type in
187       directory)
188         printf '<a class="file directory" href="?p=browse&d=%s">%s</a>\n' \
189                "$(urlsafe "${directory%/}/${name##*/}")" "$(htmlsafe "${name##*/}")"
190         ;;
191       video)
192         $indexed && _printVideo "${name##*/}" \
193         || printf '<a class="file video" href="?action=watch&l=%s">%s</a>\n' \
194                   "$(urlsafe "${directory%/}/${name##*/}")" "$(htmlsafe "${name##*/}")"
195         ;;
196       plain)
197         printf '<span class="file plain">%s</span>\n' "$(htmlsafe "${name##*/}")"
198         ;;
199     esac
200   done
201 }
202
203 pagecount() {
204   printf "$(( $(filelist | wc -l) / $pagesize + 1 ))"
205 }