]> git.plutz.net Git - serve0/blob - pages/common.sh
introduced player preferences
[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=$(echo -E "${_GET[o]}" |egrep '^(Name|Date|Length)$')
23 page=$(echo -E "${_GET[pn]}" |egrep '^[0-9]+$')
24 [ -z "$page" ] && page=0
25
26 page_link=''
27 [ -n "$info" ]   && page_link="i=$(urlsave "$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" = 0 ] && echo 0 || echo $(($page - 1)) )"
32 page_next="${page_link}pn=$(($page + 1))"
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 _printVideo(){
43   info="$1"
44   cache="${_DATA}/cache/$info.cache"
45   meta="${_DATA}/meta/$info.meta"
46
47   if [ "$cache" -nt "$meta" ]; then
48     cat "$cache"
49   else
50     videofile="${_DATA}/videos/$info"
51     thumb="${_DATA}/thumbs/$info.jpg"
52     [ -r "$meta"  ] || ${_EXEC}/helpers/genmeta.sh "$videofile" "$meta"
53     [ -r "$thumb" ] || ${_EXEC}/helpers/genthumb.sh "$videofile" "$thumb"
54
55     head -n1 "$meta" |read length width height filename
56     [ -z "$length" ] && length=0
57     [ -z "$width"  ] && width=0
58     [ -z "$height" ] && height=0
59
60     tags="$(sed -n 2p "$meta")"
61     video="$(urlsave "videos/$info")"
62     thumb="$(urlsave "thumbs/$info.jpg")"
63     linkinfo="$(urlsave "$info")"
64
65     [ "$(($length % 60))" -lt 10 ] && minutes="$(($length / 60)):0$(($length % 60))" \
66                                    || minutes="$(($length / 60)):$(($length % 60))"
67
68     tee "$cache" <<VIDEOend
69       <li class="thumb">
70         <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
71           <img src="$thumb" alt="Preview not yet available">
72         </a>
73         <h2>$info</h2>
74         
75         <input type="checkbox" name="tagsel" value="$info">
76         <span class="info property">${minutes}min</span>
77         <span class="info property">${width}x${height}</span>
78         $(printf %s "$tags" |sed -r 's:\|*([^|]+)\|*:<span class="info tag">\1</span>:g')
79       </li>
80 VIDEOend
81   fi
82 }
83
84 _by_name(){ find ${_DATA}/videos/ -mindepth 1 -maxdepth 1 -printf '%f\n' |sort |egrep -i "$file_pattern" }
85 _by_date(){ ls -c ${_DATA}/videos/ |egrep -i "$file_pattern" }
86 _by_length(){ find ${_DATA}/by_length/ -mindepth 1 -maxdepth 1 -printf '%f\n' |sort |sed -r 's:[0-9]{5} - ::' }
87
88 thumblist() {
89   cache="${_DATA}/cache/?o=${order}&s=${search}&f=${filter}&pn=${page}"
90   if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
91     cat "$cache"
92   else
93     filterex="q0"
94     for each in $(sed 's,\^, ,g' <<<"$filter"); do
95       if egrep -q '^!' <<<"$each"; then
96         this="$(sed 's,^!,,' <<<"$each")"
97         filterex="/${this}/q1;{${filterex}}"
98       else
99         filterex="/${each}/{${filterex}}"
100       fi
101     done
102     
103     case "$order" in
104       Date) genlist="_by_date"
105         ;;
106       Length) genlist="_by_length"
107         ;;
108       *) genlist="_by_name"
109         ;;
110     esac
111       
112     if [ -n "$search" ] ; then
113       #$genlist |egrep -i "$search"
114       $genlist |sed -rn 's:^(.+)$:'"${_DATA}"'/meta/\1'.meta':p' |xargs -d\\n egrep -sil "$search" |sed -rn 's:^(.*/)+([^/]+)\.meta$:\2:p'
115     elif [ "$filterex" != "q0" ]; then
116       $genlist |while read video; do
117         [ -r "${_DATA}/meta/$video.meta" ] && sed -rn "2{$filterex;q1}" "${_DATA}/meta/$video.meta" && printf '%s\n' "$video"
118       done
119     else
120       $genlist
121     fi |sed -n $(($page * $pagesize + 1)),$(($page * $pagesize + $pagesize))p |tee "$cache"
122   fi | while read line; do
123     _printVideo "$line"
124   done
125 }