]> git.plutz.net Git - rawnet/blobdiff - page_video.sh
refactoring: fewer global variables, manage all url parsing in index.cgi, split some...
[rawnet] / page_video.sh
old mode 100644 (file)
new mode 100755 (executable)
index 587ee52..49f49a7
@@ -1,93 +1,73 @@
 #!/bin/sh
 
+[ "$includepage_video" ] && return 0
+includepage_video="$0"
+
+. "$_EXEC/db_channel.sh"
 . "$_EXEC/db_video.sh"
+. "$_EXEC/widgets.sh"
+. "$_EXEC/upload.sh"
 
+read_channel "$channel"
 read_video "$video"
 
-UPLOAD(){
-  local file="$1"
-  local boundary line length=0
-
-  [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
-
-  boundary="${CONTENT_TYPE#*; boundary=}"
-  boundary="${boundary%%;*}"
-
-  while read -r line; do
-    length="$(( length + ${#line} + 1))"
-    [ "${line%${CR}}" = "--$boundary" ] && break
-  done
-  while read -r line; do
-    length="$(( length + ${#line} + 1))"
-    [ ! "${line%${CR}}" ] && break \
-    || debug "$line"
-  done
-
-  printf "%i\n" "$(( CONTENT_LENGTH - length ))" >"${file}.upload"
-  head -c "$(( CONTENT_LENGTH - length ))" \
-  | sed -nE '
-    # print lines until boundary ( = actual file upload)
-    :FILE; p; n;
-    /^--'"${boundary}"'(--)?\r?$/!bFILE;
-    # discard remaining lines
-    :END; $q; n; bEND;
-  ' >"$file"
-  truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
-  rm -- "${file}.upload"
-}
-
 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
   update_video)
     if [ ! "$USER_ID" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_NOTLOGGEDIN"
     elif ! AUTHOR; then
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
-    elif update_video "$video" "name=$(POST name)" \
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPDATE_NOTALLOWED"
+    elif update_video "$VIDEO_ID" "name=$(POST name)" \
                       "description=$(POST description)" \
                       "status=$(POST status |grep -m1 -xE 'void|private|hidden|public')" \
                       "uploader=$USER_ID"; then
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#UPDATE_SUCCESS"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#UPDATE_SUCCESS"
     else
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPDATE_NOLOCK"
     fi
     ;;
   update_video_cancel)
-    REDIRECT "${_BASE}/channel/${channel}/${video}/#CANCELED"
+    REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#CANCELED"
     ;;
   delete)
     if [ ! "$USER_ID" ]; then
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_NOTLOGGEDIN"
     elif ! AUTHOR; then
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPDATE_NOTALLOWED"
     elif [ "$(POST delconfirm)" != confirm ]; then
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOT_CONFIRMED"
-    elif delete_video "$video"; then
-      REDIRECT "${_BASE}/channel/${channel}/#DELETE_CONFIRM"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_NOT_CONFIRMED"
+    elif delete_video "$VIDEO_ID"; then
+      rm -f -- "$_DATA/$CHANNEL_ID/$VIDEO_ID.upload.mp4" \
+               "$_DATA/$CHANNEL_ID/$VIDEO_ID.mp4" \
+               "$_DATA/$CHANNEL_ID/$VIDEO_ID.webm" \
+               "$_DATA/$CHANNEL_ID/$VIDEO_ID.thumb.jpg"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/#DELETE_CONFIRM"
     else
-      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPDATE_NOLOCK"
     fi
     ;;
 esac
 
-if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; then
+if [ "$REQUEST_METHOD" = POST -a "$CHANNEL_ID" -a "$VIDEO_ID" ]; then
   if ! AUTHOR; then
     head -c "$CONTENT_LENGTH" >/dev/null
-    REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOTALLOWED"
+    REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPLOAD_NOTALLOWED"
   elif [ "$VIDEO_STATUS" != void ]; then
     head -c "$CONTENT_LENGTH" >/dev/null
-    REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPLOAD_NOCLOBBER"
-  elif UPLOAD "$_DATA/$channel/$video.mp4"; then
-    update_video "$video" status=private
+    REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPLOAD_NOCLOBBER"
+  elif UPLOAD "$_DATA/$CHANNEL_ID/$VIDEO_ID.upload.mp4"; then
+    update_video "$VIDEO_ID" status=private
     VIDEO_STATUS=private
+    REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/edit"
   fi
 fi
 
-if [ "$channel" -a "$video" -a "$action" = edit ]; then
-  AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
+if [ "$CHANNEL_ID" -a "$VIDEO_ID" -a "$action" = edit ]; then
+  AUTHOR || REDIRECT "$_BASE/$CHANNEL_ID/$VIDEO_ID/#ERROR_EDIT_NOTALLOWED"
 
   yield_page "$VIDEO_NAME - Edit" "video edit" <<-EOF
        [form .video .edit method=POST
-         [input name="name" value="$(HTML "$VIDEO_NAME")" placeholder="Video Name"]
+         [input name="name" value="$(HTML "$VIDEO_NAME")" placeholder="Video Name" autocomplete=off]
          [fieldset .status $([ $VIDEO_STATUS = void ] && printf "disabled=disabled")
            [radio "status" "private" #status_private $(checked $VIDEO_STATUS private void)]
              [label for=status_private tooltip="Video is only visible to channel authors" Private]
@@ -107,77 +87,12 @@ if [ "$channel" -a "$video" -a "$action" = edit ]; then
        ]
        EOF
 
-elif [ "$channel" -a "$video" -a "$action" = frameuploadprogress ]; then
-  AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
+elif [ "$CHANNEL_ID" -a "$VIDEO_ID" -a "$action" = frameuploadprogress ]; then
+  AUTHOR || REDIRECT "$_BASE/$CHANNEL_ID/$VIDEO_ID/#ERROR_EDIT_NOTALLOWED"
   printf '%s\r\n' 'Content-Type: text/html' 'Connection: close' ''
-  printf '<!DOCTYPE HTML>
-  <html><head>
-    <title>Upload Progress</title>
-    <style type="text/css"><!--
-    body {
-      text-align: center;
-    }
-    .progress {
-      display: block;
-      position: absolute;
-      width: 20em;
-      background-color: #FFF;
-      border: 1pt solid;
-      border-radius: 4pt;
-      height: 1.25em;
-    }
-    .progress .bar {
-      display: block;
-      position: absolute;
-      left: 0; top: 0; bottom: 0;
-      background-color: #666;
-    }
-    .progress .count {
-      display: block;
-      position: absolute;
-      left: 0; top: 0; right: 0; bottom: 0;
-      line-height: 1.375em;
-    }
-    --></style>
-  </head><body>
-  '
-  printf '<div class=progress><div class=bar style="width: 0%%;"></div><div class=count>%i / %i</div></div>\n' 0 0
-  while [ ! -f "$_DATA/$channel/$video.mp4" -a \
-          ! -f "$_DATA/$channel/$video.mp4.upload" ]; do sleep 1; done
-  read size <"$_DATA/$channel/$video.mp4.upload"
-  while [ -f "$_DATA/$channel/$video.mp4.upload" ]; do
-    stat="$(stat -c %s "$_DATA/$channel/$video.mp4" 2>&-)"
-    printf '<div class=progress><div class=bar style="width:%i%%;"></div><div class=count>%iMB / %iMB</div></div>\n' \
-      "$(( stat * 100 / size ))" "$((stat / 1048576))" "$((size / 1048576))"
-    sleep 1
-  done
-  printf '<span class=progress><div class=bar style="width:100%%;"></div><div class=count>Ready!</div></span>\n'
-  printf '</body></html>'
-
-elif [ "$channel" -a "$video" -a "$action" = frameupload ]; then
-  AUTHOR || REDIRECT "$_BASE/$channel/$video/#ERROR_EDIT_NOTALLOWED"
-  printf '%s\r\n' 'Content-Type: text/html' ''
-  [ "$VIDEO_STATUS" = void ] && "$_EXEC"/cgilite/html-sh.sed <<-EOF
-       [!DOCTYPE HTML]
-       [html [head
-         [title Upload Form]
-       ][body
-       [form .upload method=POST enctype="multipart/form-data"
-         [input type=file name=upload]
-         [submit "action" "video_upload" Upload]
-       ]
-       ]]
-       EOF
-  [ "$VIDEO_STATUS" != void ] && "$_EXEC"/cgilite/html-sh.sed <<-EOF
-       [!DOCTYPE HTML]
-       [html [head
-         [title Upload Form]
-       ][body
-         [a href="./" target="_parent" . Reload Page!]
-       ]]
-       EOF
+  frame_uploadprogress
 
-elif [ "$channel" -a "$video" ]; then
+elif [ "$CHANNEL_ID" -a "$VIDEO_ID" -a "$VIDEO_STATUS" = void ]; then
   [ $VIDEO_STATUS = public -o $VIDEO_STATUS = hidden ] || AUTHOR || { . ${_EXEC}/page_404.sh; exit 0; }
 
   yield_page "$VIDEO_NAME" "video" <<-EOF
@@ -186,17 +101,32 @@ elif [ "$channel" -a "$video" ]; then
         [iframe src="frameuploadprogress" width="100%%" height="50"
          [a href="freameuploadprogress" Iframe: Upload progress]
        ]
-       [iframe src="frameupload" width="100%%" height="50"
-         [form .upload method=POST enctype="multipart/form-data"
-           [input type=file name=upload]
-           [submit "action" "video_upload" Upload]
-         ]
+       [form .upload method=POST enctype="multipart/form-data"
+         [input type=file name=upload]
+         [submit "action" "video_upload" Upload]
        ]')
-       $( [ $VIDEO_STATUS != void ] && printf '
-       [video preload=none controls=controls width=%i height=%i
-         [source src="%s/video/%s/%s.mp4" type="video/mp4"]
-       ]' "$VIDEO_RESX" "$VIDEO_RESY" "$_BASE" "$channel" "$video"
-        )
+       $(AUTHOR && printf '[a .button href="edit" edit]')
+       [h1 .name $(HTML "$VIDEO_NAME")]
+       [div .description . ${VIDEO_DESCR_CACHE}]
+       EOF
+
+elif [ "$CHANNEL_ID" -a "$VIDEO_ID" -a "$VIDEO_STATUS" != void ]; then
+  [ $VIDEO_STATUS = public -o $VIDEO_STATUS = hidden ] || AUTHOR || { . ${_EXEC}/page_404.sh; exit 0; }
+
+  yield_page "$VIDEO_NAME" "video" <<-EOF
+       [nav [a href="../../" Channels] - [a href="../" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")] - [span $(HTML "${VIDEO_NAME:-(Unnamed Video)}")]]
+       [video preload=none controls=controls width="$VIDEO_RESX" height="$VIDEO_RESY"
+       $([ -f "${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.mp4" ] \
+         && printf '[source src="%s/video/%s/%s.mp4"  type="video/mp4"]' \
+                   "$_BASE" "$CHANNEL_ID" "$VIDEO_ID"
+         [ -f "${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.webm" ] \
+         && printf '[source src="%s/video/%s/%s.webm" type="video/webm"]' \
+                   "$_BASE" "$CHANNEL_ID" "$VIDEO_ID"
+         [ ! -f "${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.mp4" \
+        -a ! -f "${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.webm" ] \
+         && printf '[source src="%s/video/%s/%s.upload.mp4"  type="video/mp4"]\nThe video has not yet been transcoded and may not be displayed correctly.' \
+                   "$_BASE" "$CHANNEL_ID" "$VIDEO_ID"
+        )]
        $(AUTHOR && printf '[a .button href="edit" edit]')
        [h1 .name $(HTML "$VIDEO_NAME")]
        [div .description . ${VIDEO_DESCR_CACHE}]