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