]> git.plutz.net Git - rawnet/blobdiff - page_video.sh
bugfix: prevent double submit when setting up new video page
[rawnet] / page_video.sh
index e1a2ae326116da12192dd7425af4abbb5ff180f3..b7b9953787d44effcf2913027ab5ab0b0a3f3ff5 100644 (file)
@@ -23,8 +23,9 @@ update_video(){
         hits descr_cache futureuse
   local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS \
         DESCR_CACHE FUTUREUSE
-  local arg video
-  video="$_DATA/$CHANNEL_ID/$VIDEO_ID.mp4"
+  local arg video thumb cnt
+  video="${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.mp4"
+  thumb="${_DATA}/${CHANNEL_ID}/${VIDEO_ID}_thumb.jpg"
 
   for arg in "$@"; do case $arg in
     name=*) name="${arg#*=}";;
@@ -36,11 +37,22 @@ update_video(){
   esac; done
 
   if [ -f "$video" -a -r "$video" ]; then
-    arg="$(echo; ffprobe -show_entries stream=width,height,duration "$video" 2>&-)"
+    arg="$(echo; ffprobe -show_entries format=duration:stream=width,height "$video" 2>&-)"
     resx="${arg#*width=}"; resx="${resx%%${BR}*}"
     resy="${arg#*height=}"; resy="${resy%%${BR}*}"
     length="${arg#*duration=}"; length="${length%%${BR}*}"
   fi
+  if [ "${length%.*}" -a ! "${thumb}" -nt "${video}" ]; then
+    for cnt in 1 2 3 4 5 6 7 8 9 10; do
+      ffmpeg -nostdin -y -ss "$((cnt * ${length%.*} / 11))" -i "$video" \
+             -frames 1 "${thumb%.jpg}_$((cnt - 1)).jpg"
+    done 2>&-
+    montage "${thumb%.jpg}"_[0-9].jpg \
+            -background "#000000" \
+            -tile 10x1 -geometry 320x180+0+0 \
+            -interlace line -quality 85 "${thumb}"
+    rm -- "${thumb%.jpg}"_[0-9].jpg
+  fi
 
   if LOCK "$vid_db"; then
     while read -r ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS \
@@ -59,7 +71,7 @@ update_video(){
                "$(printf %s "${description-$(UNSTRING "$DESCRIPTION")}" |markdown |STRING)" \
                "${FUTUREUSE:-\\}"
       else
-        printf '%s     %s      %s      %i      %i      %i      %s      %s      %s      %i      %s      %s\n' \
+        printf '%s     %s      %s      %i      %i      %f      %s      %s      %s      %i      %s      %s\n' \
                 "$ID" "$NAME" "$DESCRIPTION" "$RESX" "$RESY" "$LENGTH" \
                 "$COVER" "$STATUS" "$UPLOADER" "$HITS" "$DESCR_CACHE" \
                 "$FUTUREUSE"
@@ -75,7 +87,6 @@ update_video(){
 UPLOAD(){
   local file="$1"
   local boundary line last
-  local CONTENT_TYPE="$(HEADER Content-Type)"
 
   [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
 
@@ -84,15 +95,15 @@ UPLOAD(){
 
   head -c "$CONTENT_LENGTH" \
   | sed -nE '
+    # discard lines prior to boundary
     /^--'"${boundary}"'\r?$/!b;
-    :A; n;
-    /^\r?$/!bA;
-     n;
-    :FILE
-    p; n;
+    # discard lines until first blank
+    :A; n; /^\r?$/!bA; n;
+    # print lines until boundary ( = actual file upload)
+    :FILE; p; n;
     /^--'"${boundary}"'(--)?\r?$/!bFILE;
-    :END
-    $q; n; bEND;
+    # discard remaining lines
+    :END; $q; n; bEND;
   ' >"$file"
   truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
 }
@@ -115,6 +126,25 @@ UPLOAD(){
   update_video_cancel)
     REDIRECT "${_BASE}/channel/${channel}/${video}/#CANCELED"
     ;;
+  delete)
+    if [ ! "$USER_ID" ]; then
+      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOTLOGGEDIN"
+    elif ! AUTHOR; then
+      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOTALLOWED"
+    elif [ "$(POST delconfirm)" != confirm ]; then
+      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_NOT_CONFIRMED"
+    elif LOCK "$vid_db"; then
+      while read -r id tail; do
+        [ "$id" != "$video" ] && printf '%s    %s\n' "$id" "$tail"
+      done <"$vid_db" >"${vid_db}.$$"
+      mv -- "${vid_db}.$$" "$vid_db"
+      rm -- "${_DATA}/${channel}/${video}.mp4" "${_DATA}/${channel}/${video}_thumb.jpg"
+      RELEASE "$vid_db"
+      REDIRECT "${_BASE}/channel/${channel}/#DELETE_CONFIRM"
+    else
+      REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
+    fi
+    ;;
 esac
 
 if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; then
@@ -125,9 +155,8 @@ if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; 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" "$VIDEO_NAME" "$VIDEO_DESCRIPTION" 0 0 0 \
-                 "$VIDEO_COVER" private "$VIDEO_UPOADER" "$VIDEO_HITS" \
-                 "" ""
+    update_video "$video" status=private
+    VIDEO_STATUS=private
   fi
 fi
 
@@ -148,28 +177,93 @@ if [ "$channel" -a "$video" -a "$action" = edit ]; then
          [textarea name="description" placeholder="Description" . $(HTML "$VIDEO_DESCRIPTION")]
          [submit "action" "update_video" . Update]
          [submit "action" "update_video_cancel" . Cancel]
+         [fieldset .delete
+           [checkbox "delconfirm" "confirm" id="delconfirm"]
+           [label for=delconfirm Delete Video]
+           [submit "action" "delete" Delete Video]
+         ]
        ]
        EOF
+
+elif [ "$channel" -a "$video" -a "$action" = frameuploadprogress ]; then
+  AUTHOR || REDIRECT "$_BASE/$channel/$video/#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: inline-block;
+      width: 20em;
+      position: absolute;
+      background-color: #FFF;
+    }
+    --></style>
+  </head><body>
+  '
+  while [ "$VIDEO_STATUS" = void ]; do
+    printf '<span class=progress>%i</span>\n' "$(stat -c %s "$_DATA/$channel/$video.mp4" 2>&-)"
+    sleep 1
+    read -r VIDEO_ID VIDEO_NAME VIDEO_DESCRIPTION VIDEO_RESX VIDEO_RESY \
+            VIDEO_LENGTH VIDEO_COVER VIDEO_STATUS VIDEO_UPLOADER VIDEO_HITS \
+            VIDEO_DESCR_CACHE VIDEO_FUTUREUSE <<-EOF
+       $(grep "^${video}       " "${vid_db}")
+       EOF
+  done
+  printf '<span class=progress>Ready!</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
+
 elif [ "$channel" -a "$video" ]; 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)}")]
-         $(AUTHOR && printf ' - [a href="edit" edit]')
-       ]
+       [nav [a href="../../" Channels] - [a href="../" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")] - [span $(HTML "${VIDEO_NAME:-(Unnamed Video)}")]]
        $( AUTHOR && [ $VIDEO_STATUS = void ] && printf '
-       [form .upload method=POST enctype="multipart/form-data"
-         [input type=file name=upload]
-         [submit "action" "video_upload" Upload]
+        [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]
+         ]
        ]')
        $( [ $VIDEO_STATUS != void ] && printf '
        [video
          [source src="%s/video/%s/%s.mp4"]
        ]' "$_BASE" "$channel" "$video"
         )
+       $(AUTHOR && printf '[a .button href="edit" edit]')
        [h1 .name $(HTML "$VIDEO_NAME")]
        [div .description . ${VIDEO_DESCR_CACHE}]
        EOF
+
 else
   . "$_EXEC/page_404.sh"
 fi