From eaa8b25194f1c98e481cf999d6926fe2c249d29f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Fri, 22 Oct 2021 00:55:57 +0200 Subject: [PATCH] refactoring: fewer global variables, manage all url parsing in index.cgi, split some widgets into separate file --- db_channel.sh | 5 ++ db_video.sh | 19 +++++-- index.cgi | 28 ++++++++++- page_channel.sh | 128 ++++++++++-------------------------------------- page_video.sh | 26 ++++++---- upload.sh | 13 +++-- widgets.sh | 65 ++++++++++++++++++++++++ 7 files changed, 162 insertions(+), 122 deletions(-) create mode 100755 widgets.sh diff --git a/db_channel.sh b/db_channel.sh index 9c7372d..1533481 100755 --- a/db_channel.sh +++ b/db_channel.sh @@ -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() { diff --git a/db_video.sh b/db_video.sh index e2cc09d..78d1085 100755 --- a/db_video.sh +++ b/db_video.sh @@ -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}.$$" diff --git a/index.cgi b/index.cgi index 0d3c668..76e3592 100755 --- 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";; diff --git a/page_channel.sh b/page_channel.sh index 89720d2..1720276 100755 --- a/page_channel.sh +++ b/page_channel.sh @@ -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 diff --git a/page_video.sh b/page_video.sh index 3d425e3..49f49a7 100755 --- a/page_video.sh +++ b/page_video.sh @@ -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 diff --git a/upload.sh b/upload.sh index 6370407..e698573 100755 --- 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() { ' "$_BASE" printf '
%i / %i
\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 '
%iMB / %iMB
\n' \ "$(( stat * 100 / size ))" "$((stat / 1048576))" "$((size / 1048576))" sleep 1 diff --git a/widgets.sh b/widgets.sh new file mode 100755 index 0000000..1aa6272 --- /dev/null +++ b/widgets.sh @@ -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 +} -- 2.39.2