]> git.plutz.net Git - rawnet/commitdiff
refactoring: fewer global variables, manage all url parsing in index.cgi, split some...
authorPaul Hänsch <paul@plutz.net>
Thu, 21 Oct 2021 22:55:57 +0000 (00:55 +0200)
committerPaul Hänsch <paul@plutz.net>
Thu, 21 Oct 2021 22:55:57 +0000 (00:55 +0200)
db_channel.sh
db_video.sh
index.cgi
page_channel.sh
page_video.sh
upload.sh
widgets.sh [new file with mode: 0755]

index 9c7372d35f91a7aaf22786baef4a2cece14ef2bf..15334815da8c8cf841ccdb30cf1f1ed796d8ebf7 100755 (executable)
@@ -10,6 +10,11 @@ include_dbchannel="$0"
 unset CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO CHANNEL_THEME \
       CHANNEL_AUTHORS CHANNEL_DESCR_CACHE CHANNEL_FUTUREUSE
 
+LOCAL_CHANNEL='local \
+  CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO CHANNEL_THEME \
+  CHANNEL_AUTHORS CHANNEL_DESCR_CACHE CHANNEL_FUTUREUSE
+'
+
 chan_db="$_DATA/channels.db"
 
 read_channel() {
index e2cc09d78196d5f35161519dafb810c699441f06..78d108551af1c120f64cdf9f502ee5f74ae871d0 100755 (executable)
@@ -12,10 +12,15 @@ unset 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
 
-[ "$CHANNEL_ID" ] && vid_db="$_DATA/$CHANNEL_ID/videos.db"
+LOCAL_VIDEO='local \
+  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
+'
 
 read_video() {
-  local video="$1"
+  local video="$1" vid_db="$_DATA/$CHANNEL_ID/videos.db"
+  [ "$CHANNEL_ID" ] || return 1
 
   # Global exports
   VIDEO_ID='' VIDEO_NAME='' VIDEO_DESCRIPTION='' VIDEO_RESX='' VIDEO_RESY=''
@@ -51,7 +56,9 @@ update_video(){
         hits descr_cache futureuse
   local ID NAME DESCRIPTION RESX RESY LENGTH COVER STATUS UPLOADER HITS \
         DESCR_CACHE FUTUREUSE
-  local arg video thumb cnt
+  local arg video thumb cnt vid_db="$_DATA/$CHANNEL_ID/videos.db"
+  [ "$CHANNEL_ID" ] || return 1
+
   video="${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.upload.mp4"
   thumb="${_DATA}/${CHANNEL_ID}/${VIDEO_ID}.thumb.jpg"
 
@@ -113,7 +120,8 @@ update_video(){
 }
 
 new_video(){
-  local video="${1:-$(randomid)}"
+  local video="${1:-$(randomid)}" vid_db="$_DATA/$CHANNEL_ID/videos.db"
+  [ "$CHANNEL_ID" ] || return 1
 
   if LOCK "$vid_db"; then
     if grep -q "^${video}      " "$vid_db"; then
@@ -130,7 +138,8 @@ new_video(){
 }
 
 delete_video() {
-  local video="$1"
+  local video="$1" vid_db="$_DATA/$CHANNEL_ID/videos.db"
+  [ "$CHANNEL_ID" ] || return 1
 
   if LOCK "$vid_db"; then
     grep -v "^${video} " <"$vid_db" >"${vid_db}.$$"
index 0d3c66885e4892f256de8aa4d6ebc1cb7a3208ca..76e3592a236f63bafe9d9171eae4cbe2db5373ed 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -129,7 +129,33 @@ case ${PATH_INFO} in
     . "${_EXEC}/cgilite/file.sh"
     FILE "${_DATA}/${PATH_INFO#/video/}"
     ;;
-  /|/channel/*) . "${_EXEC}/page_channel.sh";;
+  /channel/*/*/*)
+    action="${PATH_INFO##*/}"
+    video="${PATH_INFO%/*}" video="${video##*/}"
+    channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
+    . "$_EXEC/page_video.sh"
+    ;;
+  /channel/*/*/)
+    action=""
+    video="${PATH_INFO%/}" video="${video##*/}"
+    channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
+    . "$_EXEC/page_video.sh"
+    ;;
+  /channel/*/*)
+    action="${PATH_INFO##*/}"
+    video=""
+    channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
+    . "$_EXEC/page_channel.sh"
+    ;;
+  /channel/*/)
+    action=""
+    video=""
+    channel="${PATH_INFO#/channel/}" channel="${channel%%/*}"
+    . "$_EXEC/page_channel.sh"
+    ;;
+  /|/channel/)
+    . "${_EXEC}/page_channel.sh"
+    ;;
   /playlist/*) . "${_EXEC}/page_playlist.sh";;
   /search/*) . "${_EXEC}/page_search.sh";;
   *) . "${_EXEC}/page_404.sh";;
index 89720d2acb9faa74a84ca9bc12c6fb760d193416..1720276c3df252e7a1eb3423a63036e5fffc4ece 100755 (executable)
@@ -1,35 +1,12 @@
 #!/bin/sh
 
-. "$_EXEC/db_channel.sh"
-. "$_EXEC/db_video.sh"
-
-channel='' video='' action=''
-path_info="$PATH_INFO"
-path_info="${path_info#/channel/}"
-if [ "$(checkid "${path_info%%/*}")" ]; then
-  channel="${path_info%%/*}"
-  path_info="${path_info#*/}"
-fi
-if [ "$(checkid "${path_info%%/*}")" ]; then
-  video="${path_info%%/*}"
-  path_info="${path_info#*/}"
-fi
-action="${path_info}"
-unset path_info
-
-read_channel "$channel" \
-&& vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
+["$includepage_channel" ] && return 0
+includepage_channel="$0"
 
-AUTHOR(){
-  if [ "$CHANNEL_ID" -a "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
-    return 0
-  else
-    return 1
-  fi
-}
+. "$_EXEC/db_channel.sh"
+. "$_EXEC/widgets.sh"
 
-# Video
-# ID   NAME    DESCRIPTION     RESX    RESY    LENGTH  COVER   STATUS  UPLOADER        HITS
+read_channel "$channel"
 
 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
   newchannel)
@@ -43,13 +20,13 @@ AUTHOR(){
     fi
     ;;
   update_channel)
-    if [ ! "$channel" ]; then
+    if [ ! "$CHANNEL_ID" ]; then
       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
     elif [ ! "$USER_ID" ]; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NOTLOGGEDIN"
     elif ! AUTHOR; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOTALLOWED"
-    elif update_channel "$channel" "name=$(POST name)" \
+    elif update_channel "$CHANNEL_ID" "name=$(POST name)" \
                         "description=$(POST description)" \
                         "authors=$USER_ID"; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/"
@@ -61,6 +38,7 @@ AUTHOR(){
     REDIRECT "${_BASE}/channel/$CHANNEL_ID/"
     ;;
   newvideo)
+    . "$_EXEC/db_video.sh"
     video="$(POST video |checkid)"
 
     AUTHOR \
@@ -68,82 +46,21 @@ AUTHOR(){
 
     if [ ! "$video" ]; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_INVALID_ID"
-    elif [ ! "$channel" ]; then
+    elif [ ! "$CHANNEL_ID" ]; then
       REDIRECT "${_BASE}/channel/#ERROR_NOCHANNEL"
     elif [ ! "$USER_ID" ]; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NOTLOGGEDIN"
     elif ! AUTHOR; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_UPDATE_NOTALLOWED"
     elif new_video "$video"; then
-      REDIRECT "${_BASE}/channel/$CHANNEL_ID/${video}/"
+      REDIRECT "${_BASE}/channel/$CHANNEL_ID/$video/"
     else
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/#ERROR_NEWVIDEO_NOLOCK"
     fi
     ;;
 esac
 
-w_video(){
-  local thumb
-  local 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
-
-  if read_video; then
-    VIDEO_LENGTH="${VIDEO_LENGTH%.*}"
-    [ "${VIDEO_STATUS}" = public ] || AUTHOR || return 0
-
-    thumb="${_BASE}/video/${CHANNEL_ID}/${VIDEO_ID}.thumb.jpg"
-    [ "$NAME" = \\ ] && NAME="(Unnamed Video)"
-    printf '[div .video .thumb
-              [a href="%s/channel/%s/%s/"
-                [h3 . %s]
-                [figure [img src="%s" alt=""]]
-                [span .duration . %i:%02i]
-                [div .description . %s]
-              ]
-            ]' "$_BASE" "$CHANNEL_ID" "$VIDEO_ID" \
-               "$(HTML "${VIDEO_NAME:-(Unnamed Video)}")" \
-               "$thumb" \
-               "$((VIDEO_LENGTH / 60))" "$((VIDEO_LENGTH % 60))" \
-               "$(UNSTRING "$DESCR_CACHE")"
-  else
-    return 1
-  fi
-}
-
-w_channel_list(){
-  local vid_db
-  local CHANNEL_ID CHANNEL_NAME CHANNEL_DESCRIPTION CHANNEL_LOGO \
-        CHANNEL_THEME CHANNEL_AUTHORS CHANNEL_DESCR_CACHE CHANNEL_FUTUREUSE
-
-  [ "$USER_ID" ] && cat <<-EOF
-       [form .channel .newchannel method=POST
-         [hidden "channel" "$(timeid)"]
-         [submit "action" "newchannel" New Channel]
-       ]
-       EOF
-
-  [ -f "$chan_db" -a -r "$chan_db" ] \
-  && while read_channel; do
-    vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
-
-    cat <<-EOF
-       [div .channel
-         [div .description
-           [h2 [a href="${_BASE}/channel/${CHANNEL_ID}/" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
-           ${CHANNEL_DESCR_CACHE}
-         ]$(
-            [ -f "$vid_db" -a -r "$vid_db" ] \
-           && while w_video; do :; done <"$vid_db"
-         )
-       ]
-       EOF
-  done <"$chan_db"
-}
-
-if [ "$channel" -a "$video" ]; then
-  . ${_EXEC}/page_video.sh
-elif [ "$channel" -a "$action" = edit ]; then
+if [ "$CHANNEL_ID" -a "$action" = edit ]; then
   AUTHOR || REDIRECT "${_BASE}/$CHANNEL_ID/#ERROR_EDIT_NOTALLOWED"
   yield_page "$CHANNEL_NAME - Edit" "channel edit" <<-EOF
        [form .channel .edit method=POST
@@ -153,7 +70,8 @@ elif [ "$channel" -a "$action" = edit ]; then
          [submit "action" "update_channel_cancel" . Cancel]
        ]
        EOF
-elif [ "$channel" ]; then
+elif [ "$CHANNEL_ID" ]; then
+  vid_db="$_DATA/$CHANNEL_ID/videos.db"
   yield_page "$CHANNEL_NAME" "channel" <<-EOF
        [nav [a href="../" Channels] - [span $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]
          $(AUTHOR && printf ' - [a href="edit" edit]')
@@ -163,16 +81,24 @@ elif [ "$channel" ]; then
        [h1 .videos Videos]
        [div .videos . $(
          AUTHOR && printf '
-             [form .video .newvideo method=POST
-               [hidden "video" "%s"]
-               [submit "action" "newvideo" New Video]
-             ]' "$(timeid)"
+            [form .video .newvideo method=POST
+              [hidden "video" "%s"]
+              [submit "action" "newvideo" New Video]
+            ]' "$(timeid)"
          [ -f "$vid_db" -a -r "$vid_db" ] \
-         && while w_video "$ID"; do :; done <"$vid_db"
+         && while w_video; do :; done <"$vid_db"
        )]
        EOF
 else
   yield_page "Channels" "channels" <<-EOF
-       $(w_channel_list)
+       $([ "$USER_ID" ] && printf '
+         [form .channel .newchannel method=POST
+           [hidden "channel" "%s"]
+           [submit "action" "newchannel" New Channel]
+         ]' "$(timeid)"
+       )
+       $([ -f "$chan_db" -a -r "$chan_db" ] \
+         && while w_channel; do :; done <"$chan_db"
+       )
        EOF
 fi
index 3d425e32044168184bc9d5a0ce20980cf1ff2207..49f49a78181d08c12338395c6ea817b10c109d29 100755 (executable)
@@ -1,8 +1,14 @@
 #!/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"
 
 [ "$REQUEST_METHOD" = POST ] && case "$(POST action)" in
@@ -11,7 +17,7 @@ read_video "$video"
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_NOTLOGGEDIN"
     elif ! AUTHOR; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPDATE_NOTALLOWED"
-    elif update_video "$video" "name=$(POST name)" \
+    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
@@ -30,7 +36,7 @@ read_video "$video"
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPDATE_NOTALLOWED"
     elif [ "$(POST delconfirm)" != confirm ]; then
       REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_NOT_CONFIRMED"
-    elif delete_video "$video"; then
+    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" \
@@ -42,7 +48,7 @@ read_video "$video"
     ;;
 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_ID/$VIDEO_ID/#ERROR_UPLOAD_NOTALLOWED"
@@ -50,14 +56,14 @@ if [ "$REQUEST_METHOD" = POST -a "$channel" -a "$video" ]; then
     head -c "$CONTENT_LENGTH" >/dev/null
     REDIRECT "${_BASE}/channel/$CHANNEL_ID/$VIDEO_ID/#ERROR_UPLOAD_NOCLOBBER"
   elif UPLOAD "$_DATA/$CHANNEL_ID/$VIDEO_ID.upload.mp4"; then
-    update_video "$video" status=private
+    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
@@ -81,12 +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' ''
   frame_uploadprogress
 
-elif [ "$channel" -a "$video" -a "$VIDEO_STATUS" = void ]; 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
@@ -104,7 +110,7 @@ elif [ "$channel" -a "$video" -a "$VIDEO_STATUS" = void ]; then
        [div .description . ${VIDEO_DESCR_CACHE}]
        EOF
 
-elif [ "$channel" -a "$video" -a "$VIDEO_STATUS" != void ]; 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
index 63704071050e2683ef0374581c616408c786b855..e698573b84d3289e18edd809af53d3a78ab0f3e3 100755 (executable)
--- a/upload.sh
+++ b/upload.sh
@@ -1,5 +1,8 @@
 #!/bin/sh
 
+[ "$include_upload" ] && return 0
+include_upload="$0"
+
 UPLOAD(){
   local file="$1"
   local boundary line length=0
@@ -40,11 +43,11 @@ frame_uploadprogress() {
   </head><body id=uploadprogress>
   ' "$_BASE"
   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.upload.mp4" \
-        -a ! -f "$_DATA/$channel/$video.upload.mp4.upload" ]; do sleep 1; done
-  read size <"$_DATA/$channel/$video.upload.mp4.upload"
-  while [ -f "$_DATA/$channel/$video.upload.mp4.upload" ]; do
-    stat="$(stat -c %s "$_DATA/$channel/$video.upload.mp4" 2>&-)"
+  while [  ! -f "$_DATA/$CHANNEL_ID/${VIDEO_ID}.upload.mp4" \
+        -a ! -f "$_DATA/$CHANNEL_ID/${VIDEO_ID}.upload.mp4.upload" ]; do sleep 1; done
+  read size <"$_DATA/$CHANNEL_ID/${VIDEO_ID}.upload.mp4.upload"
+  while [ -f "$_DATA/$CHANNEL_ID/${VIDEO_ID}.upload.mp4.upload" ]; do
+    stat="$(stat -c %s "$_DATA/$CHANNEL_ID/${VIDEO_ID}.upload.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
diff --git a/widgets.sh b/widgets.sh
new file mode 100755 (executable)
index 0000000..1aa6272
--- /dev/null
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+["$include_widgets" ] && return 0
+include_widgets="$0"
+
+. "$_EXEC/db_channel.sh"
+. "$_EXEC/db_video.sh"
+
+AUTHOR(){
+  if [ "$CHANNEL_ID" -a "$USER_ID" -a ! "${CHANNEL_AUTHORS##*${USER_ID}*}" ]; then
+    return 0
+  else
+    return 1
+  fi
+}
+
+w_video(){
+  local thumb
+  eval "$LOCAL_VIDEO"
+
+  if read_video; then
+    VIDEO_LENGTH="${VIDEO_LENGTH%.*}"
+    [ "${VIDEO_STATUS}" = public ] || AUTHOR || return 0
+
+    thumb="${_BASE}/video/${CHANNEL_ID}/${VIDEO_ID}.thumb.jpg"
+    [ "$NAME" = \\ ] && NAME="(Unnamed Video)"
+    printf '[div .video .thumb
+              [a href="%s/channel/%s/%s/"
+                [h3 . %s]
+                [figure [img src="%s" alt=""]]
+                [span .duration . %i:%02i]
+                [div .description . %s]
+              ]
+            ]' "$_BASE" "$CHANNEL_ID" "$VIDEO_ID" \
+               "$(HTML "${VIDEO_NAME:-(Unnamed Video)}")" \
+               "$thumb" \
+               "$((VIDEO_LENGTH / 60))" "$((VIDEO_LENGTH % 60))" \
+               "$(UNSTRING "$DESCR_CACHE")"
+  else
+    return 1
+  fi
+}
+
+w_channel(){
+  local vid_db
+  eval "$LOCAL_CHANNEL"
+
+  if read_channel; then
+    vid_db="${_DATA}/${CHANNEL_ID}/videos.db"
+
+    cat <<-EOF
+       [div .channel
+         [div .description
+           [h2 [a href="${_BASE}/channel/${CHANNEL_ID}/" $(HTML "${CHANNEL_NAME:-(Unnamed Channel)}")]]
+           ${CHANNEL_DESCR_CACHE}
+         ]$(
+            [ -f "$vid_db" -a -r "$vid_db" ] \
+           && while w_video; do :; done <"$vid_db"
+         )
+       ]
+       EOF
+  else
+    return 1
+  fi
+}