]> git.plutz.net Git - rawnet/blob - page_video.sh
video styling (controls, size, etc...)
[rawnet] / page_video.sh
1 #!/bin/sh
2
3 # ID    NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS (void|private|hidden|public)     UPLOADER        HITS    DESCR_CACHE     FUTUREUSE
4
5 if [ "$video" -a -f "$vid_db" -a -r "$vid_db" ]; then
6   read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
7           VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
8           VIDEO_DESCR_CACHE VIDEO_FUTUREUSE <<-EOF
9         $(grep "^${video}       " "${vid_db}")
10         EOF
11   if [ "$VIDEO_ID" ]; then
12            VIDEO_NAME="$(UNSTRING "$VIDEO_NAME")"
13     VIDEO_DESCRIPTION="$(UNSTRING "$VIDEO_DESCRIPTION")"
14           VIDEO_COVER="$(UNSTRING "$VIDEO_COVER")"
15     VIDEO_DESCR_CACHE="$(UNSTRING "$VIDEO_DESCR_CACHE")"
16   else
17     video=''
18   fi
19 fi
20
21 update_video(){
22   local id="${1}" name description resx resy length cover status uploader \
23         hits descr_cache futureuse
24   local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS \
25         DESCR_CACHE FUTUREUSE
26   local arg video thumb cnt
27   video="${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.mp4"
28   thumb="${_DATA}/${CHANNEL_ID}/${VIDEO_ID}_thumb.jpg"
29
30   for arg in "$@"; do case $arg in
31     name=*) name="${arg#*=}";;
32     description=*) description="${arg#*=}";;
33     cover=*) cover="${arg#*=}";;
34     status=*) status="${arg#*=}";;
35     uploader=*) uploader="${arg#*=}";;
36     hits=*) hits="${arg#*=}";;
37   esac; done
38
39   if [ -f "$video" -a -r "$video" ]; then
40     arg="$(echo; ffprobe -show_entries format=duration:stream=width,height "$video" 2>&-)"
41     resx="${arg#*width=}"; resx="${resx%%${BR}*}"
42     resy="${arg#*height=}"; resy="${resy%%${BR}*}"
43     length="${arg#*duration=}"; length="${length%%${BR}*}"
44   fi
45   if [ "${length%.*}" -a ! "${thumb}" -nt "${video}" ]; then
46     for cnt in 1 2 3 4 5 6 7 8 9 10; do
47       ffmpeg -nostdin -y -ss "$((cnt * ${length%.*} / 11))" -i "$video" \
48              -frames 1 "${thumb%.jpg}_$((cnt - 1)).jpg"
49     done 2>&-
50     montage "${thumb%.jpg}"_[0-9].jpg \
51             -background "#000000" \
52             -tile 10x1 -geometry 320x180+0+0 \
53             -interlace line -quality 85 "${thumb}"
54     rm -- "${thumb%.jpg}"_[0-9].jpg
55   fi
56
57   if LOCK "$vid_db"; then
58     while read -r ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS \
59                   DESCR_CACHE FUTUREUSE; do
60       if [ "$id" = "$ID" ]; then
61         printf '%s      %s      %s      %i      %i      %f      %s      %s      %s      %i      %s      %s\n' \
62                "$id" "$(STRING "${name-$(UNSTRING "$NAME")}")" \
63                "$(STRING "${description-$(UNSTRING "$DESCRIPTION")}")" \
64                "${resx:-${resx-${RESX}}${resx+0}}" \
65                "${resy:-${resy-${RESY}}${resy+0}}" \
66                "${length:-${length-${LENGTH}}${length+0}}" \
67                "$(STRING "${cover-$(UNSTRING "$COVER")}")" \
68                "${status:-${status-${STATUS}}${status+void}}" \
69                "${uploader:-${uploader-${UPLOADER}}${uploader+\\}}" \
70                "${hits:-${hits-${HITS}}${hits+0}}" \
71                "$(printf %s "${description-$(UNSTRING "$DESCRIPTION")}" |markdown |STRING)" \
72                "${FUTUREUSE:-\\}"
73       else
74         printf '%s      %s      %s      %i      %i      %f      %s      %s      %s      %i      %s      %s\n' \
75                 "$ID" "$NAME" "$DESCRIPTION" "$RESX" "$RESY" "$LENGTH" \
76                 "$COVER" "$STATUS" "$UPLOADER" "$HITS" "$DESCR_CACHE" \
77                 "$FUTUREUSE"
78       fi
79     done <"$vid_db" >"${vid_db}.$$"
80     mv -- "${vid_db}.$$" "${vid_db}"
81     RELEASE "$vid_db"
82   else
83     return 1
84   fi
85 }
86
87 UPLOAD(){
88   local file="$1"
89   local boundary line last
90
91   [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
92
93   boundary="${CONTENT_TYPE#*; boundary=}"
94   boundary="${boundary%%;*}"
95
96   head -c "$CONTENT_LENGTH" \
97   | sed -nE '
98     # discard lines prior to boundary
99     /^--'"${boundary}"'\r?$/!b;
100     # discard lines until first blank
101     :A; n; /^\r?$/!bA; n;
102     # print lines until boundary ( = actual file upload)
103     :FILE; p; n;
104     /^--'"${boundary}"'(--)?\r?$/!bFILE;
105     # discard remaining lines
106     :END; $q; n; bEND;
107   ' >"$file"
108   truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
109 }
110
111 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
112   update_video)
113     if [ ! "$USER_ID" ]; then
114       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
115     elif ! AUTHOR; then
116       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
117     elif update_video "$video" "name=$(POST name)" \
118                       "description=$(POST description)" \
119                       "status=$(POST status |grep -m1 -xE 'void|private|hidden|public')" \
120                       "uploader=$USER_ID"; then
121       REDIRECT "${_BASE}/channel/${channel}/${video}/#UPDATE_SUCCESS"
122     else
123       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
124     fi
125     ;;
126   update_video_cancel)
127     REDIRECT "${_BASE}/channel/${channel}/${video}/#CANCELED"
128     ;;
129   delete)
130     if [ ! "$USER_ID" ]; then
131       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
132     elif ! AUTHOR; then
133       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
134     elif [ "$(POST delconfirm)" != confirm ]; then
135       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOT_CONFIRMED"
136     elif LOCK "$vid_db"; then
137       while read -r id tail; do
138         [ "$id" != "$video" ] && printf '%s     %s\n' "$id" "$tail"
139       done <"$vid_db" >"${vid_db}.$$"
140       mv -- "${vid_db}.$$" "$vid_db"
141       rm -- "${_DATA}/${channel}/${video}.mp4" "${_DATA}/${channel}/${video}_thumb.jpg"
142       RELEASE "$vid_db"
143       REDIRECT "${_BASE}/channel/${channel}/#DELETE_CONFIRM"
144     else
145       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
146     fi
147     ;;
148 esac
149
150 if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; then
151   if ! AUTHOR; then
152     head -c "$CONTENT_LENGTH" >/dev/null
153     REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOTALLOWED"
154   elif [ "$VIDEO_STATUS" != void ]; then
155     head -c "$CONTENT_LENGTH" >/dev/null
156     REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOCLOBBER"
157   elif UPLOAD "$_DATA/$channel/$video.mp4"; then
158     update_video "$video" status=private
159     VIDEO_STATUS=private
160   fi
161 fi
162
163 if [ "$channel" -a "$video" -a "$action" = edit ]; then
164   AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
165
166   yield_page "$VIDEO_NAME - Edit" "video edit" <<-EOF
167         [form .video .edit method=POST
168           [input name="name" value="$(HTML "$VIDEO_NAME")" placeholder="Video Name"]
169           [fieldset .status $([ $VIDEO_STATUS = void ] && printf "disabled=disabled")
170             [radio "status" "private" #status_private $(checked $VIDEO_STATUS private void)]
171               [label for=status_private tooltip="Video is only visible to channel authors" Private]
172             [radio "status" "hidden" #status_hidden  $(checked $VIDEO_STATUS hidden)]
173               [label for=status_hidden tooltip="Video will not be listed but can be viewed by anyone knowing the URL" Hidden]
174             [radio "status" "public" #status_public $(checked $VIDEO_STATUS public)]
175               [label for=status_public tooltip="Video will be listed publicly" Public]
176           ]
177           [textarea name="description" placeholder="Description" . $(HTML "$VIDEO_DESCRIPTION")]
178           [submit "action" "update_video" . Update]
179           [submit "action" "update_video_cancel" . Cancel]
180           [fieldset .delete
181             [checkbox "delconfirm" "confirm" id="delconfirm"]
182             [label for=delconfirm Delete Video]
183             [submit "action" "delete" Delete Video]
184           ]
185         ]
186         EOF
187
188 elif [ "$channel" -a "$video" -a "$action" = frameuploadprogress ]; then
189   AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
190   printf '%s\r\n' 'Content-Type: text/html' 'Connection: close' ''
191   printf '<!DOCTYPE HTML>
192   <html><head>
193     <title>Upload Progress</title>
194     <style type="text/css"><!--
195     body {
196       text-align: center;
197     }
198     .progress {
199       display: inline-block;
200       width: 20em;
201       position: absolute;
202       background-color: #FFF;
203     }
204     --></style>
205   </head><body>
206   '
207   while [ "$VIDEO_STATUS" = void ]; do
208     printf '<span class=progress>%i</span>\n' "$(stat -c %s "$_DATA/$channel/$video.mp4" 2>&-)"
209     sleep 1
210     read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
211             VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
212             VIDEO_DESCR_CACHE VIDEO_FUTUREUSE <<-EOF
213         $(grep "^${video}       " "${vid_db}")
214         EOF
215   done
216   printf '<span class=progress>Ready!</span>\n'
217   printf '</body></html>'
218
219 elif [ "$channel" -a "$video" -a "$action" = frameupload ]; then
220   AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
221   printf '%s\r\n' 'Content-Type: text/html' ''
222   [ "$VIDEO_STATUS" = void ] && "$_EXEC"/cgilite/html-sh.sed <<-EOF
223         [!DOCTYPE HTML]
224         [html [head
225           [title Upload Form]
226         ][body
227         [form .upload method=POST enctype="multipart/form-data"
228           [input type=file name=upload]
229           [submit "action" "video_upload" Upload]
230         ]
231         ]]
232         EOF
233   [ "$VIDEO_STATUS" != void ] && "$_EXEC"/cgilite/html-sh.sed <<-EOF
234         [!DOCTYPE HTML]
235         [html [head
236           [title Upload Form]
237         ][body
238           [a href="./" target="_parent" . Reload Page!]
239         ]]
240         EOF
241
242 elif [ "$channel" -a "$video" ]; then
243   [ $VIDEO_STATUS = public -o $VIDEO_STATUS = hidden ] || AUTHOR || { . ${_EXEC}/page_404.sh; exit 0; }
244
245   yield_page "$VIDEO_NAME" "video" <<-EOF
246         [nav [a href="../../" Channels] - [a href="../" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")] - [span $(HTML "${VIDEO_NAME:-(Unnamed Video)}")]]
247         $( AUTHOR && [ $VIDEO_STATUS = void ] && printf '
248         [iframe src="frameuploadprogress" width="100%%" height="50"
249           [a href="freameuploadprogress" Iframe: Upload progress]
250         ]
251         [iframe src="frameupload" width="100%%" height="50"
252           [form .upload method=POST enctype="multipart/form-data"
253             [input type=file name=upload]
254             [submit "action" "video_upload" Upload]
255           ]
256         ]')
257         $( [ $VIDEO_STATUS != void ] && printf '
258         [video preload=none controls=controls width=%i height=%i
259           [source src="%s/video/%s/%s.mp4" type="video/mp4"]
260         ]' "$VIDEO_RESX" "$VIDEO_RESY" "$_BASE" "$channel" "$video"
261         )
262         $(AUTHOR && printf '[a .button href="edit" edit]')
263         [h1 .name $(HTML "$VIDEO_NAME")]
264         [div .description . ${VIDEO_DESCR_CACHE}]
265         EOF
266
267 else
268   . "$_EXEC/page_404.sh"
269 fi