]> git.plutz.net Git - serve0/blob - pages/list.sh
37f764e4bf15979d809a53d3bf91512929574ea7
[serve0] / pages / list.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2016 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 filters="${_GET[f]}"
25 debug "FILTERS: $filters"
26
27 present_filter(){
28   printf %s\\n "$*" \
29   | sed -r 's;(^|!|\^|\|)([^!\^\|:]+:);\1;g;s;\^;\n;g' \
30   | sort -r \
31   | sed -r '$q;s;$; \&amp\; ;g'
32 }
33
34 if [ -n "$search" ]; then
35   TITLE="$search by $order"
36 elif [ -n $filter ]; then
37   TITLE="$(present_filter "$filter") by $order"
38 else
39   TITLE="List by $order"
40 fi
41
42 category_selected(){
43   cat="${1}:"
44   filter="$2"
45   [ "$cat" = "none:" ] && cat=''
46
47   printf %s "$taglist_filter" \
48   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
49   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
50   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
51 }
52
53 _printVideo(){
54   info="$1"
55   cache="${_DATA}/cache/$info.cache"
56   meta="${_DATA}/meta/$info.meta"
57
58   if [ "$cache" -nt "$meta" ]; then
59     cat "$cache"
60   else
61     videofile="${_DATA}/videos/$info"
62     thumb="${_DATA}/thumbs/${info}.jpg"
63     [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
64     [ -r "$meta"  ] || genmeta "$videofile" "$meta"
65
66     head -n1 "$meta" |read length width height filename
67     length=$(validate "$length" '[0-9]+' 0)
68     width=$(validate "$width" '[0-9]+' 0)
69     height=$(validate "$height" '[0-9]+' 0)
70
71     tags="$(sed -n 2p "$meta")"
72     video="$(urlsafe "videos/$info")"
73     thumb="$(urlsafe "thumbs/$info.jpg")"
74     linkinfo="$(urlsafe "$info")"
75     htmlinfo="$(htmlsafe "$info")"
76     attrinfo="$(attribsafe "$info")"
77
78     minutes="$(printf "%d:%02d" $(($length / 60)) $(($length % 60)) )"
79
80     tee "$cache" <<VIDEOend
81       <li id="$attrinfo" class="thumb">
82         <button class="watchlink" name="${linkinfo}" formaction="?action=watch&i=${linkinfo}">
83           <img src="$thumb" alt="Preview not yet available">
84         </button>
85         <h2><a href="?action=watch&i=${linkinfo}">${htmlinfo}</a></h2>
86         
87         <input type="checkbox" name="tagsel" value="${htmlinfo}">
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   fi
94 }
95
96 genlist(){
97   case "$order" in
98     Date)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf '%T@ %f\n' |sort -r |cut -d\  -f2- |egrep -i "$file_pattern";;
99     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
100     Name)   find "${_DATA}"/videos/ -maxdepth 1 -mindepth 1 -printf     '%f\n' |sort |egrep -i "$file_pattern";;
101   esac
102 }
103
104 selectionlist() {
105   cachebase="?o=${order}&s=${search//\//}&f=${filter//\//}"
106   cache="${_DATA}/cache/${cachebase}"
107   if [ -s "$cache" -a "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
108     cat "$cache"
109   else
110     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
111     printf '%s\n' "$filter" |tr '^' '\n' \
112     | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
113     | while read each; do
114       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
115                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
116     done
117       
118     genlist \
119     | if [ -n "$search" ] ; then
120       debug "Applying search: ${search}"
121       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
122       | xargs -d '\n' egrep -sil "$search" \
123       | sed -r 's;^.*/;;;s;\.meta$;;'
124     elif [ -n "$filter" ]; then
125       debug "Applying filters: ${filter} ++ ${filterex}"
126       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
127       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
128     else
129       debug "No search or filtering"
130       cat
131     fi \
132     | tee "$cache"
133   fi
134 }
135
136 thumblist() {
137   selectionlist \
138   | sed -n "$page,$(($page + $pagesize - 1))p" \
139   | while read line; do
140     _printVideo "$line"
141   done
142 }
143
144 pagecount() {
145   printf "$(( $(selectionlist | wc -l) / $pagesize + 1 ))"
146 }