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