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