]> git.plutz.net Git - rawnet/blobdiff - page_video.sh
mark executable
[rawnet] / page_video.sh
old mode 100644 (file)
new mode 100755 (executable)
index fc1d358..587ee52
 #!/bin/sh
 
-# ID   NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS (void|private|hidden|public)     UPLOADER        HITS    DESCR_CACHE     FUTUREUSE
+. "$_EXEC/db_video.sh"
 
-if [ "$video" -a -f "$vid_db" -a -r "$vid_db" ]; then
-  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
-  if [ "$VIDEO_ID" ]; then
-           VIDEO_NAME="$(UNSTRING "$VIDEO_NAME")"
-    VIDEO_DESCRIPTION="$(UNSTRING "$VIDEO_DESCRIPTION")"
-          VIDEO_COVER="$(UNSTRING "$VIDEO_COVER")"
-    VIDEO_DESCR_CACHE="$(UNSTRING "$VIDEO_DESCR_CACHE")"
-  else
-    video=''
-  fi
-fi
-
-update_video(){
-  local id="${1}" name description resx resy length cover status uploader \
-        hits descr_cache futureuse
-  local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS \
-        DESCR_CACHE FUTUREUSE
-  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#*=}";;
-    description=*) description="${arg#*=}";;
-    cover=*) cover="${arg#*=}";;
-    status=*) status="${arg#*=}";;
-    uploader=*) uploader="${arg#*=}";;
-    hits=*) hits="${arg#*=}";;
-  esac; done
-
-  if [ -f "$video" -a -r "$video" ]; then
-    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 \
-                  DESCR_CACHE FUTUREUSE; do
-      if [ "$id" = "$ID" ]; then
-        printf '%s     %s      %s      %i      %i      %f      %s      %s      %s      %i      %s      %s\n' \
-               "$id" "$(STRING "${name-$(UNSTRING "$NAME")}")" \
-               "$(STRING "${description-$(UNSTRING "$DESCRIPTION")}")" \
-               "${resx:-${resx-${RESX}}${resx+0}}" \
-               "${resy:-${resy-${RESY}}${resy+0}}" \
-               "${length:-${length-${LENGTH}}${length+0}}" \
-               "$(STRING "${cover-$(UNSTRING "$COVER")}")" \
-               "${status:-${status-${STATUS}}${status+void}}" \
-               "${uploader:-${uploader-${UPLOADER}}${uploader+\\}}" \
-               "${hits:-${hits-${HITS}}${hits+0}}" \
-               "$(printf %s "${description-$(UNSTRING "$DESCRIPTION")}" |markdown |STRING)" \
-               "${FUTUREUSE:-\\}"
-      else
-        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"
-      fi
-    done <"$vid_db" >"${vid_db}.$$"
-    mv -- "${vid_db}.$$" "${vid_db}"
-    RELEASE "$vid_db"
-  else
-    return 1
-  fi
-}
+read_video "$video"
 
 UPLOAD(){
   local file="$1"
-  local boundary line last
+  local boundary line length=0
 
   [ ! "${CONTENT_TYPE}" -o "${CONTENT_TYPE##multipart/form-data;*}" ] && return 1
 
   boundary="${CONTENT_TYPE#*; boundary=}"
   boundary="${boundary%%;*}"
 
-  head -c "$CONTENT_LENGTH" \
+  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 '
-    # discard lines prior to boundary
-    /^--'"${boundary}"'\r?$/!b;
-    # discard lines until first blank
-    :A; n; /^\r?$/!bA; n;
     # print lines until boundary ( = actual file upload)
     :FILE; p; n;
     /^--'"${boundary}"'(--)?\r?$/!bFILE;
@@ -106,6 +33,7 @@ UPLOAD(){
     :END; $q; n; bEND;
   ' >"$file"
   truncate -s $(( $(stat -c %s -- "$file") -2 )) -- "$file"
+  rm -- "${file}.upload"
 }
 
 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
@@ -133,13 +61,7 @@ UPLOAD(){
       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"
+    elif delete_video "$video"; then
       REDIRECT "${_BASE}/channel/${channel}/#DELETE_CONFIRM"
     else
       REDIRECT "${_BASE}/channel/${channel}/${video}/#ERROR_UPDATE_NOLOCK"
@@ -196,24 +118,40 @@ elif [ "$channel" -a "$video" -a "$action" = frameuploadprogress ]; then
       text-align: center;
     }
     .progress {
-      display: inline-block;
-      width: 20em;
+      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>
   '
-  while [ "$VIDEO_STATUS" = void ]; do
-    printf '<span class=progress>%i</span>\n' "$(stat -c %s "$_DATA/$channel/$video.mp4" 2>&-)"
+  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
-    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 '<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