]> git.plutz.net Git - serve0/blob - pages/list.sh
added licence headers, coding style improvements
[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 quicklinks(){
28   output=''
29   tac ${_DATA}/meta/recent \
30   | while [ "$(printf %s "$output" |wc -l)" -lt 10 ] && read line; do
31     line="<a href="$line">$line</a>" 
32     printf %s "$output" |grep -qF "$line" || output="$output$line$LF"
33   done 
34   printf %s\\n "$output"
35 }
36
37 category_selected(){
38   cat="${1}:"
39   filter="$2"
40   [ "$cat" = "none:" ] && cat=''
41
42   printf %s "$taglist" \
43   | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
44   | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
45   | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
46 }
47
48 _printVideo(){
49   info="$1"
50   cache="${_DATA}/cache/$info.cache"
51   meta="${_DATA}/meta/$info.meta"
52
53   if [ "$cache" -nt "$meta" ]; then
54     cat "$cache"
55   else
56     videofile="${_DATA}/videos/$info"
57     thumb="${_DATA}/thumbs/${info}.jpg"
58     [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
59     [ -r "$meta"  ] || genmeta "$videofile" "$meta"
60
61     head -n1 "$meta" |read length width height filename
62     length=$(validate "$length" '[0-9]+' 0)
63     width=$(validate "$width" '[0-9]+' 0)
64     height=$(validate "$height" '[0-9]+' 0)
65
66     tags="$(sed -n 2p "$meta")"
67     video="$(urlsafe "videos/$info")"
68     thumb="$(urlsafe "thumbs/$info.jpg")"
69     linkinfo="$(urlsafe "$info")"
70     htmlinfo="$(htmlsafe "$info")"
71
72     [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
73                                    || minutes="$(($length / 60)):$(($length % 60))"
74
75     tee "$cache" <<VIDEOend
76       <li class="thumb">
77         <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
78           <img src="$thumb" alt="Preview not yet available">
79         </a>
80         <h2>${htmlinfo}</h2>
81         
82         <input type="checkbox" name="tagsel" value="${htmlinfo}">
83         <span class="info property">${minutes}min</span>
84         <span class="info property">${width}x${height}</span>
85         $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
86       </li>
87 VIDEOend
88   fi
89 }
90
91 genlist(){
92   case "$order" in
93     Date)   ls -c "${_DATA}"/videos/ |egrep -i "$file_pattern";;
94     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
95     Name)   printf '%s\n' "${_DATA}"/videos/* |sed -r 's;^.*/;;;';;
96   esac
97 }
98
99 thumblist() {
100   cache="${_DATA}/cache/${pagesize}?o=${order}&s=${search}&f=${filter}&pn=${page}"
101   if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
102     cat "$cache"
103   else
104     filterex="s;^([0-9]+\t){3}(.+)\n.*$;\2;p"
105     printf '%s\n' "$filter" |tr '^' '\n' \
106     | while read each; do
107       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
108                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
109     done
110       
111     genlist \
112     | if [ -n "$search" ] ; then
113       debug "Applying search: ${search}"
114       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
115       | xargs -d '\n' egrep -sil "$search" \
116       | sed -r 's;^.*/;;;s;\.meta$;;'
117     elif [ -n "$filter" ]; then
118       debug "Applying filters: ${filter} ++ ${filterex}"
119       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
120       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
121     else
122       debug "No search or filtering"
123       cat
124     fi \
125     | sed -n "$page,$(($page + $pagesize - 1))p" \
126     | tee "$cache"
127   fi \
128   | while read line; do
129     _printVideo "$line"
130   done
131 }