]> git.plutz.net Git - serve0/blob - pages/common.sh
stop mplayer helpers from consuming stdin, remove dependency on `at`
[serve0] / pages / common.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 filter="${_GET[f]}"
21 search="${_GET[s]}"
22 order="${_GET[o]}" #validated
23 page="${_GET[pn]}" #validated
24 pagesize="${_COOKIE[pagesize]}" #validated
25
26 page_link=''
27 [ -n "$info" ]   && page_link="i=$(urlsafe "$info")&"
28 [ -n "$order" ]  && page_link="${page_link}o=${order}&"
29 [ -n "$search" ] && page_link="${page_link}s=${search}&"
30 [ -n "$filter" ] && page_link="${page_link}f=${filter}&"
31 page_prev="${page_link}pn=$([ "$page" -le "$pagesize" ] && echo 1 || echo $(($page - $pagesize)) )"
32 page_next="${page_link}pn=$(($page + $pagesize))"
33 page_this="${page_link}pn=$page"
34
35 taglist=$(
36   [ -r ${_DATA}/cache/taglist ] && cat ${_DATA}/cache/taglist ||\
37   for each in ${_DATA}/meta/*.meta; do
38     sed -n '2s:|:\n:gp' "$each"
39   done |sort -u |grep -xv '' |tee ${_DATA}/cache/taglist
40 )
41
42 genthumb(){
43   videofile="$1"
44   thumb="$2"
45
46   nohup nice -10 "${_EXEC}"/helpers/genthumb.sh "$videofile" "$thumb" >/dev/null 2>/dev/null &
47   #printf '%s\n' "${_EXEC}/helpers/genthumb.sh '$videofile' '$thumb' >/dev/null 2>/dev/null" \
48   #| batch
49 }
50 genmeta(){
51   videofile="$1"
52   meta="$2"
53
54   "${_EXEC}"/helpers/genmeta.sh "$videofile" "$meta" >/dev/null
55 }
56
57 _printVideo(){
58   info="$1"
59   cache="${_DATA}/cache/$info.cache"
60   meta="${_DATA}/meta/$info.meta"
61
62   if [ "$cache" -nt "$meta" ]; then
63     cat "$cache"
64   else
65     videofile="${_DATA}/videos/$info"
66     thumb="${_DATA}/thumbs/${info}.jpg"
67     [ -r "$thumb" ] || genthumb "$videofile" "$thumb"
68     [ -r "$meta"  ] || genmeta "$videofile" "$meta"
69
70     head -n1 "$meta" |read length width height filename
71     [ -z "$length" ] && length=0
72     [ -z "$width"  ] && width=0
73     [ -z "$height" ] && height=0
74
75     tags="$(sed -n 2p "$meta")"
76     video="$(urlsafe "videos/$info")"
77     thumb="$(urlsafe "thumbs/$info.jpg")"
78     linkinfo="$(urlsafe "$info")"
79     htmlinfo="$(htmlsafe "$info")"
80
81     [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
82                                    || minutes="$(($length / 60)):$(($length % 60))"
83
84     tee "$cache" <<VIDEOend
85       <li class="thumb">
86         <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
87           <img src="$thumb" alt="Preview not yet available">
88         </a>
89         <h2>${htmlinfo}</h2>
90         
91         <input type="checkbox" name="tagsel" value="${htmlinfo}">
92         <span class="info property">${minutes}min</span>
93         <span class="info property">${width}x${height}</span>
94         $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*: <span class="info tag">\1</span>:g')
95       </li>
96 VIDEOend
97   fi
98 }
99
100 genlist(){
101   case "$order" in
102     Date)   ls -c "${_DATA}"/videos/ |egrep -i "$file_pattern";;
103     Length) sed -sn 1p "${_DATA}"/meta/*.meta |sort -n |sed -r 's;^[0-9\t]+\t;;';;
104     Name)   printf '%s\n' "${_DATA}"/videos/* |sed -r 's;^.*/;;;';;
105   esac
106 }
107
108 thumblist() {
109   cache="${_DATA}/cache/${pagesize}?o=${order}&s=${search}&f=${filter}&pn=${page}"
110   if [ "$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     | while read each; do
116       [ "${each:0:1}" = '!' ] && filterex="/^[^\n]+\n(.*\|)?(${each#?})(\|.*)?$/d;{${filterex}}" \
117                               || filterex="/^[^\n]+\n(.*\|)?(${each})(\|.*)?$/{${filterex}}"
118     done
119       
120     genlist \
121     | if [ -n "$search" ] ; then
122       debug "Applying search: ${search}"
123       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
124       | xargs -d '\n' egrep -sil "$search" \
125       | sed -r 's;^.*/;;;s;\.meta$;;'
126     elif [ -n "$filter" ]; then
127       debug "Applying filters: ${filter} ++ ${filterex}"
128       xargs -d '\n' printf "${_DATA}/meta/%s.meta\n" \
129       | xargs -d '\n' sed -srn ":a;N;2!ba;{${filterex}}"
130     else
131       debug "No search or filtering"
132       cat
133     fi \
134     | sed -n "$page,$(($page + $pagesize - 1))p" \
135     | tee "$cache"
136   fi \
137   | while read line; do
138     _printVideo "$line"
139   done
140 }