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