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