]> git.plutz.net Git - rawnet/blob - page_video.sh
separated data layer
[rawnet] / page_video.sh
1 #!/bin/sh
2
3 . "$_EXEC/db_video.sh"
4
5 read_video "$video"
6
7 UPLOAD(){
8   local file="$1"
9   local boundary line length=0
10
11   [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
12
13   boundary="${CONTENT_TYPE#*; boundary=}"
14   boundary="${boundary%%;*}"
15
16   while read -r line; do
17     length="$(( length + ${#line} + 1))"
18     [ "${line%${CR}}" = "--$boundary" ] && break
19   done
20   while read -r line; do
21     length="$(( length + ${#line} + 1))"
22     [ ! "${line%${CR}}" ] && break \
23     || debug "$line"
24   done
25
26   printf "%i\n" "$(( CONTENT_LENGTH - length ))" >"${file}.upload"
27   head -c "$(( CONTENT_LENGTH - length ))" \
28   | sed -nE '
29     # print lines until boundary ( = actual file upload)
30     :FILE; p; n;
31     /^--'"${boundary}"'(--)?\r?$/!bFILE;
32     # discard remaining lines
33     :END; $q; n; bEND;
34   ' >"$file"
35   truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
36   rm -- "${file}.upload"
37 }
38
39 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
40   update_video)
41     if [ ! "$USER_ID" ]; then
42       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
43     elif ! AUTHOR; then
44       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
45     elif update_video "$video" "name=$(POST name)" \
46                       "description=$(POST description)" \
47                       "status=$(POST status |grep -m1 -xE 'void|private|hidden|public')" \
48                       "uploader=$USER_ID"; then
49       REDIRECT "${_BASE}/channel/${channel}/${video}/#UPDATE_SUCCESS"
50     else
51       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
52     fi
53     ;;
54   update_video_cancel)
55     REDIRECT "${_BASE}/channel/${channel}/${video}/#CANCELED"
56     ;;
57   delete)
58     if [ ! "$USER_ID" ]; then
59       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
60     elif ! AUTHOR; then
61       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
62     elif [ "$(POST delconfirm)" != confirm ]; then
63       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOT_CONFIRMED"
64     elif delete_video "$video"; then
65       REDIRECT "${_BASE}/channel/${channel}/#DELETE_CONFIRM"
66     else
67       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
68     fi
69     ;;
70 esac
71
72 if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; then
73   if ! AUTHOR; then
74     head -c "$CONTENT_LENGTH" >/dev/null
75     REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOTALLOWED"
76   elif [ "$VIDEO_STATUS" != void ]; then
77     head -c "$CONTENT_LENGTH" >/dev/null
78     REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOCLOBBER"
79   elif UPLOAD "$_DATA/$channel/$video.mp4"; then
80     update_video "$video" status=private
81     VIDEO_STATUS=private
82   fi
83 fi
84
85 if [ "$channel" -a "$video" -a "$action" = edit ]; then
86   AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
87
88   yield_page "$VIDEO_NAME - Edit" "video edit" <<-EOF
89         [form .video .edit method=POST
90           [input name="name" value="$(HTML "$VIDEO_NAME")" placeholder="Video Name"]
91           [fieldset .status $([ $VIDEO_STATUS = void ] && printf "disabled=disabled")
92             [radio "status" "private" #status_private $(checked $VIDEO_STATUS private void)]
93               [label for=status_private tooltip="Video is only visible to channel authors" Private]
94             [radio "status" "hidden" #status_hidden  $(checked $VIDEO_STATUS hidden)]
95               [label for=status_hidden tooltip="Video will not be listed but can be viewed by anyone knowing the URL" Hidden]
96             [radio "status" "public" #status_public $(checked $VIDEO_STATUS public)]
97               [label for=status_public tooltip="Video will be listed publicly" Public]
98           ]
99           [textarea name="description" placeholder="Description" . $(HTML "$VIDEO_DESCRIPTION")]
100           [submit "action" "update_video" . Update]
101           [submit "action" "update_video_cancel" . Cancel]
102           [fieldset .delete
103             [checkbox "delconfirm" "confirm" id="delconfirm"]
104             [label for=delconfirm Delete Video]
105             [submit "action" "delete" Delete Video]
106           ]
107         ]
108         EOF
109
110 elif [ "$channel" -a "$video" -a "$action" = frameuploadprogress ]; then
111   AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
112   printf '%s\r\n' 'Content-Type: text/html' 'Connection: close' ''
113   printf '<!DOCTYPE HTML>
114   <html><head>
115     <title>Upload Progress</title>
116     <style type="text/css"><!--
117     body {
118       text-align: center;
119     }
120     .progress {
121       display: block;
122       position: absolute;
123       width: 20em;
124       background-color: #FFF;
125       border: 1pt solid;
126       border-radius: 4pt;
127       height: 1.25em;
128     }
129     .progress .bar {
130       display: block;
131       position: absolute;
132       left: 0; top: 0; bottom: 0;
133       background-color: #666;
134     }
135     .progress .count {
136       display: block;
137       position: absolute;
138       left: 0; top: 0; right: 0; bottom: 0;
139       line-height: 1.375em;
140     }
141     --></style>
142   </head><body>
143   '
144   printf '<div class=progress><div class=bar style="width: 0%%;"></div><div class=count>%i / %i</div></div>\n' 0 0
145   while [ ! -f "$_DATA/$channel/$video.mp4" -a \
146           ! -f "$_DATA/$channel/$video.mp4.upload" ]; do sleep 1; done
147   read size <"$_DATA/$channel/$video.mp4.upload"
148   while [ -f "$_DATA/$channel/$video.mp4.upload" ]; do
149     stat="$(stat -c %s "$_DATA/$channel/$video.mp4" 2>&-)"
150     printf '<div class=progress><div class=bar style="width:%i%%;"></div><div class=count>%iMB / %iMB</div></div>\n' \
151       "$(( stat * 100 / size ))" "$((stat / 1048576))" "$((size / 1048576))"
152     sleep 1
153   done
154   printf '<span class=progress><div class=bar style="width:100%%;"></div><div class=count>Ready!</div></span>\n'
155   printf '</body></html>'
156
157 elif [ "$channel" -a "$video" -a "$action" = frameupload ]; then
158   AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
159   printf '%s\r\n' 'Content-Type: text/html' ''
160   [ "$VIDEO_STATUS" = void ] && "$_EXEC"/cgilite/html-sh.sed <<-EOF
161         [!DOCTYPE HTML]
162         [html [head
163           [title Upload Form]
164         ][body
165         [form .upload method=POST enctype="multipart/form-data"
166           [input type=file name=upload]
167           [submit "action" "video_upload" Upload]
168         ]
169         ]]
170         EOF
171   [ "$VIDEO_STATUS" != void ] && "$_EXEC"/cgilite/html-sh.sed <<-EOF
172         [!DOCTYPE HTML]
173         [html [head
174           [title Upload Form]
175         ][body
176           [a href="./" target="_parent" . Reload Page!]
177         ]]
178         EOF
179
180 elif [ "$channel" -a "$video" ]; then
181   [ $VIDEO_STATUS = public -o $VIDEO_STATUS = hidden ] || AUTHOR || { . ${_EXEC}/page_404.sh; exit 0; }
182
183   yield_page "$VIDEO_NAME" "video" <<-EOF
184         [nav [a href="../../" Channels] - [a href="../" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")] - [span $(HTML "${VIDEO_NAME:-(Unnamed Video)}")]]
185         $( AUTHOR && [ $VIDEO_STATUS = void ] && printf '
186         [iframe src="frameuploadprogress" width="100%%" height="50"
187           [a href="freameuploadprogress" Iframe: Upload progress]
188         ]
189         [iframe src="frameupload" width="100%%" height="50"
190           [form .upload method=POST enctype="multipart/form-data"
191             [input type=file name=upload]
192             [submit "action" "video_upload" Upload]
193           ]
194         ]')
195         $( [ $VIDEO_STATUS != void ] && printf '
196         [video preload=none controls=controls width=%i height=%i
197           [source src="%s/video/%s/%s.mp4" type="video/mp4"]
198         ]' "$VIDEO_RESX" "$VIDEO_RESY" "$_BASE" "$channel" "$video"
199         )
200         $(AUTHOR && printf '[a .button href="edit" edit]')
201         [h1 .name $(HTML "$VIDEO_NAME")]
202         [div .description . ${VIDEO_DESCR_CACHE}]
203         EOF
204
205 else
206   . "$_EXEC/page_404.sh"
207 fi