]> git.plutz.net Git - serve0/blob - actions/watch.sh
f79600984ad4dc89a4afd57161f3969a88d96431
[serve0] / actions / watch.sh
1 #!/bin/zsh
2
3 cgi_refdata
4 info="${_GET[i]}"
5 location="$(invalidate "${_GET[l]}" '(.*/)?\.\.(/.*)?|' /)"
6 foreign="$(validate "${_GET[url]}" 'https?://.*' '')"
7 order="${_POST[o]}"
8 search="${_POST[s]}"
9 filter="${_POST[f]}"
10 page="${_POST[pn]}"
11
12 watch_link="${_POST[page]}&"
13 [ -n "$location" ] && watch_link="l=$(urlsafe "${location}")&"
14 [ -n "$info" ]     && watch_link="i=$(urlsafe "${info}")&"
15
16 meta="${_DATA}/meta/$info.meta"
17 head -n1 "$meta" |read length width height filename
18 [ "$length" -gt 0 ] || length=0
19 [ "$width"  -gt 0 ] || width=0
20 [ "$height" -gt 0 ] || height=0
21
22 linkinfo="$(urlsafe "$info")"
23
24 if [ "${_COOKIE[fakemp4]}" = yes ] && [ "${info##*.}" != mp4 ]; then
25   [ -e "${_DATA}/videos/${info%.*}.mp4" ] || ln -s "${_DATA}/videos/$info" "${_DATA}/mp4/${info%.*}.mp4"
26   videourl="$(urlsafe "mp4/${info%.*}.mp4")"
27 elif [ -n "$foreign" ]; then
28   _COOKIE[watch]=server
29   videourl="${_GET[url]}"
30   watch_link="w=$(urlsafe "${_GET[return]}")"
31 elif [ -z "$info" ]; then
32   videourl="$(urlsafe "${location#/}")"
33 else
34   videourl="$(urlsafe "videos/$info")"
35 fi
36
37 case "$_COOKIE[watch]" in
38   raspi)
39     #rpiaddr="http://atoemchen.helmchyn.no-ip.biz/?action=watch"
40     #redirect "${rpiaddr}&url=$(urlsafe "http://${HTTP_HOST}/${videourl}")&return=$(urlsafe "http://${HTTP_HOST}/${_POST[page]}")"
41     ncat atoemchen 80 >&- <<-EOF
42         GET /?action=watch&url=$(urlsafe "http://${HTTP_HOST}/${videourl}") HTTP/1.1$(printf \\r)
43         Host: atoemchen$(printf \\r)
44         $(printf \\r)
45         EOF
46     redirect "?p=playctl&${watch_link}w=$(urlsafe "${_POST[page]}#${_GET[i]}")"
47   ;;
48   480p)
49     [ $height -gt 480 ] && redirect "?action=transcode&i=${linkinfo}&t=480" \
50                         || redirect "$videourl"
51   ;;
52   240p)
53     [ $height -gt 240 ] && redirect "?action=transcode&i=${linkinfo}&t=240" \
54                         || redirect "$videourl"
55   ;;
56   download)
57     redirect "$videourl"
58   ;;
59   server)
60     [ -n "$location" ] && playuri="${treeroot%/}/${location#/}"
61     [ -n "$info" ]     && playuri="${_DATA}/videos/$info"
62     [ -n "$foreign" ]  && playuri="$videourl"
63
64     metafile="${_DATA}/meta/${info}.meta"
65
66     playmeta="${_DATA}/mplayer.meta"
67     fifofile="${_DATA}/mplayer.fifo"
68     statusfile="${_DATA}/mplayer.status"
69     statusfile="/tmp/mplayer.status"
70     volume="$(validate "$(cat "${_DATA}/mplayer.volume" || true)" '[1-9]?[0-9]|100' 20)"
71     [ \! -p "$fifofile" ] && rm -f "$fifofile" && mkfifo -m 600 "$fifofile"
72
73     debug PLAYURI "$playuri"
74
75     if [ -f "$metafile" ]; then
76       cp "$metafile" "$playmeta"
77     else
78       rm "$playmeta"
79       "${_EXEC}"/helpers/genmeta.sh "$playuri" "$playmeta" >&-
80     fi
81
82     export DISPLAY=":0"
83     uid="$(id |sed -rn '1s;.*uid=([0-9]+)[^0-9].*;\1;p')"
84     export XDG_RUNTIME_DIR="/run/user/$uid"  # required for Pulseaudio
85     xrandr || true >&-  # helps enable all screens
86     xvkbd -text '\x+10000\y+10000' || true >&-
87
88     if mplayer |grep -q MPlayer2; then
89       nohup /usr/bin/mplayer \
90         --slave --input=file="$fifofile" \
91         --volume="$volume" \
92         "$playuri" 2>"$statusfile" >&- &
93     else
94       nohup /usr/bin/mplayer \
95         -slave -input file="$fifofile" \
96         -volume "$volume" \
97         "$playuri" >"$statusfile" &
98     fi
99
100     chmod 600 "$statusfile"
101
102     redirect "?p=playctl&${watch_link}"
103   ;;
104   *) redirect "?p=view&${watch_link}"
105   ;;
106 esac
107
108
109