]> git.plutz.net Git - serve0/blob - actions/watch.sh
styling for pagination links
[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     ncat "$RHOST" "$RPORT" >&- <<-EOF
45         GET /?action=watch&url=$(urlsafe "http://${HTTP_HOST}/${videourl}") HTTP/1.1$(printf \\r)
46         Host: ${RHOST}$(printf \\r)
47         $(printf \\r)
48         EOF
49     redirect "?p=playctl&${watch_link}w=$(urlsafe "${_POST[page]}#${_GET[i]}")"
50   ;;
51   480p)
52     [ $height -gt 480 ] && redirect "?action=transcode&i=${linkinfo}&t=480" \
53                         || redirect "$videourl"
54   ;;
55   240p)
56     [ $height -gt 240 ] && redirect "?action=transcode&i=${linkinfo}&t=240" \
57                         || redirect "$videourl"
58   ;;
59   download)
60     redirect "$videourl"
61   ;;
62   server)
63     [ -n "$location" ] && playuri="${treeroot%/}/${location#/}"
64     [ -n "$info" ]     && playuri="${_DATA}/videos/$info"
65     [ -n "$foreign" ]  && playuri="$videourl"
66
67     metafile="${_DATA}/meta/${info}.meta"
68
69     playmeta="${_DATA}/mplayer.meta"
70     fifofile="${_DATA}/mplayer.fifo"
71     statusfile="${_DATA}/mplayer.status"
72     statusfile="/tmp/mplayer.status"
73     volume="$(validate "$(cat "${_DATA}/mplayer.volume" || true)" '[1-9]?[0-9]|100' 20)"
74     [ \! -p "$fifofile" ] && rm -f "$fifofile" && mkfifo -m 600 "$fifofile"
75
76     debug PLAYURI "$playuri"
77
78     if [ -f "$metafile" ]; then
79       cp "$metafile" "$playmeta"
80     else
81       rm "$playmeta"
82       "${_EXEC}"/helpers/genmeta.sh "$playuri" "$playmeta" >&-
83     fi
84
85     export DISPLAY=":0"
86     uid="$(id |sed -rn '1s;.*uid=([0-9]+)[^0-9].*;\1;p')"
87     export XDG_RUNTIME_DIR="/run/user/$uid"  # required for Pulseaudio
88     xrandr || true >&-  # helps enable all screens
89     xvkbd -text '\x+10000\y+10000' || true >&-
90
91     if mplayer |grep -q MPlayer2; then
92       nohup /usr/bin/mplayer \
93         --slave --input=file="$fifofile" \
94         --volume="$volume" \
95         "$playuri" 2>"$statusfile" >&- &
96     else
97       nohup /usr/bin/mplayer \
98         -slave -input file="$fifofile" \
99         -volume "$volume" \
100         "$playuri" >"$statusfile" &
101     fi
102
103     chmod 600 "$statusfile"
104
105     redirect "?p=playctl&${watch_link}"
106   ;;
107   *) redirect "?p=view&${watch_link}"
108   ;;
109 esac
110
111
112