]> git.plutz.net Git - serve0/blob - pages/browse.sh
bugfix, base links in paginated browse pages
[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 . $_EXEC/pages/common.sh
21
22 LF='
23 '
24 directory="$(invalidate "${_GET[d]}" '(.*/)?\.\.(/.*)?|' /)"
25 [ -d "${treeroot}/${directory#/}/.index" ] && indexed=true || indexed=false
26 filters="${_GET[f]}"
27 debug "FILTERS: $filters"
28
29 page_link="p=browse&d=$(urlsafe "$directory")&${page_link}"
30
31 present_filter(){
32   printf %s\\n "$*" \
33   | sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^;\n;g' \
34   | sort -r \
35   | sed -r '$q;s;$; \&amp\; ;g'
36 }
37
38 if [ -n "$search" ]; then
39   TITLE="$search by $order"
40 elif [ -n $filter ]; then
41   TITLE="$(present_filter "$filter") by $order"
42 else
43   TITLE="List by $order"
44 fi
45
46 category_selected(){
47   cat="${1}:"
48   filter="$2"
49   [ "$cat" = "none:" ] && cat=''
50
51   printf %s "$taglist_filter" \
52   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
53   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
54   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
55 }
56
57 _printVideo(){
58   name="$1"
59
60   video="${treeroot%/}/${directory%/}/${name}"
61   thumb="${treeroot%/}/${directory%/}/.index/${name}.jpg"
62    meta="${treeroot%/}/${directory%/}/.index/${name}.meta"
63
64   [ -f "$thumb" ] || genthumb "$video" "$thumb"
65   [ -f "$meta"  ] || genmeta "$video" "$meta"
66
67   { read -r length width height filename
68     read -r tags
69   } <"$meta"
70   read length width height <<-EOF
71         $(validate "$length $width $height" '[0-9]+ [0-9]+ [0-9]+' "0 0 0")
72         EOF
73
74   location="$(attribsafe "${directory%/}/${name}")"
75   thumblocation="$(attribsafe "${directory%/}/.index/${name}.jpg")"
76   display="$(htmlsafe "$name")"
77
78   minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
79
80   cat <<VIDEOend
81     <li id="${location}" class="thumb">
82       <button class="watchlink" name="${location}" formaction="?action=watch&l=${location}">
83         <img src="${thumblocation}" alt="Preview not yet available">
84       </button>
85       <h2><a href="?action=watch&l=${location}">${display}</a></h2>
86       
87       <input type="checkbox" name="tagsel" value="${location}">
88       <span class="info property">${minutes}min</span>
89       <span class="info property">${width}x${height}</span>
90       $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
91     </li>
92 VIDEOend
93 }
94
95 genlist(){
96   case "$order" in
97     Date)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf '%T@ %f\n' |sort -r |cut -d\  -f2- |egrep -i "$file_pattern";;
98     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
99     Name)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf     '%f\n' |sort |egrep -i "$file_pattern";;
100   esac
101 }
102
103 selectionlist() {
104   cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
105   cache="${_DATA}/cache/${cachebase}"
106   if [ -s "$cache" -a "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
107     cat "$cache"
108   else
109     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
110     printf '%s\n' "$filter" |tr '^' '\n' \
111     | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
112     | while read each; do
113       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
114                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
115     done
116       
117     genlist \
118     | if [ -n "$search" ] ; then
119       debug "Applying search: ${search}"
120       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
121       | xargs -d '\n' egrep -sil "$search" \
122       | sed -r 's;^.*/;;;s;\.meta$;;'
123     elif [ -n "$filter" ]; then
124       debug "Applying filters: ${filter} ++ ${filterex}"
125       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
126       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
127     else
128       debug "No search or filtering"
129       cat
130     fi \
131     | tee "$cache"
132   fi
133 }
134
135 filelist(){
136   dir="${treeroot%/}/${directory#/}"
137
138   find -L "$dir" -mindepth 1 -maxdepth 1 -type d \! -name ".index" -printf "directory %P\n" \
139   | sort
140   find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -printf "video %P\n" \
141   | grep -iE "$file_pattern" \
142   | sort
143   find -L "$dir" -mindepth 1 -maxdepth 1 \! -type d -printf "plain %P\n" \
144   | grep -viE "$file_pattern" \
145   | sort
146 }
147
148 thumblist(){
149   debug "Filelist with dir = $directory"
150
151   [ -n "${directory#/}" ] && printf %s "<a class='file directory' href='?p=browse&d=$(urlsafe "${directory%/*}")'>..</a>"
152
153   filelist \
154   | sed -n "$page,$(($page + $pagesize - 1))p" \
155   | while read -r type name; do
156     case $type in
157       directory)
158         printf '<a class="file directory" href="?p=browse&d=%s">%s</a>\n' \
159                "$(urlsafe "${directory%/}/$name")" "$(htmlsafe "$name")"
160         ;;
161       video)
162         $indexed && _printVideo "$name" \
163         || printf '<a class="file video" href="?action=watch&l=%s">%s</a>\n' \
164                   "$(urlsafe "${directory%/}/$name")" "$(htmlsafe "$name")"
165         ;;
166       plain)
167         printf '<span class="file plain">%s</span>\n' "$(htmlsafe "$name")"
168         ;;
169     esac
170   done
171 }
172
173 pagecount() {
174   printf "$(( $(filelist | wc -l) / $pagesize + 1 ))"
175 }